GraphQL Java Demo代码

时间:2023-03-09 05:05:48
GraphQL Java Demo代码

mvn 引用GraphQL

<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>11.0</version>
</dependency>

1. 创建数据类

package com.demo.graph;

public class CPU {
private String name;
private String cache; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCache() {
return cache;
}
public void setCache(String cache) {
this.cache = cache;
}
}
package com.demo.graph;

public class Memory {
private String name;
private String size;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
} }
package com.demo.graph;

import java.util.List;

public class Computer {
private String name;
private CPU cpu;
private List<Memory> memoryList; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CPU getCpu() {
return cpu;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public List<Memory> getMemoryList() {
return memoryList;
}
public void setMemoryList(List<Memory> memoryList) {
this.memoryList = memoryList;
}
}

2. 创建测试类

package com.demo.graph;

import java.util.ArrayList;
import java.util.List;
import java.util.Map; import graphql.GraphQL;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject; public class GraphQLDemoTest { public static void main(String[] args) {
CPU cpu = new CPU();
cpu.setName("I7");
cpu.setCache("19M"); Memory m1 = new Memory();
m1.setName("海盗船");
m1.setSize("8G"); Memory m2 = new Memory();
m2.setName("海盗船");
m2.setSize("8G"); List<Memory> memoryList = new ArrayList<Memory>();
memoryList.add(m1);
memoryList.add(m2); Computer computer = new Computer();
computer.setName("组装机");
computer.setCpu(cpu);
computer.setMemoryList(memoryList); // 定义GraphQL类型
GraphQLObjectType cpuType = newObject().name("cpuType")
.field(newFieldDefinition().name("name").type(GraphQLString))
.field(newFieldDefinition().name("cache").type(GraphQLString)).build(); GraphQLObjectType memoryType = newObject().name("memoryType")
.field(newFieldDefinition().name("name").type(GraphQLString))
.field(newFieldDefinition().name("size").type(GraphQLString)).build(); GraphQLObjectType computerType = newObject().name("computerType")
.field(newFieldDefinition().name("name").type(GraphQLString))
.field(newFieldDefinition().name("cpu").type(cpuType))
.field(newFieldDefinition().name("memoryList").type(new GraphQLList(memoryType))).build(); // 关联返回类型与返回数据
GraphQLObjectType queryType = newObject().name("computerQuery")
.field(newFieldDefinition().type(computerType).name("computer").dataFetcher(evn -> {
return computer;
})).build(); GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); Map<String, Object> result = graphQL.execute("{computer{name,cpu{name,cache},memoryList{name,size}}}")
.getData(); // 打印返回结果
System.out.println(result); } }