graphql 多个graphqls文件_如何使用 SpringBoot 快速搭建 GraphQL 服务

本文介绍了如何使用SpringBoot结合graphqls文件快速搭建GraphQL服务。通过IntelliJ IDEA创建SpringBoot项目,添加相关依赖,创建graphqls文件定义API,建立数据实体和解析器,最终在graphiql工具中测试并获取响应数据。
摘要由CSDN通过智能技术生成

至于 GraphQL 的简介,大家可以自行搜索, 这里不再赘述。

我们直接开始!

本文使用的开发工具:

IntelliJ IDEA Ultimate

  1. 新建 SpringBoot 项目: 点击 New -> Project -> Spring Initializer
  2. 选择依赖 Spring Web Starter

65d0012add7b6680863da9f7914d1eca.png

3. 打开pom.xml 文件 添加如下 Maven 依赖

<dependencies>
        <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>
    </dependencies>

4. 直接启动项目, 在浏览器输入 http://localhost:8080/graphiql

如出现以下画面,则说明启动成功,此网页工具可用来测试 GraphQL 语句

36d36b2312056d7330d1bb5f9132ad56.png

但现在我们还不能做任何事,因为项目还是空的

5. 接下来,在 resources 文件夹下新建一个 petshop.graphqls 文件

f3b6e6c62b027f065f59d3f85c7ada45.png

6. 在 petshop.graphqls 文件中添加如下内容,用于声明 API 可接受与返回的参数

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

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

enum Animal {
    DOG
    CAT
    BADGER
    MAMMOTH
    OOOOOO
}

6. 创建一个 entities 包,在包中新建一个 Pet 类

cb17c03e93a2a75b2c55e9f7efa27df7.png

Pet 类内容如下:

package com.example.graphql.entities;

import com.example.graphql.enums.Animal;

/**
 * @author Covey Liu, covey@liukedun.com
 * Date: 8/27/2019 2:15 PM
 */
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;
    }
}

7. 创建eums包,在包中新建Animal类

Animal类内容如下

package com.example.graphql.enums;

/**
 * @author Covey Liu, covey@liukedun.com
 * Date: 8/27/2019 2:09 PM
 */

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

8. 创建 resolvers包,在包中新建 Query类, 用于返回请求数据

Query类内容如下:

package com.example.graphql.resolvers;

import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import com.example.graphql.entities.Pet;
import com.example.graphql.enums.Animal;
import org.springframework.stereotype.Component;

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

/**
 * @author Covey Liu, covey@liukedun.com
 * Date: 8/27/2019 2:18 PM
 */

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

9. 确认结构如下图所示,重启应用

1fa8c6ec063b252eb11ccb01570c49f4.png

10. 成功运行后打开浏览器 http://localhost:8080/graphiql

在左侧输入以下请求:(可自行删改),点击运行按钮

{
  pets {
      name
      type
      age
  }
  animals
}

11. 根据请求格式,成功响应数据

339de864fb396e99a6a2ed3fab461cec.png

12. 尝试改变请求格式,获得不同数据,比如我只想获得宠物的名字

{
  pets {
    name
  }
}

38dba36c2ae00e094453c54acc80dad8.png
Spring Boot GraphQL结合了Spring框架和GraphQL查询语言,允许你在微服务架构中轻松构建API。为了实现在`.graphql`文件更新后无需停机即可动态刷新Schema,通常你可以通过以下步骤: 1. 使用Spring GraphQL库:Spring Boot提供了一个方便的起步点,`spring-boot-starter-data-gremlin`或`spring-boot-starter-data-neo4j`等,它们都支持GraphQL。 2. 配置Schema:在`application.yml`或`application.properties`中配置GraphQL的端点和Schema路径,比如指定`graphql-endpoint`和`graphql-schema-path`。 ```yaml spring: graphql: path: /graphql schema: # 这里可以设置为静态Schema或者动态扫描目录 type: SPARQL or SCHEMA_GRAPHQL location: classpath:/graphql/schema.graphqls ``` 3. 动态Schema(如果需要):如果你想要在代码运行时动态生成Schema,可以选择`type: SCHEMA_GRAPHQL`模式,并在启动时扫描指定的目录,如`classpath:/graphql`。当`.graphql`文件更改时,你可以实现监听文件变化并更新Schema。 - 可以使用第三方库如`graphql-code-generator`,它会监听文件系统变化并在更改时自动生成新的Schema类。 4. 实现热部署:如果你使用的是像Spring Cloud这样的微服务环境,可以考虑集成一些热部署工具,如JRebel或Hazelcast Jet,它们能实时监控文件变动,然后触发应用重启或部分更新,而不是完全停止服务。 **相关问题--:** 1. Spring Boot GraphQL如何保证Schema的安全性? 2. 是否可以直接修改生产环境中`.graphql`文件而不会影响服务? 3. 如何避免频繁的重启对业务的影响?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值