Skip to content

Instantly share code, notes, and snippets.

@atesibrahim
Created August 1, 2022 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atesibrahim/136030f86a2a6c3d72a2a4a28bf1bba7 to your computer and use it in GitHub Desktop.
Save atesibrahim/136030f86a2a6c3d72a2a4a28bf1bba7 to your computer and use it in GitHub Desktop.
@Controller
@AllArgsConstructor
public class BookMutation {
BookRepository bookRepository;
@MutationMapping
public BookOutput newBook(@Argument("input") NewBook newBook){
Book book = Book.builder().authorName(newBook.getAuthorName()).title(newBook.getTitle()).build();
Book book1 = bookRepository.save(book);
return BookOutput.builder().authorName(book1.getAuthorName()).title(book1.getTitle()).build();
}
@MutationMapping
public BookOutput deleteBook(@Argument("input") BookInput bookInput){
Book deleteBook = new Book();
Optional<Book> findBook = bookRepository.findById(bookInput.getId());
if(findBook.isPresent()){
bookRepository.delete(findBook.get());
deleteBook = findBook.get();
}
return BookOutput.builder().id(deleteBook.getId()).title(deleteBook.getTitle()).authorName(deleteBook.getAuthorName()).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment