java 使用GraphQL-关联对象
GraphQL并不会实现关联查询,数据关联需要程序自己实现
官网首页有介绍获取多个资源只需要一个请求,如想获取用户信息和身份证信息,原来需要先查用户信息,再通过用户id查询身份证信息,而在GraphQL中一次请求就可以实现。
对于这个观点我不敢苟同,可能我还没有体会到这种感觉,我认为只要需求明确,多个资源一次请求在RESTFUl中同样可以实现。

废话不说了,进入在正题
之前已经实现了对user对象的查询操作,现在对user添加一个card属性,操作user对象时可以关联到card信息
User.java
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
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
#对应的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
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类锁和对象锁实践和内部私有锁关联
Java类锁和对象锁实践 感谢[jiehao]同学的投稿,投稿可将文章发送到tengfei@ifeve.com 类锁和对象锁是否会冲突?对象锁和私有锁是否会冲突?通过实例来进行说明. 一.相关约定 为 ...
- Java GC机制和对象Finalize方法的一点总结
GC是垃圾收集的意思(Garbage Collection),内存处理是编程人员容易出现问题的地方,忘记或者错误的内存回收会导致程序或系统的不稳定甚至崩溃,Java提供的GC功能可以自动监测对象是否超 ...
- Mybatis之ResultMap一个简短的引论,关联对象
基础部分能够查看我的还有一篇博客http://blog.csdn.net/elim168/article/details/40622491 MyBatis中在查询进行select映射的时候.返回类型能 ...
- Oracle03——游标、异常、存储过程、存储函数、触发器和Java代码访问Oracle对象
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7476717.html 1.游标(光标)Cursor 在写java程序中有集合的概念,那么 ...
- java处理json与对象的转化 递归
整个类是一个case,总结了我在使用java处理json的时候遇到的问题,还有级联关系的对象如何遍历,json和对象之间的转换! 对于对象json转换中遇到的问题我参考了一篇博客,http://blo ...
- MyBatis之ResultMap简介,关联对象
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...
- Java基础 -- 深入理解Java类型信息(Class对象)与反射机制
一 RTTI概念 认识Claa对象之前,先来了解一个概念,RTTI(Run-Time Type Identification)运行时类型识别,对于这个词一直是 C++ 中的概念,至于Java中出现RT ...
- 【Java并发.4】对象的组合
到目前为止,我们已经介绍了关于线程安全与同步的一些基础知识.然而,我们并不希望对每一系内存访问都进行分析以确保程序是线程安全的,而是希望将一些现有的线程安全组件组合为更大规模的组件或程序. 4.1 设 ...
- Java开发各层对象专用名词含义 PO,VO,DAO,BO,DTO,POJO, BYO,Entity,JavaBean,JavaBeans
Java的几种名词(PO,VO,DAO,BO,POJO)解释 PO:persistant object 持久对象.可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中某个表中的一 ...
- Mybatis 关联对象不能输出的解决办法
Mybatis 关联对象不能输出的解决办法 1.如图所示,现在进行查询的时候并没有得到来自另一张表address项 2.我们进行如下配置: (1).在mybatis-config.xml 文件中配置, ...
随机推荐
- 帝国cms更换Ueditor编辑器上传图片加水印
Ueditor安装包,里面有个/php/文件夹,找到Uploader.class.php,这是通用上传类文件找到private function upFile(),这是上传文件的主处理方法,找到122 ...
- 导入eclipse有Unbound classpath variable: 'M2_REPO报错的解决方法
Eclipse maven of the project reported in Unbound classpath variable: 'M2_REPO /**/***/***. jar' But ...
- [转]OpenGL图形渲染管线、VBO、VAO、EBO概念及用例
直接给出原文链接吧 1.OpenGL图形渲染管线.VBO.VAO.EBO概念及用例 2.OpenGL中glVertex.显示列表(glCallList).顶点数组(Vertex array).VBO及 ...
- NLP自然语言处理
转:https://blog.csdn.net/qq_17677907/article/details/86448214 1.有哪些文本表示模型,它们各有什么优缺点? 文本表示模型是研究如何表示文 ...
- Spring整合Redis,并配置Jedis连接池
目录 只言片语 创建redis连接池的配置文件 单机版 spring整合redis(使用JedisPool) 项目中使用示例 集群版 spring整合redis(使用JedisCluster) 项目中 ...
- eclipse配置maven环境 腾讯课堂的(还没试)
下载和基本配置 https://ke.qq.com/webcourse/index.html#cid=434021&term_id=100518216&taid=37765432689 ...
- 如何使APP开机自启动
方案一 将app做成系统应用,直接安装在 system/app 目录下 具体步骤为: 1.在AndroidManifest文件中,添加 android:sharedUserId="andro ...
- [LeetCode] 218. The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [LeetCode] 261. Graph Valid Tree 图是否是树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] 275. H-Index II H指数 II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...