GraphQL -- GraphQL入门,与SpringBoot整合,并用Postman测试接口

什么是GraphQL

简介

GraphQL是一种用于 API 的查询语言,也是一个基于服务端的运行引擎。GraphQL 提供了一套完整的规范和描述用于查询 API,服务端能够根据客户端的需要自动适配并返回准确的数据。

优势
  • 在目前流行的restful架构下,服务端返回的接口数据大多由后端开发所定义,而不是由前端来提出,比如对于同一个用户信息,有时后端接口返回来的数据中,仅有部分是前端所需要的,这是其他返回的数据便是冗余的。如果为了解决冗余的问题,编写对应返回数据的接口,又会使得接口数量变得庞大臃肿。
  • 而使用GraphQL可以使开发者可以自定义数据模型、查询规范和查询参数,并在一个请求中指定需要返回的数据以准确获取所有想要的数据。

spring boot整合GraphQL

下面创建一个简单的spring boot项目整合GraphQL,做简单的演示
  1. 创建一个mavan项目

  2. 在pom文件中导入GraphQL的依赖,

        <dependencies>
        <!--graphql-spring-boot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-graphql</artifactId>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphiql-spring-boot-starter</artifactId>
            <version>5.0.2</version>
        </dependency>
        <!-- spring-boot web服务的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
  3. 在application.yaml文件中添加GraphQL相关的配置

     server:
       port: 8889
     spring:
       graphql:
         graphiql:
         enabled: true
         path: /graphql
       schema:
         locations: classpath:graphql/
    
  4. 接口代码编写

    • TestController
    • 此处有两个注解,@QueryMapping,@MutationMapping分别对应graphQL所支持的数据操作query(查询:支持对数据的基本查询)与mutation(变更:支持对数据的增删改操作)
     @Controller
     public class TestController {
       /**
        * 经典 hollo graphql
        */
       @QueryMapping
       public String hello(){
         return "hello graphql";
       }
       /**
        * 查询学生列表
        */
       @QueryMapping
       public List<Student> getStudent(@Argument("flag") String flag){
         //此处的业务操作就不写了,直接返回Student对象集合
         ArrayList<Student> list = new ArrayList<>();
         list.add(new Student(1,"zmx"));
         list.add(new Student(2,"cyn"));
         return list;
       } 
       /**
        * 修改学生信息
        * @param Student
        * @return
        */
       @MutationMapping
       public String updateStudent(@Argument StudentDTO Student){//@Argument 注解可以指定参数名
         //执行修改逻辑,这里就不写了
         return "success!";
       }
    }
    
    • Student实体类与StudentDTO实体类
     @Data
     @AllArgsConstructor
     @NoArgsConstructor
     public class Student {
       private int id;
       private String name;
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class StudentDTO {
       private int id;
       private String name;
    }
    
    • 启动类
       @ComponentScan("org.example.graphql")
       @SpringBootApplication()
       public class PartApplication {
       public static void main(String[] args) {
          //如果启动类启动失败,可以输出异常信息,查看原因
          try {
             SpringApplication.run(PartApplication.class, args);
          } catch (Exception e) {
             System.out.println(e.toString());
          }
       }
       /**
        * 注入WebGraphQlInterceptor Bean
        * @return
        */
        @Bean
        public WebGraphQlInterceptor interceptor() {
           return (webInput, interceptorChain) ->
               // Switch threads to prove ThreadLocal context propagation works
               interceptorChain
                       .next(webInput)
                       .timeout(Duration.ofMinutes(5),
                               Mono.error(new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE)));
        }
      }
    
    
  5. 在resource文件夹下创建graphql文件夹(与配置文件中的schma-locations对应),在其中编写.graphqls文件

    • 编写.graphqls文件前,我们可以在使用idea安装一个GraphQL插件
      插件
    • 安装后新建文件时,便可以选择.graphqls文件
      新建GraphQL File
    • 在idea下方工具栏也会出现GraphQL栏,可以检查我们书写的GraphQL语法是否正确
      插件
    • 好了,回归正题,我们来编写.graphqls文件,使用到的schema语法可以参考链接: GraphQL官网 ,根据Controller层的接口,写出的代码如下
      • 代码目录
        目录

      • root.graphqls的内容

         schema {
           query: Query
           mutation: Mutation
         }
         type Mutation{
         }
         type Query{
         }
        
        
      • test.graphqls的内容

         extend type Query{
           hello:String
           getStudent(flag:String):[Student]
         }
         extend type Mutation {
           updateStudent(studentDto:StudentDTO):String
         }
         type Student{
           id:Int
           name:String
         }
         input StudentDTO{
           id:Int
           name:String
         }
        
        
  6. 到此代码,测试编写完成,可以启动项目测试一下接口了。

