GraphQL is both a query language for apis and a runtime for your data queries. GraphQL provides an easy-to-understand set of complete descriptions of the data in your API, enabling a client to get exactly what it needs without any redundancy, making it easier for the API to evolve over time, and for building powerful development tools.

After startup, the interface is shown in the figure above

1.1 Adding dependency org.springFramework. boot spring-boot-starter

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> < the dependency > < groupId > com. Graphql - Java < / groupId > < artifactId > graphql - Java - tools < / artifactId > < version > 5.2.4 < / version > </dependency> <dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-spring-boot-starter</artifactId> < version > 5.0.2 < / version > < / dependency > < the dependency > < groupId > com. Graphql - Java < / groupId > < artifactId > graphiql - spring - the boot - starter < / artifactId > < version > 5.0.2 < / version > < / dependency > < the dependency > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>Copy the code

Type Query {pets: [Pet] animals: [Animal]}

type Pet { id: Int type: Animal name: String age: Int }

Enum Animal {DOG CAT BADGER MAMMOTH OOOOOO}

Configuration file Type

1.3 Write the entity class Package com.example.demo;

public class Pet { private long id;

private String name;

private Animal type;

private int age;

public Pet(long id, String name, Animal type, int age) {
    this.id = id;
    this.name = name;
    this.type = type;
    this.age = age;
}

public Pet() {

}

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

public Animal getType() {
    return type;
}

public void setType(Animal type) {
    this.type = type;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
Copy the code

} package com.example.demo;

Public enum Animal {/** * Animal */ DOG, CAT, BADGER, MAMMOTH, OOOOOO}

import com.coxautodev.graphql.tools.GraphQLQueryResolver; import org.springframework.stereotype.Component;

import java.util.ArrayList; import java.util.List;

@Component public class Query implements GraphQLQueryResolver { public List pets() { List pets = new ArrayList<>();

    Pet aPet = new Pet();
    aPet.setId(1L);
    aPet.setName("Covey's cat");
    aPet.setAge(3);
    aPet.setType(Animal.CAT);

    pets.add(aPet);

    return pets;
}

public List<Animal> animals() {
    Animal animal = Animal.MAMMOTH;
    Animal animal1 = Animal.BADGER;
    Animal animal2 = Animal.CAT;
    Animal animal3 = Animal.OOOOOO;
    Animal animal4 = Animal.DOG;
    List<Animal> animalList = new ArrayList<>(4);
    animalList.add(animal);
    animalList.add(animal1);
    animalList.add(animal2);
    animalList.add(animal3);
    animalList.add(animal4);
    return animalList;
}
Copy the code

} After the introduction of this chapter, to be continued…

References:

Github.com/graphql-jav…

www.graphql-java.com/tutorials/g…

Graphql. Cn/code / # serve…