diff --git a/pom.xml b/pom.xml
index 0a4c07f..40c51c9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,209 +1,172 @@
- 4.0.0
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ 4.0.0
- com.example
- DemoGraphQL
- 0.0.1-SNAPSHOT
- jar
+ com.example
+ DemoGraphQL
+ 0.0.1-SNAPSHOT
+ jar
- DemoGraphQL
- Demo project for Spring Boot
+ DemoGraphQL
+ Demo project for Spring Boot
-
- org.springframework.boot
- spring-boot-starter-parent
- 2.6.3
-
-
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.7.1
+
+
-
- UTF-8
- UTF-8
- 11.1.0
- 1.4.200
- 1.18.20
- 11
-
-
-
- org.springframework.boot
- spring-boot-starter-web
-
-
- org.springframework.boot
- spring-boot-starter-data-jpa
-
-
- org.springframework.boot
- spring-boot-starter-tomcat
-
-
- org.springframework.boot
- spring-boot-configuration-processor
- true
-
-
- org.projectlombok
- lombok
- ${lombok.version}
- provided
-
+
+ UTF-8
+ UTF-8
+ 11.1.0
+ 1.4.200
+ 1.18.20
+ 11
+
+
+
+ org.springframework.boot
+ spring-boot-starter-graphql
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+ org.springframework.boot
+ spring-boot-starter-tomcat
+
+
+ org.springframework.boot
+ spring-boot-configuration-processor
+ true
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+ provided
+
-
- com.graphql-java-kickstart
- graphql-spring-boot-starter
- ${graphql.spring.starter.version}
-
-
- com.graphql-java-kickstart
- graphiql-spring-boot-starter
- ${graphql.spring.starter.version}
- runtime
-
-
- com.graphql-java-kickstart
- altair-spring-boot-starter
- ${graphql.spring.starter.version}
- runtime
-
-
- com.graphql-java-kickstart
- playground-spring-boot-starter
- ${graphql.spring.starter.version}
- runtime
-
-
- com.graphql-java-kickstart
- voyager-spring-boot-starter
- ${graphql.spring.starter.version}
- runtime
-
-
- com.h2database
- h2
- ${h2.version}
- runtime
-
-
- com.github.gavlyukovskiy
- p6spy-spring-boot-starter
- 1.7.0
-
+
+ com.h2database
+ h2
+ ${h2.version}
+ runtime
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.springframework.graphql
+ spring-graphql-test
+ test
+
+
-
- org.springframework.boot
- spring-boot-starter-test
- test
-
-
- com.graphql-java-kickstart
- graphql-spring-boot-starter-test
- ${graphql.spring.starter.version}
- test
-
-
- org.springframework.boot
- spring-boot-devtools
- true
-
-
+
+
+
+
+ maven-compiler-plugin
+
+ ${java.version}
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+
+
+ org.springframework.boot
+ spring-boot-configuration-processor
+ 2.6.3
+
+
+
+
+
+
+
+
+ src/main/resources
+ true
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ org.projectlombok
+ lombok-maven-plugin
+ ${lombok.version}.0
+
+
+ generate-sources
+
+ delombok
+
+
+
+
+
+
-
-
-
-
- maven-compiler-plugin
-
- ${java.version}
-
-
- org.projectlombok
- lombok
- ${lombok.version}
-
-
- org.springframework.boot
- spring-boot-configuration-processor
- 2.6.3
-
-
-
-
-
-
-
-
- src/main/resources
- true
-
-
-
-
- org.springframework.boot
- spring-boot-maven-plugin
-
-
- org.projectlombok
- lombok-maven-plugin
- ${lombok.version}.0
-
-
- generate-sources
-
- delombok
-
-
-
-
-
-
+
+
+ jdk16
+
+ 16
+
+
+ 16
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ ${java.version}
+
+ true
+
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
+ -J--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED
+
+
+
+
+
+
+
-
-
- jdk16
-
- 16
-
-
- 16
-
-
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
-
- ${java.version}
-
- true
-
- -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
- -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
- -J--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
- -J--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
- -J--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
- -J--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
- -J--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
- -J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
- -J--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
- -J--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED
-
-
-
-
-
-
-
-
-
-
- jcenter
- https://jcenter.bintray.com/
-
-
+
+
+ jcenter
+ https://jcenter.bintray.com/
+
+
diff --git a/src/main/java/com/example/DemoGraphQL/DemoGraphQlApplication.java b/src/main/java/com/example/DemoGraphQL/DemoGraphQlApplication.java
index 55da939..745a291 100644
--- a/src/main/java/com/example/DemoGraphQL/DemoGraphQlApplication.java
+++ b/src/main/java/com/example/DemoGraphQL/DemoGraphQlApplication.java
@@ -1,81 +1,29 @@
package com.example.DemoGraphQL;
-import com.example.DemoGraphQL.exception.GraphQLErrorAdapter;
import com.example.DemoGraphQL.model.Author;
import com.example.DemoGraphQL.model.Book;
import com.example.DemoGraphQL.repository.AuthorRepository;
import com.example.DemoGraphQL.repository.BookRepository;
-import com.example.DemoGraphQL.resolver.BookResolver;
-import com.example.DemoGraphQL.resolver.Mutation;
-import com.example.DemoGraphQL.resolver.Query;
-import graphql.ExceptionWhileDataFetching;
-import graphql.GraphQLError;
-import graphql.kickstart.execution.error.GraphQLErrorHandler;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.stream.Collectors;
-
@SpringBootApplication
public class DemoGraphQlApplication extends SpringBootServletInitializer {
- public static void main(String[] args) {
- SpringApplication.run(DemoGraphQlApplication.class, args);
- }
-
- @Bean
- public GraphQLErrorHandler errorHandler() {
- return new GraphQLErrorHandler() {
- @Override
- public List processErrors(List errors) {
- List clientErrors = errors.stream()
- .filter(this::isClientError)
- .collect(Collectors.toList());
-
- List serverErrors = errors.stream()
- .filter(e -> !isClientError(e))
- .map(GraphQLErrorAdapter::new)
- .collect(Collectors.toList());
-
- List e = new ArrayList<>();
- e.addAll(clientErrors);
- e.addAll(serverErrors);
- return e;
- }
-
- protected boolean isClientError(GraphQLError error) {
- return !(error instanceof ExceptionWhileDataFetching || error instanceof Throwable);
- }
- };
- }
-
- @Bean
- public BookResolver authorResolver(AuthorRepository authorRepository) {
- return new BookResolver(authorRepository);
- }
-
- @Bean
- public Query query(AuthorRepository authorRepository, BookRepository bookRepository) {
- return new Query(authorRepository, bookRepository);
- }
-
- @Bean
- public Mutation mutation(AuthorRepository authorRepository, BookRepository bookRepository) {
- return new Mutation(authorRepository, bookRepository);
- }
+ public static void main(String[] args) {
+ SpringApplication.run(DemoGraphQlApplication.class, args);
+ }
- @Bean
- public CommandLineRunner demo(AuthorRepository authorRepository, BookRepository bookRepository) {
- return (args) -> {
- Author author = new Author("Herbert", "Schildt");
- authorRepository.save(author);
- bookRepository.save(new Book("Java: A Beginner's Guide, Sixth Edition", "0071809252", 728, author));
- };
- }
+ @Bean
+ public CommandLineRunner demo(AuthorRepository authorRepository, BookRepository bookRepository) {
+ return (args) -> {
+ Author author = new Author("Herbert", "Schildt");
+ authorRepository.save(author);
+ bookRepository.save(new Book("Java: A Beginner's Guide, Sixth Edition", "0071809252", 728, author));
+ };
+ }
}
diff --git a/src/main/java/com/example/DemoGraphQL/controller/AuthorController.java b/src/main/java/com/example/DemoGraphQL/controller/AuthorController.java
new file mode 100644
index 0000000..fca28a7
--- /dev/null
+++ b/src/main/java/com/example/DemoGraphQL/controller/AuthorController.java
@@ -0,0 +1,37 @@
+package com.example.DemoGraphQL.controller;
+
+import com.example.DemoGraphQL.model.Author;
+import com.example.DemoGraphQL.repository.AuthorRepository;
+import org.springframework.graphql.data.method.annotation.Argument;
+import org.springframework.graphql.data.method.annotation.MutationMapping;
+import org.springframework.graphql.data.method.annotation.QueryMapping;
+import org.springframework.stereotype.Controller;
+
+@Controller
+public class AuthorController {
+
+ private final AuthorRepository authorRepository;
+
+ public AuthorController(AuthorRepository authorRepository) {
+ this.authorRepository = authorRepository;
+ }
+
+ @MutationMapping
+ public Author newAuthor(@Argument String firstName, @Argument String lastName) {
+ Author author = new Author();
+ author.setFirstName(firstName);
+ author.setLastName(lastName);
+ authorRepository.save(author);
+ return author;
+ }
+
+ @QueryMapping
+ public Iterable findAllAuthors() {
+ return authorRepository.findAll();
+ }
+
+ @QueryMapping
+ public long countAuthors() {
+ return authorRepository.count();
+ }
+}
diff --git a/src/main/java/com/example/DemoGraphQL/controller/BookController.java b/src/main/java/com/example/DemoGraphQL/controller/BookController.java
new file mode 100644
index 0000000..80d7c1b
--- /dev/null
+++ b/src/main/java/com/example/DemoGraphQL/controller/BookController.java
@@ -0,0 +1,72 @@
+package com.example.DemoGraphQL.controller;
+
+import com.example.DemoGraphQL.exception.BookNotFoundException;
+import com.example.DemoGraphQL.model.Author;
+import com.example.DemoGraphQL.model.Book;
+import com.example.DemoGraphQL.repository.AuthorRepository;
+import com.example.DemoGraphQL.repository.BookRepository;
+import org.springframework.graphql.data.method.annotation.Argument;
+import org.springframework.graphql.data.method.annotation.MutationMapping;
+import org.springframework.graphql.data.method.annotation.QueryMapping;
+import org.springframework.graphql.data.method.annotation.SchemaMapping;
+import org.springframework.stereotype.Controller;
+
+import java.util.Optional;
+
+@Controller
+public class BookController {
+
+ private final BookRepository bookRepository;
+ private final AuthorRepository authorRepository;
+
+ public BookController(BookRepository bookRepository, AuthorRepository authorRepository) {
+ this.bookRepository = bookRepository;
+ this.authorRepository = authorRepository;
+ }
+
+ @MutationMapping
+ public Book newBook(@Argument String title, @Argument String isbn, @Argument Integer pageCount, @Argument Long authorId) {
+ Book book = new Book();
+ book.setAuthor(new Author(authorId));
+ book.setTitle(title);
+ book.setIsbn(isbn);
+ book.setPageCount(pageCount != null ? pageCount : 0);
+ bookRepository.save(book);
+ return book;
+ }
+
+ @MutationMapping
+ public Book updateBookPageCount(@Argument Integer pageCount, @Argument Long id) {
+ Optional opt = bookRepository.findById(id);
+ if (opt.isPresent()) {
+ Book book = opt.get();
+ book.setPageCount(pageCount);
+ bookRepository.save(book);
+ return book;
+ }
+ throw new BookNotFoundException("The book to be updated was found", id);
+ }
+
+ @QueryMapping
+ public Iterable findAllBooks() {
+ return bookRepository.findAll();
+ }
+
+ @QueryMapping
+ public long countBooks() {
+ return bookRepository.count();
+ }
+
+ @SchemaMapping
+ public Author getAuthor(Book book) {
+ return authorRepository.findById(book.getAuthor()
+ .getId())
+ .orElseThrow(null);
+ }
+
+ @MutationMapping
+ public boolean deleteBook(@Argument Long id) {
+ bookRepository.deleteById(id);
+ return true;
+ }
+}
diff --git a/src/main/java/com/example/DemoGraphQL/resolver/BookResolver.java b/src/main/java/com/example/DemoGraphQL/resolver/BookResolver.java
deleted file mode 100644
index a00cbc2..0000000
--- a/src/main/java/com/example/DemoGraphQL/resolver/BookResolver.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.example.DemoGraphQL.resolver;
-
-import com.example.DemoGraphQL.model.Author;
-import com.example.DemoGraphQL.model.Book;
-import com.example.DemoGraphQL.repository.AuthorRepository;
-import graphql.kickstart.tools.GraphQLResolver;
-
-public class BookResolver implements GraphQLResolver {
- private AuthorRepository authorRepository;
-
- public BookResolver(AuthorRepository authorRepository) {
- this.authorRepository = authorRepository;
- }
-
- public Author getAuthor(Book book) {
- return authorRepository.findById(book.getAuthor()
- .getId())
- .orElseThrow(null);
- }
-}
diff --git a/src/main/java/com/example/DemoGraphQL/resolver/Mutation.java b/src/main/java/com/example/DemoGraphQL/resolver/Mutation.java
deleted file mode 100644
index c20e701..0000000
--- a/src/main/java/com/example/DemoGraphQL/resolver/Mutation.java
+++ /dev/null
@@ -1,58 +0,0 @@
-package com.example.DemoGraphQL.resolver;
-
-import com.example.DemoGraphQL.exception.BookNotFoundException;
-import com.example.DemoGraphQL.model.Author;
-import com.example.DemoGraphQL.model.Book;
-import com.example.DemoGraphQL.repository.AuthorRepository;
-import com.example.DemoGraphQL.repository.BookRepository;
-import graphql.kickstart.tools.GraphQLMutationResolver;
-
-import java.util.Optional;
-
-public class Mutation implements GraphQLMutationResolver {
- private BookRepository bookRepository;
- private AuthorRepository authorRepository;
-
- public Mutation(AuthorRepository authorRepository, BookRepository bookRepository) {
- this.authorRepository = authorRepository;
- this.bookRepository = bookRepository;
- }
-
- public Author newAuthor(String firstName, String lastName) {
- Author author = new Author();
- author.setFirstName(firstName);
- author.setLastName(lastName);
-
- authorRepository.save(author);
-
- return author;
- }
-
- public Book newBook(String title, String isbn, Integer pageCount, Long authorId) {
- Book book = new Book();
- book.setAuthor(new Author(authorId));
- book.setTitle(title);
- book.setIsbn(isbn);
- book.setPageCount(pageCount != null ? pageCount : 0);
-
- bookRepository.save(book);
-
- return book;
- }
-
- public boolean deleteBook(Long id) {
- bookRepository.deleteById(id);
- return true;
- }
-
- public Book updateBookPageCount(Integer pageCount, Long id) {
- Optional opt = bookRepository.findById(id);
- if (opt.isPresent()) {
- Book book = opt.get();
- book.setPageCount(pageCount);
- bookRepository.save(book);
- return book;
- }
- throw new BookNotFoundException("The book to be updated was found", id);
- }
-}
diff --git a/src/main/java/com/example/DemoGraphQL/resolver/Query.java b/src/main/java/com/example/DemoGraphQL/resolver/Query.java
deleted file mode 100644
index 3bd07af..0000000
--- a/src/main/java/com/example/DemoGraphQL/resolver/Query.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package com.example.DemoGraphQL.resolver;
-
-import com.example.DemoGraphQL.model.Author;
-import com.example.DemoGraphQL.model.Book;
-import com.example.DemoGraphQL.repository.AuthorRepository;
-import com.example.DemoGraphQL.repository.BookRepository;
-import graphql.kickstart.tools.GraphQLQueryResolver;
-
-public class Query implements GraphQLQueryResolver {
- private BookRepository bookRepository;
- private AuthorRepository authorRepository;
-
- public Query(AuthorRepository authorRepository, BookRepository bookRepository) {
- this.authorRepository = authorRepository;
- this.bookRepository = bookRepository;
- }
-
- public Iterable findAllBooks() {
- return bookRepository.findAll();
- }
-
- public long countBooks() {
- return bookRepository.count();
- }
-
- public Iterable findAllAuthors() {
- return authorRepository.findAll();
- }
-
- public long countAuthors() {
- return authorRepository.count();
- }
-}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 2e4fb50..1e8f51c 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -1,12 +1,13 @@
-graphql:
- servlet:
- mapping: /graphql
myprops:
artifactId: '@project.artifactId@'
version: '@project.version@'
h2:
version: '@h2.version@'
spring:
+ graphql:
+ graphiql:
+ enabled: true
+ path: /graphiql
datasource:
url: jdbc:h2:mem:testdb;TRACE_LEVEL_FILE=3
username: sa