1

I am trying to create a mutation method for adding movies to a users favorite list, but am unsure how to structure the mutation to include a specified user.

I have a query to view all favorites a user might have which looks like this.

query getFavorites {
  user(id: 2) {
    name
    email
    username
    favorites {
      title
      length
    }
  }
}

here is the query method for that

@GraphQLQuery
public List<Movie> getFavorites(@GraphQLContext User u) {
    User user = userRepository.findById(u.getId()).get();
    return user.getMovies();

}

But I am unsure how to write the mutation with respect a specific user

here is my mutation method for adding a favorite, currently I an just getting a NullPointerException because of a null user.

@GraphQLMutation
public Movie addFavorite(@GraphQLContext User user, long movieId) {
    Movie movie = movieRepository.findById(movieId).orElse(null);
    user.getMovies().addAll(Arrays.asList(movie));
    userRepository.save(user);
    return movie;
}

0 Answers0