Я пытаюсь использовать файл graphql для каждого объекта в проекте и не уверен, как правильно их настроить. Я продолжаю получать сообщение об ошибке, я просмотрел много репозиториев на GitHub, но не нашел ничего, что могло бы это исправить. Мне кажется, что он думает, что и mov ie, и Theatre должны иметь методы 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)
Вот мои текущие файлы:
пользователей. 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);
}
}
TheatreMutation. 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);
}
}