diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 0cf7df86cd..9d44de64a3 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -40,6 +40,22 @@ spring-boot-starter-security + + com.graphql-java + graphql-spring-boot-starter + 3.6.0 + + + com.graphql-java + graphiql-spring-boot-starter + 3.6.0 + + + com.graphql-java + graphql-java-tools + 3.2.0 + + org.springframework.boot spring-boot-starter-tomcat diff --git a/spring-boot/src/main/java/com/baeldung/graphql/Author.java b/spring-boot/src/main/java/com/baeldung/graphql/Author.java new file mode 100644 index 0000000000..11e927c564 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/graphql/Author.java @@ -0,0 +1,31 @@ +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/src/main/java/com/baeldung/graphql/AuthorDao.java b/spring-boot/src/main/java/com/baeldung/graphql/AuthorDao.java new file mode 100644 index 0000000000..522732faeb --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/graphql/AuthorDao.java @@ -0,0 +1,18 @@ +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/src/main/java/com/baeldung/graphql/AuthorResolver.java b/spring-boot/src/main/java/com/baeldung/graphql/AuthorResolver.java new file mode 100644 index 0000000000..982c6cebc1 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/graphql/AuthorResolver.java @@ -0,0 +1,17 @@ +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/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java b/spring-boot/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java new file mode 100644 index 0000000000..a7a864cf96 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java @@ -0,0 +1,59 @@ +package com.baeldung.graphql; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class GraphqlConfiguration { + @Bean + public PostDao postDao() { + List posts = new ArrayList<>(); + for (int postId = 0; postId < 10; ++postId) { + for (int authorId = 0; authorId < 10; ++authorId) { + Post post = new Post(); + post.setId("Post" + authorId + postId); + post.setTitle("Post " + authorId + ":" + postId); + post.setText("Post " + postId + " + by author " + authorId); + post.setAuthorId("Author" + authorId); + posts.add(post); + } + } + return new PostDao(posts); + } + + @Bean + public AuthorDao authorDao() { + List authors = new ArrayList<>(); + for (int authorId = 0; authorId < 10; ++authorId) { + Author author = new Author(); + author.setId("Author" + authorId); + author.setName("Author " + authorId); + author.setThumbnail("http://example.com/authors/" + authorId); + authors.add(author); + } + 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/src/main/java/com/baeldung/graphql/Mutation.java b/spring-boot/src/main/java/com/baeldung/graphql/Mutation.java new file mode 100644 index 0000000000..0e16e3c8b7 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/graphql/Mutation.java @@ -0,0 +1,25 @@ +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/src/main/java/com/baeldung/graphql/Post.java b/spring-boot/src/main/java/com/baeldung/graphql/Post.java new file mode 100644 index 0000000000..14d3084841 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/graphql/Post.java @@ -0,0 +1,49 @@ +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/src/main/java/com/baeldung/graphql/PostDao.java b/spring-boot/src/main/java/com/baeldung/graphql/PostDao.java new file mode 100644 index 0000000000..f8d243ee3a --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/graphql/PostDao.java @@ -0,0 +1,29 @@ +package com.baeldung.graphql; + +import java.util.List; +import java.util.stream.Collectors; + +public class PostDao { + private 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()); + } + + public List getAuthorPosts(String author) { + return posts.stream() + .filter(post -> author.equals(post.getAuthorId())) + .collect(Collectors.toList()); + } + + public void savePost(Post post) { + posts.add(0, post); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/graphql/PostResolver.java b/spring-boot/src/main/java/com/baeldung/graphql/PostResolver.java new file mode 100644 index 0000000000..dbfde330ea --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/graphql/PostResolver.java @@ -0,0 +1,17 @@ +package com.baeldung.graphql; + +import java.util.Optional; + +import com.coxautodev.graphql.tools.GraphQLResolver; + +public class PostResolver implements GraphQLResolver { + private AuthorDao authorDao; + + public PostResolver(AuthorDao authorDao) { + this.authorDao = authorDao; + } + + public Optional getAuthor(Post post) { + return authorDao.getAuthor(post.getAuthorId()); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/graphql/Query.java b/spring-boot/src/main/java/com/baeldung/graphql/Query.java new file mode 100644 index 0000000000..7bb625798c --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/graphql/Query.java @@ -0,0 +1,17 @@ +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 recentPosts(int count, int offset) { + return postDao.getRecentPosts(count, offset); + } +} diff --git a/spring-boot/src/main/java/org/baeldung/boot/DemoApplication.java b/spring-boot/src/main/java/org/baeldung/boot/DemoApplication.java index 2d83b650ec..5de4134739 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/DemoApplication.java +++ b/spring-boot/src/main/java/org/baeldung/boot/DemoApplication.java @@ -1,11 +1,14 @@ package org.baeldung.boot; +import com.baeldung.graphql.GraphqlConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.baeldung.autoconfiguration.MySQLAutoconfiguration; +import org.springframework.context.annotation.Import; @SpringBootApplication(exclude=MySQLAutoconfiguration.class) +@Import(GraphqlConfiguration.class) public class DemoApplication { public static void main(String[] args) { diff --git a/spring-boot/src/main/resources/graphql/blog.graphqls b/spring-boot/src/main/resources/graphql/blog.graphqls new file mode 100644 index 0000000000..aa0c8757e9 --- /dev/null +++ b/spring-boot/src/main/resources/graphql/blog.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! +}