1. 程式人生 > >GraphQL Java Demo程式碼

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);

    }

}