From c18ce8a0cffbd1eae83a1b4b266c6678a2bebbb4 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Fri, 7 Oct 2022 18:21:41 +0100 Subject: [PATCH] [JAVA-14663] Update Graphql code for Boot 2.7.x changes and move to new module (#12729) --- spring-boot-modules/pom.xml | 3 +- ...GraphQL collection.postman_collection.json | 124 ++++++++++++++++++ .../spring-boot-graphql/README.md | 29 ++++ .../spring-boot-graphql/pom.xml | 41 ++++++ .../java/com/baeldung/graphql/Author.java | 11 ++ .../baeldung/graphql/AuthorController.java | 21 +++ .../java/com/baeldung/graphql/AuthorDao.java | 18 +++ .../baeldung/graphql/GraphqlApplication.java} | 8 +- .../graphql/GraphqlConfiguration.java | 28 +--- .../main/java/com/baeldung/graphql/Post.java | 14 ++ .../com/baeldung/graphql/PostController.java | 52 ++++++++ .../java/com/baeldung/graphql/PostDao.java | 14 +- .../src/main/resources/application.yml | 4 + .../src/main/resources/graphql/post.graphqls | 2 +- .../PostControllerIntegrationTest.java | 54 ++++++++ .../baeldung/graphql/SpringContextTest.java | 12 ++ ...GraphQL collection.postman_collection.json | 4 +- .../graphql-test/create_post.graphql | 8 ++ .../graphql-test/recent_posts.graphql | 13 ++ .../recent_posts_expected_response.json | 28 ++++ .../spring-boot-libraries/README.md | 22 +--- .../spring-boot-libraries/pom.xml | 16 --- .../java/com/baeldung/graphql/Author.java | 31 ----- .../java/com/baeldung/graphql/AuthorDao.java | 16 --- .../com/baeldung/graphql/AuthorResolver.java | 17 --- .../java/com/baeldung/graphql/Mutation.java | 25 ---- .../main/java/com/baeldung/graphql/Post.java | 49 ------- .../com/baeldung/graphql/PostResolver.java | 20 --- .../main/java/com/baeldung/graphql/Query.java | 17 --- 29 files changed, 452 insertions(+), 249 deletions(-) create mode 100644 spring-boot-modules/spring-boot-graphql/GraphQL collection.postman_collection.json create mode 100644 spring-boot-modules/spring-boot-graphql/README.md create mode 100644 spring-boot-modules/spring-boot-graphql/pom.xml create mode 100644 spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Author.java create mode 100644 spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorController.java create mode 100644 spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorDao.java rename spring-boot-modules/{spring-boot-libraries/src/main/java/com/baeldung/graphql/DemoApplication.java => spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlApplication.java} (61%) rename spring-boot-modules/{spring-boot-libraries => spring-boot-graphql}/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java (74%) create mode 100644 spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Post.java create mode 100644 spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostController.java rename spring-boot-modules/{spring-boot-libraries => spring-boot-graphql}/src/main/java/com/baeldung/graphql/PostDao.java (53%) create mode 100644 spring-boot-modules/spring-boot-graphql/src/main/resources/application.yml rename spring-boot-modules/{spring-boot-libraries => spring-boot-graphql}/src/main/resources/graphql/post.graphqls (80%) create mode 100644 spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/PostControllerIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/SpringContextTest.java rename spring-boot-modules/{spring-boot-libraries => spring-boot-graphql}/src/test/resources/GraphQL collection.postman_collection.json (93%) create mode 100644 spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post.graphql create mode 100644 spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/recent_posts.graphql create mode 100644 spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/recent_posts_expected_response.json delete mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Author.java delete mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/AuthorDao.java delete mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/AuthorResolver.java delete mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Mutation.java delete mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Post.java delete mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/PostResolver.java delete mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Query.java diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index bb97e15822..eaf12d128c 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -41,6 +41,7 @@ spring-boot-environment spring-boot-exceptions spring-boot-flowable + spring-boot-graphql spring-boot-groovy spring-boot-jasypt @@ -101,4 +102,4 @@ - \ No newline at end of file + diff --git a/spring-boot-modules/spring-boot-graphql/GraphQL collection.postman_collection.json b/spring-boot-modules/spring-boot-graphql/GraphQL collection.postman_collection.json new file mode 100644 index 0000000000..8f45c34447 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/GraphQL collection.postman_collection.json @@ -0,0 +1,124 @@ +{ + "info": { + "_postman_id": "bae0e3d0-2b86-46aa-b6df-523497a2296a", + "name": "GraphQL collection", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "mutations", + "item": [ + { + "name": "createPost", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "graphql", + "graphql": { + "query": "mutation createPost ($title: String!, $text: String!, $category: String, $authorId: String!) {\n createPost (title: $title, text: $text, category: $category, authorId: $authorId) {\n id\n title\n text\n category\n }\n}", + "variables": "{\n \"title\": \"new post\",\n \"text\": \"new post text\",\n \"category\": \"category\",\n \"authorId\": \"Author0\"\n}" + } + }, + "url": { + "raw": "http://localhost:8080/graphql", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "graphql" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "queries", + "item": [ + { + "name": "get recent posts", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "graphql", + "graphql": { + "query": "{\r\n recentPosts(count: 10, offset: 0) {\r\n id\r\n title\r\n category\r\n text\r\n author {\r\n id\r\n name\r\n thumbnail\r\n }\r\n }\r\n}", + "variables": "" + } + }, + "url": { + "raw": "http://localhost:8080/graphql", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "graphql" + ] + } + }, + "response": [] + }, + { + "name": "recentPosts - variables", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "graphql", + "graphql": { + "query": "query recentPosts ($count: Int, $offset: Int) {\n recentPosts (count: $count, offset: $offset) {\n id\n title\n text\n category\n }\n}", + "variables": "{\n \"count\": 5,\n \"offset\": 0\n}" + } + }, + "url": { + "raw": "http://localhost:8080/graphql", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "graphql" + ] + } + }, + "response": [] + } + ] + } + ], + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + } + ], + "variable": [ + { + "key": "url", + "value": "", + "type": "string" + } + ] +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-graphql/README.md b/spring-boot-modules/spring-boot-graphql/README.md new file mode 100644 index 0000000000..8223360597 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/README.md @@ -0,0 +1,29 @@ +## Spring Boot Graphql + +This module contains articles about Spring Boot Graphql + +### The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + +### Relevant Articles: + +- [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql) +- [Expose GraphQL Field with Different Name](https://www.baeldung.com/graphql-field-name) + +### GraphQL sample queries + +Query +```shell script +curl \ +--request POST 'localhost:8080/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:8080/graphql' \ +--header 'Content-Type: application/json' \ +--data-raw '{"query":"mutation {\n createPost(title: \"New Title\", authorId: \"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-graphql/pom.xml b/spring-boot-modules/spring-boot-graphql/pom.xml new file mode 100644 index 0000000000..b89bc42d2f --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + spring-boot-graphql + spring-boot-graphql + war + + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-graphql + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.graphql + spring-graphql-test + test + + + + diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Author.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Author.java new file mode 100644 index 0000000000..e4597504af --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Author.java @@ -0,0 +1,11 @@ +package com.baeldung.graphql; + +import lombok.Data; + +@Data +public class Author { + + private String id; + private String name; + private String thumbnail; +} diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorController.java new file mode 100644 index 0000000000..bbc1466f8a --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorController.java @@ -0,0 +1,21 @@ +package com.baeldung.graphql; + +import org.springframework.graphql.data.method.annotation.SchemaMapping; +import org.springframework.stereotype.Controller; + +import java.util.List; + +@Controller +public class AuthorController { + + private final PostDao postDao; + + public AuthorController(PostDao postDao) { + this.postDao = postDao; + } + + @SchemaMapping + public List posts(Author author) { + return postDao.getAuthorPosts(author.getId()); + } +} diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorDao.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorDao.java new file mode 100644 index 0000000000..37946e57dd --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/AuthorDao.java @@ -0,0 +1,18 @@ +package com.baeldung.graphql; + +import java.util.List; + +public class AuthorDao { + private final List authors; + + public AuthorDao(List authors) { + this.authors = authors; + } + + public Author getAuthor(String id) { + return authors.stream() + .filter(author -> id.equals(author.getId())) + .findFirst() + .orElseThrow(RuntimeException::new); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/DemoApplication.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlApplication.java similarity index 61% rename from spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/DemoApplication.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlApplication.java index 1fd93af3b7..34bdeebe7b 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/DemoApplication.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlApplication.java @@ -1,20 +1,16 @@ package com.baeldung.graphql; -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 class GraphqlApplication { public static void main(String[] args) { - System.setProperty("spring.config.name", "demo"); - SpringApplication.run(DemoApplication.class, args); + SpringApplication.run(GraphqlApplication.class, args); } } diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java similarity index 74% rename from spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java index c5ae8bd772..30cb71c43c 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java @@ -1,13 +1,14 @@ package com.baeldung.graphql; -import java.util.ArrayList; -import java.util.List; - import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.ArrayList; +import java.util.List; + @Configuration public class GraphqlConfiguration { + @Bean public PostDao postDao() { List posts = new ArrayList<>(); @@ -16,6 +17,7 @@ public class GraphqlConfiguration { Post post = new Post(); post.setId("Post" + authorId + postId); post.setTitle("Post " + authorId + ":" + postId); + post.setCategory("Post category"); post.setText("Post " + postId + " + by author " + authorId); post.setAuthorId("Author" + authorId); posts.add(post); @@ -36,24 +38,4 @@ public class GraphqlConfiguration { } return new AuthorDao(authors); } - - @Bean - public PostResolver postResolver(AuthorDao authorDao) { - return new PostResolver(authorDao); - } - - @Bean - public AuthorResolver authorResolver(PostDao postDao) { - return new AuthorResolver(postDao); - } - - @Bean - public Query query(PostDao postDao) { - return new Query(postDao); - } - - @Bean - public Mutation mutation(PostDao postDao) { - return new Mutation(postDao); - } } diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Post.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Post.java new file mode 100644 index 0000000000..65f189162a --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/Post.java @@ -0,0 +1,14 @@ +package com.baeldung.graphql; + +import lombok.Data; + +@Data +public class Post { + + private String id; + private String title; + private String text; + private String category; + private String authorId; + +} diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostController.java new file mode 100644 index 0000000000..2df17bf5f6 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostController.java @@ -0,0 +1,52 @@ +package com.baeldung.graphql; + +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.List; +import java.util.UUID; + +@Controller +public class PostController { + + private final PostDao postDao; + private final AuthorDao authorDao; + + public PostController(PostDao postDao, AuthorDao authorDao) { + this.postDao = postDao; + this.authorDao = authorDao; + } + + @QueryMapping + public List recentPosts(@Argument int count, @Argument int offset) { + return postDao.getRecentPosts(count, offset); + } + + @SchemaMapping + public Author author(Post post) { + return authorDao.getAuthor(post.getAuthorId()); + } + + @SchemaMapping(typeName="Post", field="first_author") + public Author getFirstAuthor(Post post) { + return authorDao.getAuthor(post.getAuthorId()); + } + + @MutationMapping + public Post createPost(@Argument String title, @Argument String text, + @Argument String category, @Argument String authorId) { + Post post = new Post(); + post.setId(UUID.randomUUID().toString()); + post.setTitle(title); + post.setText(text); + post.setCategory(category); + post.setAuthorId(authorId); + postDao.savePost(post); + + return post; + } + +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/PostDao.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostDao.java similarity index 53% rename from spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/PostDao.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostDao.java index 0a755a7cf4..a0724efaad 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/PostDao.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphql/PostDao.java @@ -4,21 +4,27 @@ import java.util.List; import java.util.stream.Collectors; public class PostDao { - private List posts; + + private final List posts; public PostDao(List posts) { this.posts = posts; } public List getRecentPosts(int count, int offset) { - return posts.stream().skip(offset).limit(count).collect(Collectors.toList()); + return posts.stream() + .skip(offset) + .limit(count) + .collect(Collectors.toList()); } public List getAuthorPosts(String author) { - return posts.stream().filter(post -> author.equals(post.getAuthorId())).collect(Collectors.toList()); + return posts.stream() + .filter(post -> author.equals(post.getAuthorId())) + .collect(Collectors.toList()); } public void savePost(Post post) { - posts.add(0, post); + posts.add(post); } } diff --git a/spring-boot-modules/spring-boot-graphql/src/main/resources/application.yml b/spring-boot-modules/spring-boot-graphql/src/main/resources/application.yml new file mode 100644 index 0000000000..52586ed567 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/application.yml @@ -0,0 +1,4 @@ +spring: + graphql: + graphiql: + enabled: true diff --git a/spring-boot-modules/spring-boot-libraries/src/main/resources/graphql/post.graphqls b/spring-boot-modules/spring-boot-graphql/src/main/resources/graphql/post.graphqls similarity index 80% rename from spring-boot-modules/spring-boot-libraries/src/main/resources/graphql/post.graphqls rename to spring-boot-modules/spring-boot-graphql/src/main/resources/graphql/post.graphqls index e426f3508f..49beb44891 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/resources/graphql/post.graphqls +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/graphql/post.graphqls @@ -21,5 +21,5 @@ type Query { # The Root Mutation for the application type Mutation { - writePost(title: String!, text: String!, category: String, author: String!) : Post! + createPost(title: String!, text: String!, category: String, authorId: String!) : Post! } diff --git a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/PostControllerIntegrationTest.java b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/PostControllerIntegrationTest.java new file mode 100644 index 0000000000..1cb008b7be --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/PostControllerIntegrationTest.java @@ -0,0 +1,54 @@ +package com.baeldung.graphql; + +import lombok.SneakyThrows; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest; +import org.springframework.context.annotation.Import; +import org.springframework.graphql.test.tester.GraphQlTester; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +@GraphQlTest(PostController.class) +@Import(GraphqlConfiguration.class) +class PostControllerIntegrationTest { + + @Autowired + private GraphQlTester graphQlTester; + + @Test + void givenPosts_whenExecuteQueryForRecentPosts_thenReturnResponse() { + String documentName = "recent_posts"; + + graphQlTester.documentName(documentName) + .variable("count", 2) + .variable("offset", 0) + .execute() + .path("$") + .matchesJson(expected(documentName)); + } + + @Test + void givenNewPostData_whenExecuteMutation_thenNewPostCreated() { + String documentName = "create_post"; + + graphQlTester.documentName(documentName) + .variable("title", "New Post") + .variable("text", "New post text") + .variable("category", "category") + .variable("authorId", "Author0") + .execute() + .path("createPost.id").hasValue() + .path("createPost.title").entity(String.class).isEqualTo("New Post") + .path("createPost.text").entity(String.class).isEqualTo("New post text") + .path("createPost.category").entity(String.class).isEqualTo("category"); + } + + @SneakyThrows + public static String expected(String fileName) { + Path path = Paths.get("src/test/resources/graphql-test/" + fileName + "_expected_response.json"); + return new String(Files.readAllBytes(path)); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/SpringContextTest.java b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/SpringContextTest.java new file mode 100644 index 0000000000..2b8ce5f35d --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/java/com/baeldung/graphql/SpringContextTest.java @@ -0,0 +1,12 @@ +package com.baeldung.graphql; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest(classes = GraphqlApplication.class) +public class SpringContextTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/test/resources/GraphQL collection.postman_collection.json b/spring-boot-modules/spring-boot-graphql/src/test/resources/GraphQL collection.postman_collection.json similarity index 93% rename from spring-boot-modules/spring-boot-libraries/src/test/resources/GraphQL collection.postman_collection.json rename to spring-boot-modules/spring-boot-graphql/src/test/resources/GraphQL collection.postman_collection.json index 8245152bdd..662ae57149 100644 --- a/spring-boot-modules/spring-boot-libraries/src/test/resources/GraphQL collection.postman_collection.json +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/GraphQL collection.postman_collection.json @@ -9,14 +9,14 @@ "name": "mutations", "item": [ { - "name": "writePost", + "name": "createPost", "request": { "method": "POST", "header": [], "body": { "mode": "graphql", "graphql": { - "query": "mutation writePost ($title: String!, $text: String!, $category: String) {\n writePost (title: $title, text: $text, category: $category) {\n id\n title\n text\n category\n }\n}", + "query": "mutation createPost ($title: String!, $text: String!, $category: String) {\n createPost (title: $title, text: $text, category: $category) {\n id\n title\n text\n category\n }\n}", "variables": "{\n \"title\": \"\",\n \"text\": \"\",\n \"category\": \"\"\n}" }, "options": { diff --git a/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post.graphql b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post.graphql new file mode 100644 index 0000000000..fc6497a14c --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/create_post.graphql @@ -0,0 +1,8 @@ +mutation createPost ($title: String!, $text: String!, $category: String, $authorId: String!) { + createPost (title: $title, text: $text, category: $category, authorId: $authorId) { + id + title + text + category + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/recent_posts.graphql b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/recent_posts.graphql new file mode 100644 index 0000000000..802661faf2 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/recent_posts.graphql @@ -0,0 +1,13 @@ +query recentPosts ($count: Int, $offset: Int) { + recentPosts (count: $count, offset: $offset) { + id + title + text + category + author { + id + name + thumbnail + } + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/recent_posts_expected_response.json b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/recent_posts_expected_response.json new file mode 100644 index 0000000000..0f492886ea --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/test/resources/graphql-test/recent_posts_expected_response.json @@ -0,0 +1,28 @@ +{ + "data": { + "recentPosts": [ + { + "id": "Post00", + "title": "Post 0:0", + "category": "Post category", + "text": "Post 0 + by author 0", + "author": { + "id": "Author0", + "name": "Author 0", + "thumbnail": "http://example.com/authors/0" + } + }, + { + "id": "Post10", + "title": "Post 1:0", + "category": "Post category", + "text": "Post 0 + by author 1", + "author": { + "id": "Author1", + "name": "Author 1", + "thumbnail": "http://example.com/authors/1" + } + } + ] + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries/README.md b/spring-boot-modules/spring-boot-libraries/README.md index 68302d6d6f..e115794efe 100644 --- a/spring-boot-modules/spring-boot-libraries/README.md +++ b/spring-boot-modules/spring-boot-libraries/README.md @@ -13,24 +13,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Rate Limiting a Spring API Using Bucket4j](https://www.baeldung.com/spring-bucket4j) - [Spring Boot and Caffeine Cache](https://www.baeldung.com/spring-boot-caffeine-cache) - [Spring Boot and Togglz Aspect](https://www.baeldung.com/spring-togglz) -- [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql) -- [Expose GraphQL Field with Different Name](https://www.baeldung.com/graphql-field-name) -- More articles: [[next -->]](/spring-boot-modules/spring-boot-libraries-2) - -### 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}"}' -``` +- More articles: [[next -->]](../spring-boot-libraries-2) diff --git a/spring-boot-modules/spring-boot-libraries/pom.xml b/spring-boot-modules/spring-boot-libraries/pom.xml index db467a8de5..7ac7043abc 100644 --- a/spring-boot-modules/spring-boot-libraries/pom.xml +++ b/spring-boot-modules/spring-boot-libraries/pom.xml @@ -46,22 +46,6 @@ togglz-spring-security ${togglz.version} - - - com.graphql-java - graphql-spring-boot-starter - ${graphql-spring-boot-starter.version} - - - com.graphql-java - graphql-java-tools - ${graphql-java-tools.version} - - - com.graphql-java - graphiql-spring-boot-starter - ${graphql-spring-boot-starter.version} - org.zalando diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Author.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Author.java deleted file mode 100644 index 11e927c564..0000000000 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Author.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.graphql; - -public class Author { - private String id; - private String name; - private String thumbnail; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getThumbnail() { - return thumbnail; - } - - public void setThumbnail(String thumbnail) { - this.thumbnail = thumbnail; - } -} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/AuthorDao.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/AuthorDao.java deleted file mode 100644 index c799a558a7..0000000000 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/AuthorDao.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.graphql; - -import java.util.List; -import java.util.Optional; - -public class AuthorDao { - private List authors; - - public AuthorDao(List authors) { - this.authors = authors; - } - - public Optional getAuthor(String id) { - return authors.stream().filter(author -> id.equals(author.getId())).findFirst(); - } -} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/AuthorResolver.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/AuthorResolver.java deleted file mode 100644 index 982c6cebc1..0000000000 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/AuthorResolver.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.graphql; - -import java.util.List; - -import com.coxautodev.graphql.tools.GraphQLResolver; - -public class AuthorResolver implements GraphQLResolver { - private PostDao postDao; - - public AuthorResolver(PostDao postDao) { - this.postDao = postDao; - } - - public List getPosts(Author author) { - return postDao.getAuthorPosts(author.getId()); - } -} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Mutation.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Mutation.java deleted file mode 100644 index 0e16e3c8b7..0000000000 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Mutation.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.graphql; - -import java.util.UUID; - -import com.coxautodev.graphql.tools.GraphQLMutationResolver; - -public class Mutation implements GraphQLMutationResolver { - private PostDao postDao; - - public Mutation(PostDao postDao) { - this.postDao = postDao; - } - - public Post writePost(String title, String text, String category, String author) { - Post post = new Post(); - post.setId(UUID.randomUUID().toString()); - post.setTitle(title); - post.setText(text); - post.setCategory(category); - post.setAuthorId(author); - postDao.savePost(post); - - return post; - } -} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Post.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Post.java deleted file mode 100644 index 14d3084841..0000000000 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Post.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.baeldung.graphql; - -public class Post { - private String id; - private String title; - private String text; - private String category; - private String authorId; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public String getCategory() { - return category; - } - - public void setCategory(String category) { - this.category = category; - } - - public String getAuthorId() { - return authorId; - } - - public void setAuthorId(String authorId) { - this.authorId = authorId; - } -} 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 deleted file mode 100644 index d4dff23a29..0000000000 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/PostResolver.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.graphql; - -import com.coxautodev.graphql.tools.GraphQLResolver; - -public class PostResolver implements GraphQLResolver { - private AuthorDao authorDao; - - public PostResolver(AuthorDao authorDao) { - this.authorDao = authorDao; - } - - public Author getAuthor(Post post) { - return authorDao.getAuthor(post.getAuthorId()).orElseThrow(RuntimeException::new); - } - - public Author getFirst_author(Post post) { - return authorDao.getAuthor(post.getAuthorId()).orElseThrow(RuntimeException::new); - } - -} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Query.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Query.java deleted file mode 100644 index 6988cdd37f..0000000000 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/graphql/Query.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.graphql; - -import java.util.List; - -import com.coxautodev.graphql.tools.GraphQLQueryResolver; - -public class Query implements GraphQLQueryResolver { - private PostDao postDao; - - public Query(PostDao postDao) { - this.postDao = postDao; - } - - public List getRecentPosts(int count, int offset) { - return postDao.getRecentPosts(count, offset); - } -}