2021-01-27 GraphQL入门(一)

GraphQL

    GraphQL既是一种用于API的查询语言也是一个满足你数据查询的运行时。GraphQL对你的API中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让API更容易地随着时间推移而演进,还能用于构建强大的开发工具。

 

启动后界面如上图所示

1.1 添加依赖

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.boot</groupId>
  3.  
    <artifactId>spring-boot-starter</artifactId>
  4.  
    </dependency>
  5.  
     
  6.  
    <dependency>
  7.  
    <groupId>org.springframework.boot</groupId>
  8.  
    <artifactId>spring-boot-starter-web</artifactId>
  9.  
    </dependency>
  10.  
     
  11.  
    <dependency>
  12.  
    <groupId>com.graphql-java</groupId>
  13.  
    <artifactId>graphql-java-tools</artifactId>
  14.  
    <version>5.2.4</version>
  15.  
    </dependency>
  16.  
     
  17.  
    <dependency>
  18.  
    <groupId>com.graphql-java</groupId>
  19.  
    <artifactId>graphql-spring-boot-starter</artifactId>
  20.  
    <version>5.0.2</version>
  21.  
    </dependency>
  22.  
     
  23.  
     
  24.  
    <dependency>
  25.  
    <groupId>com.graphql-java</groupId>
  26.  
    <artifactId>graphiql-spring-boot-starter</artifactId>
  27.  
    <version>5.0.2</version>
  28.  
    </dependency>
  29.  
     
  30.  
    <dependency>
  31.  
    <groupId>org.springframework.boot</groupId>
  32.  
    <artifactId>spring-boot-starter-test</artifactId>
  33.  
    <scope>test</scope>
  34.  
    </dependency>

 

1.2 编写graphqls文件

  1.  
    type Query {
  2.  
    pets: [Pet]
  3.  
    animals: [Animal]
  4.  
    }
  5.  
     
  6.  
    type Pet {
  7.  
    id: Int
  8.  
    type: Animal
  9.  
    name: String
  10.  
    age: Int
  11.  
    }
  12.  
     
  13.  
    enum Animal {
  14.  
    DOG
  15.  
    CAT
  16.  
    BADGER
  17.  
    MAMMOTH
  18.  
    OOOOOO
  19.  
    }

本人用的idea编写graphqls文件,可以安装graphql插件,

配置文件类型

 

1.3 编写实体类

  1.  
    package com.example.demo;
  2.  
     
  3.  
    public class Pet {
  4.  
    private long id;
  5.  
     
  6.  
    private String name;
  7.  
     
  8.  
    private Animal type;
  9.  
     
  10.  
    private int age;
  11.  
     
  12.  
    public Pet(long id, String name, Animal type, int age) {
  13.  
    this.id = id;
  14.  
    this.name = name;
  15.  
    this.type = type;
  16.  
    this.age = age;
  17.  
    }
  18.  
     
  19.  
    public Pet() {
  20.  
     
  21.  
    }
  22.  
     
  23.  
    public long getId() {
  24.  
    return id;
  25.  
    }
  26.  
     
  27.  
    public void setId(long id) {
  28.  
    this.id = id;
  29.  
    }
  30.  
     
  31.  
    public String getName() {
  32.  
    return name;
  33.  
    }
  34.  
     
  35.  
    public void setName(String name) {
  36.  
    this.name = name;
  37.  
    }
  38.  
     
  39.  
    public Animal getType() {
  40.  
    return type;
  41.  
    }
  42.  
     
  43.  
    public void setType(Animal type) {
  44.  
    this.type = type;
  45.  
    }
  46.  
     
  47.  
    public int getAge() {
  48.  
    return age;
  49.  
    }
  50.  
     
  51.  
    public void setAge(int age) {
  52.  
    this.age = age;
  53.  
    }
  54.  
    }
  1.  
    package com.example.demo;
  2.  
     
  3.  
    public enum Animal {
  4.  
    /**
  5.  
    * Animal
  6.  
    */
  7.  
    DOG,
  8.  
    CAT,
  9.  
    BADGER,
  10.  
    MAMMOTH,
  11.  
    OOOOOO
  12.  
    }

1.4 编写查询组件

  1.  
    package com.example.demo;
  2.  
     
  3.  
    import com.coxautodev.graphql.tools.GraphQLQueryResolver;
  4.  
    import org.springframework.stereotype.Component;
  5.  
     
  6.  
    import java.util.ArrayList;
  7.  
    import java.util.List;
  8.  
     
  9.  
    @Component
  10.  
    public class Query implements GraphQLQueryResolver {
  11.  
    public List<Pet> pets() {
  12.  
    List<Pet> pets = new ArrayList<>();
  13.  
     
  14.  
    Pet aPet = new Pet();
  15.  
    aPet.setId(1L);
  16.  
    aPet.setName("Covey's cat");
  17.  
    aPet.setAge(3);
  18.  
    aPet.setType(Animal.CAT);
  19.  
     
  20.  
    pets.add(aPet);
  21.  
     
  22.  
    return pets;
  23.  
    }
  24.  
     
  25.  
    public List<Animal> animals() {
  26.  
    Animal animal = Animal.MAMMOTH;
  27.  
    Animal animal1 = Animal.BADGER;
  28.  
    Animal animal2 = Animal.CAT;
  29.  
    Animal animal3 = Animal.OOOOOO;
  30.  
    Animal animal4 = Animal.DOG;
  31.  
    List<Animal> animalList = new ArrayList<>(4);
  32.  
    animalList.add(animal);
  33.  
    animalList.add(animal1);
  34.  
    animalList.add(animal2);
  35.  
    animalList.add(animal3);
  36.  
    animalList.add(animal4);
  37.  
    return animalList;
  38.  
    }
  39.  
    }

本章节介绍完,后面待续...

参考资料:

https://github.com/graphql-java/graphql-java-spring

https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/

https://graphql.cn/code/#server-libraries

posted @ 2021-02-25 09:59  武汉大D哥  阅读(25)  评论(0编辑  收藏  举报