-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example for a GraphQL Application in Spring Boot (#2477)
- Loading branch information
Showing
12 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
spring-boot/src/main/java/com/baeldung/graphql/Author.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
spring-boot/src/main/java/com/baeldung/graphql/AuthorDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.baeldung.graphql; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class AuthorDao { | ||
private List<Author> authors; | ||
|
||
public AuthorDao(List<Author> authors) { | ||
this.authors = authors; | ||
} | ||
|
||
public Optional<Author> getAuthor(String id) { | ||
return authors.stream() | ||
.filter(author -> id.equals(author.getId())) | ||
.findFirst(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
spring-boot/src/main/java/com/baeldung/graphql/AuthorResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.baeldung.graphql; | ||
|
||
import java.util.List; | ||
|
||
import com.coxautodev.graphql.tools.GraphQLResolver; | ||
|
||
public class AuthorResolver implements GraphQLResolver<Author> { | ||
private PostDao postDao; | ||
|
||
public AuthorResolver(PostDao postDao) { | ||
this.postDao = postDao; | ||
} | ||
|
||
public List<Post> getPosts(Author author) { | ||
return postDao.getAuthorPosts(author.getId()); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
spring-boot/src/main/java/com/baeldung/graphql/GraphqlConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Post> 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<Author> 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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
spring-boot/src/main/java/com/baeldung/graphql/Mutation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
spring-boot/src/main/java/com/baeldung/graphql/PostDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.baeldung.graphql; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class PostDao { | ||
private List<Post> posts; | ||
|
||
public PostDao(List<Post> posts) { | ||
this.posts = posts; | ||
} | ||
|
||
public List<Post> getRecentPosts(int count, int offset) { | ||
return posts.stream() | ||
.skip(offset) | ||
.limit(count) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<Post> getAuthorPosts(String author) { | ||
return posts.stream() | ||
.filter(post -> author.equals(post.getAuthorId())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public void savePost(Post post) { | ||
posts.add(0, post); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
spring-boot/src/main/java/com/baeldung/graphql/PostResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.baeldung.graphql; | ||
|
||
import java.util.Optional; | ||
|
||
import com.coxautodev.graphql.tools.GraphQLResolver; | ||
|
||
public class PostResolver implements GraphQLResolver<Post> { | ||
private AuthorDao authorDao; | ||
|
||
public PostResolver(AuthorDao authorDao) { | ||
this.authorDao = authorDao; | ||
} | ||
|
||
public Optional<Author> getAuthor(Post post) { | ||
return authorDao.getAuthor(post.getAuthorId()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Post> recentPosts(int count, int offset) { | ||
return postDao.getRecentPosts(count, offset); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
spring-boot/src/main/java/org/baeldung/boot/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! | ||
} |