Java:GraphQL 极佳入门实例

来源:互联网 发布:华南理工大学绩点算法 编辑:程序博客网 时间:2024/06/07 01:32

学习一种新知识总是希望有个很简单但是有很全面的小demo。

GraphQL官方文档:http://graphql.org/learn/
这里就不介绍它是用来干什么的了。

创建实体类

public class Person {  private int id;  private String name;  private String password;  private List<Dog> dogs;}public class Dog {  private int id;  private String name;  private int age;}

创建数据源

[  {    "id": 1,    "name": "Irene",    "password": "123456",    "dogs": [      {        "id": 1,        "name": "hello",        "age": 5      },      {        "id": 2,        "name": "world",        "age": 6      }    ]  },  {    "id": 2,    "name": "Aiden",    "password": "123456",    "dogs": [      {        "id": 3,        "name": "hello",        "age": 5      },      {        "id": 4,        "name": "world",        "age": 6      }    ]  }]

这里直接上运行代码了。

package com;import static graphql.Scalars.GraphQLInt;import static graphql.Scalars.GraphQLString;import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;import static graphql.schema.GraphQLObjectType.newObject;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import graphql.GraphQL;import graphql.schema.DataFetcher;import graphql.schema.DataFetchingEnvironment;import graphql.schema.GraphQLArgument;import graphql.schema.GraphQLFieldDefinition;import graphql.schema.GraphQLList;import graphql.schema.GraphQLObjectType;import graphql.schema.GraphQLSchema;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;/** * @author yemingfeng */public class MainClass {  private static List<Person> persons;  public static void main(String[] args) throws IOException {    init();    GraphQLObjectType dogType = newObject()        .name("dog")        .field(newFieldDefinition()            .name("id")            .type(GraphQLInt))        .field(newFieldDefinition()            .name("name")            .type(GraphQLString))        .field(newFieldDefinition()            .name("age")            .type(GraphQLInt))        .build();    GraphQLObjectType personType = newObject()        .name("person")        .field(newFieldDefinition()            .name("id")            .type(GraphQLInt))        .field(newFieldDefinition()            .name("name")            .type(GraphQLString))        .field(newFieldDefinition()            .name("password")            .type(GraphQLString))        .field(newFieldDefinition()            .name("dogs")            .type(new GraphQLList(dogType)))        .build();    GraphQLFieldDefinition personDefinition =        GraphQLFieldDefinition.newFieldDefinition()            .name("person")            .type(personType)            .argument(GraphQLArgument.newArgument().name("id").type(GraphQLInt))            .dataFetcher(new DataFetcher() {              public Object get(DataFetchingEnvironment dataFetchingEnvironment) {                int id = dataFetchingEnvironment.getArgument("id");                for (Person person : persons) {                  if (person.getId() == id) {                    return person;                  }                }                return null;              }            })            .build();    GraphQLSchema schema = GraphQLSchema.newSchema()        .query(newObject()            .name("personQuery")            .field(personDefinition)            .build())        .build();    GraphQL graphQL = GraphQL.newGraphQL(schema).build();    System.out.println(graphQL.execute("{person(id:1){id,name,dogs{id}}}").getData());    System.out.println(graphQL.execute("{person(id:2){id,name,dogs{id,name}}}").getData());    System.out.println(graphQL.execute("{person(id:3){id,name,dogs{id,name,age}}}").getData());  }  private static void init() throws IOException {    InputStream inputStream = MainClass.class.getClassLoader().getResourceAsStream("person.json");    byte[] bytes = new byte[1024];    int length;    StringBuilder stringBuilder = new StringBuilder();    while ((length = inputStream.read(bytes)) != -1) {      stringBuilder.append(new String(bytes, 0, length));    }    persons = new Gson()        .fromJson(stringBuilder.toString(),            new TypeToken<ArrayList<Person>>() {            }.getType()        );  }}

运行结果:

{person={id=1, name=Irene, dogs=[{id=1}, {id=2}]}}{person={id=2, name=Aiden, dogs=[{id=3, name=hello}, {id=4, name=world}]}}{person=null}
原创粉丝点击