Skip to content

Commit 706b3d6

Browse files
sazzerpivovarit
authored andcommitted
Example for a GraphQL Application in Spring Boot (eugenp#2477)
1 parent 04fa178 commit 706b3d6

12 files changed

Lines changed: 305 additions & 0 deletions

File tree

‎spring-boot/pom.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@
4040
<artifactId>spring-boot-starter-security</artifactId>
4141
</dependency>
4242

43+
<dependency>
44+
<groupId>com.graphql-java</groupId>
45+
<artifactId>graphql-spring-boot-starter</artifactId>
46+
<version>3.6.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.graphql-java</groupId>
50+
<artifactId>graphiql-spring-boot-starter</artifactId>
51+
<version>3.6.0</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.graphql-java</groupId>
55+
<artifactId>graphql-java-tools</artifactId>
56+
<version>3.2.0</version>
57+
</dependency>
58+
4359
<dependency>
4460
<groupId>org.springframework.boot</groupId>
4561
<artifactId>spring-boot-starter-tomcat</artifactId>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.graphql;
2+
3+
public class Author {
4+
private String id;
5+
private String name;
6+
private String thumbnail;
7+
8+
public String getId() {
9+
return id;
10+
}
11+
12+
public void setId(String id) {
13+
this.id = id;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getThumbnail() {
25+
return thumbnail;
26+
}
27+
28+
public void setThumbnail(String thumbnail) {
29+
this.thumbnail = thumbnail;
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.graphql;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
6+
public class AuthorDao {
7+
private List<Author> authors;
8+
9+
public AuthorDao(List<Author> authors) {
10+
this.authors = authors;
11+
}
12+
13+
public Optional<Author> getAuthor(String id) {
14+
return authors.stream()
15+
.filter(author -> id.equals(author.getId()))
16+
.findFirst();
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.graphql;
2+
3+
import java.util.List;
4+
5+
import com.coxautodev.graphql.tools.GraphQLResolver;
6+
7+
public class AuthorResolver implements GraphQLResolver<Author> {
8+
private PostDao postDao;
9+
10+
public AuthorResolver(PostDao postDao) {
11+
this.postDao = postDao;
12+
}
13+
14+
public List<Post> getPosts(Author author) {
15+
return postDao.getAuthorPosts(author.getId());
16+
}
17+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.graphql;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
public class GraphqlConfiguration {
11+
@Bean
12+
public PostDao postDao() {
13+
List<Post> posts = new ArrayList<>();
14+
for (int postId = 0; postId < 10; ++postId) {
15+
for (int authorId = 0; authorId < 10; ++authorId) {
16+
Post post = new Post();
17+
post.setId("Post" + authorId + postId);
18+
post.setTitle("Post " + authorId + ":" + postId);
19+
post.setText("Post " + postId + " + by author " + authorId);
20+
post.setAuthorId("Author" + authorId);
21+
posts.add(post);
22+
}
23+
}
24+
return new PostDao(posts);
25+
}
26+
27+
@Bean
28+
public AuthorDao authorDao() {
29+
List<Author> authors = new ArrayList<>();
30+
for (int authorId = 0; authorId < 10; ++authorId) {
31+
Author author = new Author();
32+
author.setId("Author" + authorId);
33+
author.setName("Author " + authorId);
34+
author.setThumbnail("http://example.com/authors/" + authorId);
35+
authors.add(author);
36+
}
37+
return new AuthorDao(authors);
38+
}
39+
40+
@Bean
41+
public PostResolver postResolver(AuthorDao authorDao) {
42+
return new PostResolver(authorDao);
43+
}
44+
45+
@Bean
46+
public AuthorResolver authorResolver(PostDao postDao) {
47+
return new AuthorResolver(postDao);
48+
}
49+
50+
@Bean
51+
public Query query(PostDao postDao) {
52+
return new Query(postDao);
53+
}
54+
55+
@Bean
56+
public Mutation mutation(PostDao postDao) {
57+
return new Mutation(postDao);
58+
}
59+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.graphql;
2+
3+
import java.util.UUID;
4+
5+
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
6+
7+
public class Mutation implements GraphQLMutationResolver {
8+
private PostDao postDao;
9+
10+
public Mutation(PostDao postDao) {
11+
this.postDao = postDao;
12+
}
13+
14+
public Post writePost(String title, String text, String category, String author) {
15+
Post post = new Post();
16+
post.setId(UUID.randomUUID().toString());
17+
post.setTitle(title);
18+
post.setText(text);
19+
post.setCategory(category);
20+
post.setAuthorId(author);
21+
postDao.savePost(post);
22+
23+
return post;
24+
}
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.graphql;
2+
3+
public class Post {
4+
private String id;
5+
private String title;
6+
private String text;
7+
private String category;
8+
private String authorId;
9+
10+
public String getId() {
11+
return id;
12+
}
13+
14+
public void setId(String id) {
15+
this.id = id;
16+
}
17+
18+
public String getTitle() {
19+
return title;
20+
}
21+
22+
public void setTitle(String title) {
23+
this.title = title;
24+
}
25+
26+
public String getText() {
27+
return text;
28+
}
29+
30+
public void setText(String text) {
31+
this.text = text;
32+
}
33+
34+
public String getCategory() {
35+
return category;
36+
}
37+
38+
public void setCategory(String category) {
39+
this.category = category;
40+
}
41+
42+
public String getAuthorId() {
43+
return authorId;
44+
}
45+
46+
public void setAuthorId(String authorId) {
47+
this.authorId = authorId;
48+
}
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.graphql;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class PostDao {
7+
private List<Post> posts;
8+
9+
public PostDao(List<Post> posts) {
10+
this.posts = posts;
11+
}
12+
13+
public List<Post> getRecentPosts(int count, int offset) {
14+
return posts.stream()
15+
.skip(offset)
16+
.limit(count)
17+
.collect(Collectors.toList());
18+
}
19+
20+
public List<Post> getAuthorPosts(String author) {
21+
return posts.stream()
22+
.filter(post -> author.equals(post.getAuthorId()))
23+
.collect(Collectors.toList());
24+
}
25+
26+
public void savePost(Post post) {
27+
posts.add(0, post);
28+
}
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.graphql;
2+
3+
import java.util.Optional;
4+
5+
import com.coxautodev.graphql.tools.GraphQLResolver;
6+
7+
public class PostResolver implements GraphQLResolver<Post> {
8+
private AuthorDao authorDao;
9+
10+
public PostResolver(AuthorDao authorDao) {
11+
this.authorDao = authorDao;
12+
}
13+
14+
public Optional<Author> getAuthor(Post post) {
15+
return authorDao.getAuthor(post.getAuthorId());
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.graphql;
2+
3+
import java.util.List;
4+
5+
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
6+
7+
public class Query implements GraphQLQueryResolver {
8+
private PostDao postDao;
9+
10+
public Query(PostDao postDao) {
11+
this.postDao = postDao;
12+
}
13+
14+
public List<Post> recentPosts(int count, int offset) {
15+
return postDao.getRecentPosts(count, offset);
16+
}
17+
}

0 commit comments

Comments
 (0)