【问题标题】:How to setup multiple Graphql files while using spring boot?使用 spring boot 时如何设置多个 Graphql 文件?
【发布时间】:2020-07-17 23:32:11
【问题描述】:

我正在尝试在项目中为每个对象使用一个 graphql 文件,但不确定如何正确设置它们。我一直收到一个错误,我查看了许多 GitHub 存储库,但没有找到任何修复它的方法。我似乎认为电影和剧院都应该有 createUser 方法,但我不知道为什么。

 Caused by: com.coxautodev.graphql.tools.FieldResolverError: No method found with any of the following signatures (with or without graphql.schema.DataFetchingEnvironment as the last argument), in priority order:
  com.****.****.resolvers.mutations.MovieMutation.createUser(~name, ~username, ~password)
  com.****.****.resolvers.mutations.MovieMutation.getCreateUser(~name, ~username, ~password)
  com.****.****.resolvers.mutations.TheaterMutation.createUser(~name, ~username, ~password)
  com.****.****.resolvers.mutations.TheaterMutation.getCreateUser(~name, ~username, ~password)

这是我当前的文件:

users.graphqls

schema {
    query: Query
    mutation: Mutation
}

type User {
    id: ID
    name: String
    username: String
    password: String
}

type Query {
    users: [User]
}

type Mutation {
    createUser(name: String!, username: String!, password: String!): User!
    updateUser(id: ID!, name: String!, username: String!, password: String!): User!
    deleteUser(id: ID!): Boolean
}

theaters.graphqls

type Theater {
    id: ID
    name: String
    location: String
}

extend type Query {
    theaters: [Theater]
}

extend type Mutation {
    createTheater(name: String!, location: String!): Theater!
    updateTheater(id: ID!, name: String!, location: String!): Theater!
    deleteTheater(id: ID!): Boolean
}

movies.graphqls

type Movie {
    id: ID
    title: String
    length: Int
}

extend type Query {
    movies: [Movie]
}

extend type Mutation {
    createMovie(title: String!, length: Int!): Movie!
    updateMovie(id: ID!, title: String!, length: Int!): Movie!
    deleteMovie(id: ID!): Boolean
}

我所有的存储库都是这样的:

MovieRepository.java

import com.****.****.models.Movie;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MovieRepository extends JpaRepository<Movie, Long> {
}

我所有的查询都是这样的

import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import com.****.****.models.Movie;
import com.****.****.repositories.MovieRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class MovieQuery implements GraphQLQueryResolver {

    private final MovieRepository movieRepository;

    public List<Movie> movies() {
        return movieRepository.findAll();
    }
}

这是我的服务

MovieService.java

import com.****.****.models.Movie;
import com.****.****.repositories.MovieRepository;
import javassist.NotFoundException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@AllArgsConstructor
public class MovieService {

    private MovieRepository movieRepository;

    @Transactional
    public Movie createMovie(String title, int length) {
        Movie movie = new Movie();
        movie.setTitle(title);
        movie.setLength(length);
        movieRepository.save(movie);
        return movie;
    }

    @Transactional(readOnly = true)
    public List<Movie> movies(){
        return movieRepository.findAll();
    }

    @Transactional
    public boolean deleteById(long id) {
        movieRepository.deleteById(id);
        return true;
    }

    @Transactional
    public Movie updateMovie(long id, String title, int length) throws NotFoundException {
        Optional<Movie> optionalMovie = movieRepository.findById(id);

        if (optionalMovie.isPresent()) {
            Movie movie = optionalMovie.get();

            if (title != null) {
                movie.setTitle(title);
            }
            if(length > 0) {
                movie.setLength(length);
            }

            movieRepository.save(movie);
            return movie;
        }

        throw new NotFoundException("No found movie to update!");
    }
}

MovieMutation.java

import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import com.****.****.models.Movie;
import com.****.****.service.MovieService;
import javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MovieMutation implements GraphQLMutationResolver {

    @Autowired
    private MovieService movieService;

    public Movie createMovie(String title, int length) {
        return movieService.createMovie(title, length);
    }

    public boolean deleteMovie(long id) {
        movieService.deleteById(id);
        return true;
    }

    public Movie updateMovie(long id, String title, int length) throws NotFoundException {
        return movieService.updateMovie(id, title, length);
    }
}

TheaterMutation.java

import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import com.****.****.models.Theater;
import com.****.****.service.TheaterService;
import javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TheaterMutation implements GraphQLMutationResolver {

    @Autowired
    private TheaterService theaterService;

    public Theater createTheater(String name, String location) {
        return theaterService.createTheater(name, location);
    }

    public boolean deleteTheater(long id) {
        theaterService.deleteById(id);
        return true;
    }

    public Theater updateTheater(long id, String name, String location) throws NotFoundException {
        return theaterService.updateTheater(id, name, location);
    }
}

【问题讨论】:

  • 您好,能否添加扩展GraphQLMutationResolver 的类。我想你叫他们MovieMutationTheaterMutation
  • 它们已被添加。

标签: graphql graphql-java


【解决方案1】:

你的初始突变是这样定义的:

type Mutation {
    createUser(name: String!, username: String!, password: String!): User!
    updateUser(id: ID!, name: String!, username: String!, password: String!): User!
    deleteUser(id: ID!): Boolean
}

理解这里的问题需要考虑的重要行是createUser(name: String!, username: String!, password: String!): User!

因此,您的错误消息应该是不言自明的。 Graphql 正在尝试在您注册的任何 GraphQLMutationResolver 中解析具有以下签名 createUser(~name, ~username, ~password) 的方法。但是,他找不到它,因此无法启动。

您必须在某处添加此方法。事实上,您应该为所有这些方法添加实现。我建议执行以下操作:

import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DefaultMutation implements GraphQLMutationResolver {

    public User createUser(String name, String username, String password) {
        // Implement your logic here
    }

    public User updateUser(String id, String name, String username, String password) {
        // Implement your logic here
    }

    public Boolean deleteUser(String id) {
        // Implement your logic here
    }    
}

【讨论】:

  • 感谢您的帮助!再次查看后发现问题所在。我不小心为我的默认突变文件实现了 GraphQLQueryResolver。
猜你喜欢
  • 2018-02-03
  • 2017-02-24
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 2018-10-30
  • 2021-11-27
  • 2021-11-16
  • 2020-08-05
相关资源
最近更新 更多