diff --git a/spring-boot-modules/spring-boot-libraries/README.md b/spring-boot-modules/spring-boot-libraries/README.md index 10c56ca576..6976435866 100644 --- a/spring-boot-modules/spring-boot-libraries/README.md +++ b/spring-boot-modules/spring-boot-libraries/README.md @@ -15,3 +15,21 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring Boot and Togglz Aspect](https://www.baeldung.com/spring-togglz) - [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql) - [An Introduction to Kong](https://www.baeldung.com/kong) + +### GraphQL sample queries + +Query +```shell script +curl \ +--request POST 'localhost:8081/graphql' \ +--header 'Content-Type: application/json' \ +--data-raw '{"query":"query {\n recentPosts(count: 2, offset: 0) {\n id\n title\n author {\n id\n posts {\n id\n }\n }\n }\n}"}' +``` + +Mutation +```shell script +curl \ +--request POST 'localhost:8081/graphql' \ +--header 'Content-Type: application/json' \ +--data-raw '{"query":"mutation {\n writePost(title: \"New Title\", author: \"Author2\", text: \"New Text\") {\n id\n category\n author {\n id\n name\n }\n }\n}"}' +``` diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/demo/DemoApplication.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/demo/DemoApplication.java index eb091b4695..e30ee6104d 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/demo/DemoApplication.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/demo/DemoApplication.java @@ -2,12 +2,14 @@ package com.baeldung.demo; import com.baeldung.graphql.GraphqlConfiguration; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; - +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.context.annotation.Import; @SpringBootApplication @Import(GraphqlConfiguration.class) +@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class}) public class DemoApplication { public static void main(String[] args) { diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/PostResolver.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/PostResolver.java index dbfde330ea..329d1f469a 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/PostResolver.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/PostResolver.java @@ -1,7 +1,5 @@ package com.baeldung.graphql; -import java.util.Optional; - import com.coxautodev.graphql.tools.GraphQLResolver; public class PostResolver implements GraphQLResolver { @@ -11,7 +9,7 @@ public class PostResolver implements GraphQLResolver { this.authorDao = authorDao; } - public Optional getAuthor(Post post) { - return authorDao.getAuthor(post.getAuthorId()); + public Author getAuthor(Post post) { + return authorDao.getAuthor(post.getAuthorId()).orElseThrow(RuntimeException::new); } } diff --git a/spring-boot-modules/spring-boot-libraries/src/main/resources/demo.yml b/spring-boot-modules/spring-boot-libraries/src/main/resources/demo.yml new file mode 100644 index 0000000000..11e54c5449 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/resources/demo.yml @@ -0,0 +1,2 @@ +server: + port: 8081 diff --git a/spring-boot-modules/spring-boot-libraries/src/main/resources/graphql/post.graphqls b/spring-boot-modules/spring-boot-libraries/src/main/resources/graphql/post.graphqls new file mode 100644 index 0000000000..0e42f7255c --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/resources/graphql/post.graphqls @@ -0,0 +1,24 @@ +type Post { + id: ID! + title: String! + text: String! + category: String + author: Author! +} + +type Author { + id: ID! + name: String! + thumbnail: String + posts: [Post]! +} + +# The Root Query for the application +type Query { + recentPosts(count: Int, offset: Int): [Post]! +} + +# The Root Mutation for the application +type Mutation { + writePost(title: String!, text: String!, category: String, author: String!) : Post! +}