2021-01-27 GraphQL入门(一)
GraphQL
GraphQL既是一种用于API的查询语言也是一个满足你数据查询的运行时。GraphQL对你的API中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让API更容易地随着时间推移而演进,还能用于构建强大的开发工具。
启动后界面如上图所示
1.1 添加依赖
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
-
<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>
-
-
-
<dependency>
-
<groupId>com.graphql-java</groupId>
-
<artifactId>graphiql-spring-boot-starter</artifactId>
-
<version>5.0.2</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
</dependency>
1.2 编写graphqls文件
-
type Query {
-
pets: [Pet]
-
animals: [Animal]
-
}
-
-
type Pet {
-
id: Int
-
type: Animal
-
name: String
-
age: Int
-
}
-
-
enum Animal {
-
DOG
-
CAT
-
BADGER
-
MAMMOTH
-
OOOOOO
-
}
本人用的idea编写graphqls文件,可以安装graphql插件,
配置文件类型
1.3 编写实体类
-
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;
-
}
-
}
-
package com.example.demo;
-
-
public enum Animal {
-
/**
-
* Animal
-
*/
-
DOG,
-
CAT,
-
BADGER,
-
MAMMOTH,
-
OOOOOO
-
}
1.4 编写查询组件
-
package com.example.demo;
-
-
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
-
import org.springframework.stereotype.Component;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
-
public class Query implements GraphQLQueryResolver {
-
public List<Pet> pets() {
-
List<Pet> 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;
-
}
-
}
本章节介绍完,后面待续...
参考资料:
https://github.com/graphql-java/graphql-java-spring
https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/