网页工具测试
  • 项目启动后,我们可以返回GraphiQL网站查看自己的GraphQL文档,并调试
  • 访问地址:http://localhost:8889/graphiql 端口号为启动服务的端口号,我这里是8889

GraphiQL
如图所示,我们页面左侧编写GraphQL请求,中间返回请求得到的数据,最后侧我们可以看到自己编写的GraphQL文档。

  • 试着访问刚才写的查询接口,点击左上方的三角形按钮即可运行
      {
        getStudent(flag: "1") {
          id
          name
        }
        hello
      }
    
    • 返回结果
      在这里插入图片描述
    • 我们可以发现,一个请求我们同时调用了getStudent(String flag)和hello()两个接口,并且同时得到了它们的返回数据。并且如果你想要在getStudent返回的时候,只返回id的列表,那么你在查询时可以不写name字段,这样返回的结果便不会携带name(即实现了按要求返回)
      	{
           getStudent(flag: "1") {
             id
           }
           hello
         }
      
    • 返回结果
      在这里插入图片描述
  • 再试一下修改接口
     mutation{
       updateStudent(studentDto:{
         id:1
      	 name:"zmx"}
       )
      }
    
    • 返回结果
      在这里插入图片描述
postman测试
  • 除了可以用GraphiQL网页进行测试之外,我们还可以使用PostMan进行测试
    • 查询
      在这里插入图片描述

    • 修改
      在这里插入图片描述

  • 注意要点:
    • 请求为POST请求
    • Url为 http://localhost:8889/graphql
    • 选择请求体(body),选择"GraphQL"。
    • 下方GRAPHQL VARIABLES的框内,可以填入请求的参数,在修改学生信息接口中为StudentDTO对象。

总结

  • 本文简单的介绍了GraphQL,并将其与Spring Boot整合起来,简单用其实现了几个接口,并进行接口调用。目的在于认识GraphQL,并能进行简单使用。GraphQL的内容还有很多,文章没讲到的部分,就靠大家自己研究啦。
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
引用[1]:访问http://localhost:7070/baseLogin?username=xwl&password=123 引用[2]:具体jar包依赖:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> 引用[3]:首先呢,这个项目分为provider提供者和consumer消费者,使用的版本是dubbo3.0.7,nacos是2.0.4。(注意:要使用dubbo3,nacos的版本必须在2.0.0以上) Provider提供者 pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.example</groupId> <artifactId>interfaces</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>2.0.4</version> </dependency> </dependencies> application.properties 问题: springboot3.0整合nacos 回答: 在Spring Boot 3.0中整合Nacos,你需要在pom.xml文件中添加相应的依赖。具体的依赖包括:spring-cloud-starter-bootstrap、spring-boot-starter-web、spring-cloud-starter-alibaba-nacos-config和spring-cloud-starter-alibaba-nacos-discovery。[2]此外,你还需要在application.properties文件中进行相应的配置。在整合过程中,你需要注意使用的版本。对于Dubbo 3和Nacos 2.0.0以上的版本是兼容的。[3]因此,确保你使用的是Dubbo 3.0.7和Nacos 2.0.4版本。这样,你就可以成功地将Spring Boot 3.0与Nacos进行整合了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芝麻馅_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值