Skip to content

Commit f8cf81e

Browse files
author
ext_zhongling
committed
refactor: Spring Boot整合 GraphQL新式API风格项目案例(重命名实体类User.java)
1 parent 47d40aa commit f8cf81e

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

springboot-graphql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ graphql.servlet.mapping=/graphql
206206
graphql.servlet.enabled=true
207207
```
208208
> 配置完端口和端口点我们就可以对我们编写的 graphql 接口进行测试了。<br/>
209-
> 启动 Spring Boot 服务,测试书写query语句的接口地址为:http://localhost:8080/graphql
209+
> 启动 Spring Boot 服务,测试书写query语句的接口地址为:[http://localhost:8080/graphql](http://localhost:8080/graphql)
210210
211211

212212
# 延伸阅读

springboot-graphql/src/main/java/edu/study/module/graphql/springbootgraphql/entity/Post.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Post {
1212
private String title ;
1313
private String text;
1414
private String category;
15-
private User user;
15+
private UserInfo user;
1616

1717
public Post() {
1818

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
* @create 2021-04-26 17:38
1010
*/
1111
@Data
12-
public class User {
12+
public class UserInfo {
1313

1414
private int userId;
1515
private String userName;
1616
private String realName;
1717
private String email;
1818
private List<Post> posts;
1919

20-
public User() {
20+
public UserInfo() {
2121
}
2222

23-
public User(int userId, String userName, String realName, String email) {
23+
public UserInfo(int userId, String userName, String realName, String email) {
2424
this.userId = userId;
2525
this.userName = userName;
2626
this.realName = realName;

springboot-graphql/src/main/java/edu/study/module/graphql/springbootgraphql/service/PostService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
44
import edu.study.module.graphql.springbootgraphql.entity.Post;
5-
import edu.study.module.graphql.springbootgraphql.entity.User;
5+
import edu.study.module.graphql.springbootgraphql.entity.UserInfo;
66
import org.springframework.stereotype.Service;
77

88
/**
@@ -16,7 +16,7 @@ public class PostService implements GraphQLQueryResolver {
1616
*/
1717
public Post getPostById(int id) {
1818
if (id == 1) {
19-
User user = new User(1, "javadaily", "JAVA日知录", "zhangsan@qq.com");
19+
UserInfo user = new UserInfo(1, "javadaily", "JAVA日知录", "zhangsan@qq.com");
2020
Post post = new Post(1, "Hello,Graphql", "Graphql初体验", "日记");
2121
post.setUser(user);
2222
return post;

springboot-graphql/src/main/java/edu/study/module/graphql/springbootgraphql/service/UserService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
44
import com.google.common.collect.Lists;
55
import edu.study.module.graphql.springbootgraphql.entity.Post;
6-
import edu.study.module.graphql.springbootgraphql.entity.User;
6+
import edu.study.module.graphql.springbootgraphql.entity.UserInfo;
77
import org.springframework.stereotype.Service;
88

99
import javax.annotation.PostConstruct;
@@ -15,9 +15,9 @@
1515
*/
1616
@Service
1717
public class UserService implements GraphQLQueryResolver {
18-
List<User> userList = Lists.newArrayList();
18+
List<UserInfo> userList = Lists.newArrayList();
1919

20-
public User getUserById(int id) {
20+
public UserInfo getUserById(int id) {
2121
return userList.stream().filter(item -> item.getUserId() == id).findAny().orElse(null);
2222
}
2323

@@ -29,8 +29,8 @@ public void initUsers() {
2929
Post post3 = new Post(3, "Hello,Graphql3", "Graphql初体验3", "日记");
3030
List<Post> posts = Lists.newArrayList(post1, post2, post3);
3131

32-
User user1 = new User(1, "zhangsan", "张三", "zhangsan@qq.com");
33-
User user2 = new User(2, "lisi", "李四", "lisi@qq.com");
32+
UserInfo user1 = new UserInfo(1, "zhangsan", "张三", "zhangsan@qq.com");
33+
UserInfo user2 = new UserInfo(2, "lisi", "李四", "lisi@qq.com");
3434

3535
user1.setPosts(posts);
3636
user2.setPosts(posts);

springboot-graphql/src/main/resources/schema/schema.graphqls

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ schema {
44

55
type Query {
66
# 获取具体的用户
7-
getUserById(id:Int) : User
7+
getUserById(id:Int) : UserInfo
88
# 获取具体的博客
99
getPostById(id:Int) : Post
1010
}
1111

12-
type User {
12+
type UserInfo {
1313
userId : ID!,
1414
userName : String,
1515
realName : String,
@@ -22,5 +22,5 @@ type Post {
2222
title : String!,
2323
text : String,
2424
category: String
25-
user: User,
25+
user: UserInfo,
2626
}

0 commit comments

Comments
 (0)