Skip to content

Commit

Permalink
Example for a GraphQL Application in Spring Boot (#2477)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzer authored and pivovarit committed Aug 21, 2017
1 parent 04fa178 commit 706b3d6
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>3.2.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
Expand Down
31 changes: 31 additions & 0 deletions spring-boot/src/main/java/com/baeldung/graphql/Author.java
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 spring-boot/src/main/java/com/baeldung/graphql/AuthorDao.java
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 spring-boot/src/main/java/com/baeldung/graphql/AuthorResolver.java
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());
}
}
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 spring-boot/src/main/java/com/baeldung/graphql/Mutation.java
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;
}
}
49 changes: 49 additions & 0 deletions spring-boot/src/main/java/com/baeldung/graphql/Post.java
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 spring-boot/src/main/java/com/baeldung/graphql/PostDao.java
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 spring-boot/src/main/java/com/baeldung/graphql/PostResolver.java
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());
}
}
17 changes: 17 additions & 0 deletions spring-boot/src/main/java/com/baeldung/graphql/Query.java
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);
}
}
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions spring-boot/src/main/resources/graphql/blog.graphqls
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!
}

0 comments on commit 706b3d6

Please sign in to comment.