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;

@Component
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/

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值