我是靠谱客的博主 冷傲板凳,这篇文章主要介绍java 使用GraphQL-关联对象,现在分享给大家,希望可以做个参考。

GraphQL并不会实现关联查询,数据关联需要程序自己实现

官网首页有介绍获取多个资源只需要一个请求,如想获取用户信息和身份证信息,原来需要先查用户信息,再通过用户id查询身份证信息,而在GraphQL中一次请求就可以实现。

对于这个观点我不敢苟同,可能我还没有体会到这种感觉,我认为只要需求明确,多个资源一次请求在RESTFUl中同样可以实现。

 

 

废话不说了,进入在正题

  之前已经实现了对user对象的查询操作,现在对user添加一个card属性,操作user对象时可以关联到card信息

User.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class User { private int age; private long id; private String name; private Card card; public User(int age, long id, String name, Card card) { this.age = age; this.id = id; this.name = name; this.card = card; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } public User(int age, long id, String name) { this.age = age; this.id = id; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

Card.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Card { private String cardNumber; private Long userId; public Card(String cardNumber, Long userId) { this.cardNumber = cardNumber; this.userId = userId; } public String getCardNumber() { return cardNumber; } public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; } public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } }

user.graphqls

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#对应的User定义如下 schema { #定义查询 query: UserQuery } type UserQuery { #定义查询类型 user(id:Long) : User #指定对象以及参数类型 } type User { #定义对象 id: Long! #!表示非空 name:String age:Int card:Card } type Card { cardNumber:String userId:Long }

demo

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import clc.bean.Card; import clc.bean.User; import graphql.ExecutionResult; import graphql.GraphQL; import graphql.schema.GraphQLSchema; import graphql.schema.idl.RuntimeWiring; import graphql.schema.idl.SchemaGenerator; import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; import org.apache.commons.io.IOUtils; /** * ClassName: GraphQLSDLDemo<br/> * Description: <br/> * date: 2019/6/28 11:19 AM<br/> * * @author chengluchao * @since JDK 1.8 */ public class GraphQLSDLDemo2 { public static void main(String[] args) throws Exception { //读取graphqls文件 String fileName = "user.graphqls"; String fileContent = IOUtils.toString(GraphQLSDLDemo2.class.getClassLoader().getResource(fileName), "UTF-8"); //解析文件 TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(fileContent); RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring() .type("UserQuery", builder -> builder.dataFetcher("user", environment -> { Long id = environment.getArgument("id"); Card card = new Card("123456", id); return new User(18, id, "user0" + id, card); }) ) .build(); GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, wiring); GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); String query = "{user(id:15){id,name,age,card{cardNumber,userId}}}"; ExecutionResult result = graphQL.execute(query); System.out.println("query: " + query); System.out.println(result.toSpecification()); } }

query: {user(id:15){id,name,age,card{cardNumber,userId}}}
{data={user={id=15, name=user015, age=18, card={cardNumber=123456, userId=15}}}}

再次强调,关联信息是程序控制的,并不是GraphQL

 

最后

以上就是冷傲板凳最近收集整理的关于java 使用GraphQL-关联对象的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部