hello,大家好,我是小黑,又和大家见面啦~

新开一个专题是关于 GraphQL 的相关内容,主要是通过 Spring Boot 来快速开发 GraphQL 应用,希望对刚接触 GraphQL 的同学有所帮助。

项目 github 地址:https://github.com/shenjianeng/graphql-spring-boot-example

什么是 GraphQL

先看一下官网的解释:

https://graphql.org/

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

https://graphql.cn/

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

再看一下维基百科的解释:

https://en.wikipedia.org/wiki/GraphQL

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data.GraphQL was developed internally by Facebook in 2012 before being publicly released in 2015.

It allows clients to define the structure of the data required, and the same structure of the data is returned from the server.

GraphQL 是一种用于 api 的开源数据查询和操作语言,也是一种用于实现现有数据查询的运行时。GraphQL 于2012年由 Facebook内部开发,2015年公开发布。

它允许客户端定义所需数据的结构,并从服务器返回相同的数据结构。

从字面上理解:GraphQL = Graph + QL = 图表化、可视化的查询语言。它允许客户端定义所需数据的结构,并从服务器返回相同的数据结构。

Hello world

GraphQL 是一种规范,已有多种编程语言支持。

在本系列文章中,我们使用 graphql-spring-boot-starter 来完成 GraphQL 相关开发讲解。

github 地址:https://github.com/graphql-java-kickstart/graphql-spring-boot

引入相关依赖

构建一个基础的 Spring Boot Web 项目工程,引入最新的 graphql-spring-boot-starter:

  <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>8.0.0</version>
</dependency>

通过 maven 我们可以很清楚的看到 graphql-spring-boot-starter 引入了哪些依赖包:

graphql-spring-boot-starter 默认情况下会扫描 classpath 下所有的 graphqls 后缀文件。

当然,我们也可以通过 application.properties 来配置修改相关属性,本案例中,我们使用默认配置即可。

编写第一个 graphqls 文件

在 resources 文件夹下新建 schema.graphqls 文件。

如果你在使用 idea 的话,可以安装 https://plugins.jetbrains.com/plugin/8097-js-graphql 插件来帮助你编写 graphqls 文件。

# 表示构建一个 user 数据结构
type User{
# id,类型就是 ID ,! 表示是必填字段
id:ID!
# username 字段,String 类型
username:String!
nickname:String!
# city 字段,类型是 City 枚举
city:City
} # City 枚举值
enum City{
hangzhou
shanghai
} # 查询相关接口
type Query{
# 获取用户列表,返回 user 数组
userList:[User!]
}

上述 schema.graphqls 文件中定义了 User 和 City 这两种数据类型。同时,我们生成两个 Java Bean 来与之相对应。

public enum City {
hangzhou,
shanghai,
} @Data
public class User {
private UUID id;
private String username;
private String nickname;
private City city;
}

定义 GraphQLQueryResolver

@Component
public class UserGraphQLQueryResolver implements GraphQLQueryResolver { public Collection<User> userList() {
User user1 = new User();
user1.setId(UUID.randomUUID());
user1.setUsername("coder小黑");
user1.setNickname("coder小黑没有昵称");
user1.setCity(City.hangzhou); User user2 = new User();
user2.setId(UUID.randomUUID());
user2.setUsername("今晚打老虎");
user2.setNickname("爱老虎油");
user2.setCity(City.shanghai);
return Arrays.asList(user1, user2);
}
}
  • 定义了一个 Spring Beran UserGraphQLQueryResolver ,实现了 graphql.kickstart.tools.GraphQLQueryResolver 接口
  • 有一个名为 userList 的方法,方法不需要入参,返回 Collection<User>

没错,聪明的读者同学是不是已经发现了:

UserGraphQLQueryResolver#userList 就是用来匹配 schema.graphqls 文件中定义的获取用户列表查询。

使用 graphiql 请求服务器

graphiql 可以帮助我们方便的向 graphql 服务端发起请求,使用也十分简单,引入相关依赖即可。

  <dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>8.0.0</version>
<scope>runtime</scope>
</dependency>

好,让我们启动 Spring Boot 应用,访问 http://localhost:8080/graphiql

https://github.com/graphql-java-kickstart/graphql-spring-boot 的帮助下,实现一个 graphql 服务就是这么的简单。

自定义 Servlet Mapping 地址

我们来看看客户端发出的请求长什么样子:

同时,我们可以通过 application.properties 文件来修改服务端的请求接收路径:

graphql.servlet.mapping=/coder-xiao-hei

使用原生 GraphQL 实现

下面,我们再使用 GraphQL 的原生 api 来实现一下上述的案例。

public class HelloWorld {

    static Collection<User> userList() {
User user1 = new User();
user1.setId(UUID.randomUUID());
user1.setUsername("coder小黑");
user1.setNickname("coder小黑没有昵称");
user1.setCity(City.hangzhou);
return Collections.singletonList(user1);
} public static void main(String[] args) throws IOException {
Resource resource = new DefaultResourceLoader().getResource("classpath:schema.graphqls");
String schema = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8); SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema); RuntimeWiring runtimeWiring =
RuntimeWiring.newRuntimeWiring()
.type(TypeRuntimeWiring
.newTypeWiring("Query")
.dataFetcher("userList",
(DataFetcher<Collection<User>>) environment -> userList()))
.build(); SchemaGenerator schemaGenerator = new SchemaGenerator();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring); GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
ExecutionResult executionResult = build.execute("query{\n" +
" userList{\n" +
" id\n" +
" username\n" +
" }\n" +
"}"); // {userList=[{id=486a181e-eec7-4001-a9d9-65e94a004f8c, username=coder小黑}]}
System.out.println(executionResult.getData().toString());
}
}

