[JAVA-14663] Update Graphql code for Boot 2.7.x changes and move to new module (#12729)
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Author {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private String thumbnail;
|
||||
}
|
||||
+21
@@ -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<Post> posts(Author author) {
|
||||
return postDao.getAuthorPosts(author.getId());
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AuthorDao {
|
||||
private final List<Author> authors;
|
||||
|
||||
public AuthorDao(List<Author> authors) {
|
||||
this.authors = authors;
|
||||
}
|
||||
|
||||
public Author getAuthor(String id) {
|
||||
return authors.stream()
|
||||
.filter(author -> id.equals(author.getId()))
|
||||
.findFirst()
|
||||
.orElseThrow(RuntimeException::new);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
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;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})
|
||||
public class GraphqlApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GraphqlApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
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<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.setCategory("Post category");
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
+52
@@ -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<Post> 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;
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.graphql;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PostDao {
|
||||
|
||||
private final 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(post);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
spring:
|
||||
graphql:
|
||||
graphiql:
|
||||
enabled: true
|
||||
@@ -0,0 +1,25 @@
|
||||
type Post {
|
||||
id: ID!
|
||||
title: String!
|
||||
text: String!
|
||||
category: String
|
||||
author: Author!
|
||||
first_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 {
|
||||
createPost(title: String!, text: String!, category: String, authorId: String!) : Post!
|
||||
}
|
||||
Reference in New Issue
Block a user