如何在使用spring boot时设置多个Graphql文件?
我试图在项目中为每个对象使用graphql文件,但不确定如何正确设置它们。我一直收到一个错误,我查看了很多GitHub回购协议,但没有找到任何可以修复它的东西。我似乎认为电影和剧院都应该有createUser方法,但我不知道为什么如何在使用spring boot时设置多个Graphql文件?,graphql,graphql-java,Graphql,Graphql Java,我试图在项目中为每个对象使用graphql文件,但不确定如何正确设置它们。我一直收到一个错误,我查看了很多GitHub回购协议,但没有找到任何可以修复它的东西。我似乎认为电影和剧院都应该有createUser方法,但我不知道为什么 Caused by: com.coxautodev.graphql.tools.FieldResolverError: No method found with any of the following signatures (with or without gra
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
}
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
}
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
}
剧院。图形
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
}
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
}
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
}
电影。图形
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
}
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
}
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.****.****.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!");
}
}
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);
}
}
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);
}
}
theatemutation.java
import com.****.****.models.Movie;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MovieRepository extends JpaRepository<Movie, Long> {
}
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!");
}
}
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);
}
}
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);
}
}
您的初始突变定义如下:
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;
导入javassist.NotFoundException;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
@组成部分
公共类DefaultMutation实现GraphQLMutationResolver{
公共用户createUser(字符串名称、字符串用户名、字符串密码){
//在这里实现你的逻辑
}
公共用户updateUser(字符串id、字符串名称、字符串用户名、字符串密码){
//在这里实现你的逻辑
}
公共布尔deleteUser(字符串id){
//在这里实现你的逻辑
}
}
Hello,您可以添加扩展GraphQLMutationResolver
的类吗。我想你给他们打了MovieMutation
和theatemutation
?他们已经添加了。谢谢你的帮助!我重新检查后发现了问题。我意外地为我的默认文件实现了GraphQLQueryResolver。