下图清晰的描述了上述程序中相关组件的关系:

下期预告

下期我们将使用 graphQL 来实现简单的增删改查和自定义标量类型。感谢大家的关注和阅读~~

参考资料:

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

https://graphql.org

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

https://graphql.org/code/#java-kotlin

Spring Boot GraphQL 实战 01_快速入门的更多相关文章

  1. Spring Boot 2.0 的快速入门(图文教程)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! Spring Boot 2.0 的快速入门(图文教程) 大家都 ...

  2. Spring Boot GraphQL 实战 02_增删改查和自定义标量

    hello,大叫好,我是小黑,又和大家见面啦~ 今天我们来继续学习 Spring Boot GraphQL 实战,我们使用的框架是 https://github.com/graphql-java-ki ...

  3. Spring Boot GraphQL 实战 03_分页、全局异常处理和异步加载

    hello,大家好,我是小黑,又和大家见面啦~ 今天我们来继续学习 Spring Boot GraphQL 实战,我们使用的框架是 https://github.com/graphql-java-ki ...

  4. Spring Boot (一)快速入门

    一.关于Spring Boot 在开始了解Spring Boot之前,我们需要先了解一下Spring,因为Spring Boot的诞生和Spring是息息相关的,Spring Boot是Spring发 ...

  5. Spring Boot(一):入门篇

    Spring Boot(一):入门篇 一.Spring Boot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. 该框架 ...

  6. Spring Boot 2.X(一):入门篇

    什么是 Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架遵循"约定优于配置& ...

  7. “Spring Boot+Marklogic实战应用(1)”

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议.本文链接:http://www.blbk.info Spring Boot+Marklogic应用 摘要: 在前一节的介绍,相信 ...

  8. 【建议收藏】缺少 Vue3 和 Spring Boot 的实战项目经验?我这儿有啊!

    缺少 Vue3 和 Spring Boot 的实战项目经验?缺少学习项目和练手项目?我这儿有啊! 从 2019 年到 2021 年,空闲时间里陆陆续续做了一些开源项目,推荐给大家啊!记得点赞和收藏噢! ...

  9. Spring Boot 项目实战(五)集成 Dubbo

    一.前言 上篇介绍了 Redis 的集成过程,可用于解决热点数据访问的性能问题.随着业务复杂度的提高,单体应用越来越庞大,就好比一个类的代码行数越来越多,分而治之,切成多个类应该是更好的解决方法,所以 ...

随机推荐

  1. css3系列之详解box-shadow

    box-shadow box-shadow呢 是设置元素的阴影效果的,利用这个属性,可以设计很多很炫丽的效果,不信? 等下,学完,我们就来完成下面这两个效果 首先 先了解一下,box-shadow 的 ...

  2. Linux安装禅道教程

    环境: centos7 64位 禅道11.2 Linux一键安装包64位 下载: 禅道下载地址: http://dl.cnezsoft.com/zentao/11.2/ZenTaoPMS.11.2.s ...

  3. C语言讲义——链表的实现

    节点(结构体描述) struct Node { int _id; char s[50]; struct Node* pre;// 指向前一个节点的地址 struct Node* next;// 指向下 ...

  4. 知识点:C语言进阶提高篇,自定义数据类型:枚举

    一.枚举的概念 枚举是C语言中的一种基本数据类型,并不是构造类型,它可以用于声明一组常数.当一个变量有几个固定的可能取值时,可以将这个变量定义为枚举类型.比如,你可以用一个枚举类型的变量来表示季节,因 ...

  5. Verilog单周期CPU(未完待续)

    单周期CPU:指令周期=CPU周期 Top模块作为数据通路 运算器中有ALU,通路寄存器(R1.R2.R3.R4),数据缓冲寄存器(鉴于书上的运算器只有R0)........... 此为ALU和通用寄 ...

  6. django 不使用序列化器时进行查询结果序列化

    在app01views中添加 class User1(View): def post(self,request): user=User.objects.all() list=[] for i in u ...

  7. [翻译自官方]什么是RDB和AOF? 一文了解Redis持久化!

    ​概述 本文提供Redis持久化技术说明,  建议所有Redis用户阅读. 如果您想更深入了解Redis持久性原理机制和底层持久性保证, 请参考文章 揭秘Redis持久化: http://antire ...

  8. moviepy音视频剪辑:TextClip不支持中文字符以及OSError: magick.exe: unable to read font 仿宋_GB2312.ttf的解决办法

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 moviepy对中文和多语言环境的支持做得并不好,包括中文文件名以及用于显示文字的TextClip就是典型的中文支持方面存在问题的.对于编解码的问题 ...

  9. 基于.NET的程序读取Excel文件的解决方案

    目录 0. 前言 1. 使用NPOI库读取Excel文件 2. 使用OleDbConnection 3. 相关参考 shanzm-2020年12月8日 23:48:11 0. 前言 以前基于 .NET ...

  10. 前端webSocket和后台php

    HTTP协议的特性:属于"请求-响应"模型,只有客户端发起了请求消息,服务器才能给出响应消息,没有请求,就没有响应:一个请求消息,服务器只能返回一个响应消息.有些特殊应用场景中,如 ...