GraphQL並不會實現關聯查詢,數據關聯須要程序本身實現java
官網首頁有介紹獲取多個資源只須要一個請求,如想獲取用戶信息和身份證信息,原來須要先查用戶信息,再經過用戶id查詢身份證信息,而在GraphQL中一次請求就能夠實現。apache
對於這個觀點我不敢苟同,可能我尚未體會到這種感受,我認爲只要需求明確,多個資源一次請求在RESTFUl中一樣能夠實現。ui
廢話不說了,進入在正題this
以前已經實現了對user對象的查詢操做,如今對user添加一個card屬性,操做user對象時能夠關聯到card信息spa
User.javacode
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.graphqlsblog
#對應的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 }
demoip
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}}}}ci
再次強調,關聯信息是程序控制的,並非GraphQL