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