Netflix 已开放其 Domain Graph Service(DGS)框架的源代码 ,该框架是为了方便整合 GraphQL 使用,用于简化 GraphQL 的实现。

GraphQL 主要是作用于数据接口,比如前端后端交互。无需定义或修改后台 Controller、Service 等业务代码即可实现灵活的数据变更,客户端可以自由获取服务端事先定义好的数据,提高了交互接口的灵活性

组件依赖

  • graphql-dgs-spring-boot-starter
<dependency>
<groupId>com.netflix.graphql.dgs</groupId>
<artifactId>graphql-dgs-spring-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
  • DGS 必须从 jcenter 下载,不然部分依赖无法下载。踩坑很久
	<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray-plugins</name>
<url>https://jcenter.bintray.com</url>
</pluginRepository>
</pluginRepositories>
<id>bintray</id>
</profile>
</profiles>

定义接口 schema

  • /src/main/resources/schema/schema.graphqls

此文件定义了客户端请求入参格式和查询数据类型

type Query {
shows(title: String ,releaseYear: Int): [Show]
} type Show {
title: String
releaseYear: Int
}

定义数据抽取规则

@DgsComponent
public class ShowsDatafetcher { @DgsData(parentType = "Query", field = "shows")
public List<Show> shows(@InputArgument("title") String title, @InputArgument("releaseYear") Integer releaseYear) {
if (title == null) {
return shows;
} return shows.stream().filter(s -> s.getTitle().contains(title)).collect(Collectors.toList());
} // 模拟 DB 查询
private final List<Show> shows = List.of(
new Show("java", 1995),
new Show("php", 1995),
new Show("python", 1990),
new Show("golang", 2009),
new Show("rust", 2015)
);
}

UI 前端调试

  • 条件查询

接口调用

curl --location --request POST 'http://localhost:8080/graphql' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"{\n shows(title: \"java\", releaseYear: 1995) {\n title\n releaseYear\n }\n}\n","variables":null}'

java 调用

@SpringBootTest(classes = {DgsAutoConfiguration.class, ShowsDatafetcher.class})
class ShowsDatafetcherTests { @Autowired
DgsQueryExecutor dgsQueryExecutor; @Test
void shows() {
List<String> titles = dgsQueryExecutor.executeAndExtractJsonPath(
" { shows { title releaseYear }}",
"data.shows[*].title");
assertThat(titles).contains("java");
}
}

本节源码

源码: https://github.com/lltx/dgs-demo

DGS 官网: https://netflix.github.io/dgs

>>> 源码 https://gitee.com/log4j/pig,欢迎署名转载 <<<

本篇文章由一文多发平台ArtiPub自动发布

最新文章

  1. NetBeans invalid jdkhome specified 问题解决方法
  2. cookie工具类,解决servlet3.0以前不能添加httpOnly属性的问题
  3. Android酷炫实用的开源框架(UI框架)
  4. c程序辨别系统是64位 or 32位
  5. window绝对路径与相对路径
  6. CSV - 操作比较
  7. 基于linux运用python开发知识点滴
  8. BZOJ 2440 完全平方数(莫比乌斯-容斥原理)
  9. AS 2.0新功能 Instant Run
  10. zhenai
  11. 201521123091 《Java程序设计》第1周学习总结
  12. 每天学一点Docker(6)——镜像和DockerFile
  13. iBatis基础知识
  14. SDL 开发实战(七): SDL 多线程与锁机制
  15. 使用以太网通信方式刷新AB PLC固件
  16. MariaDB xtrabackup物理备份与还原
  17. Redis的五种数据类型
  18. Mongodb 笔记 - 性能及Java代码
  19. jmeter中split分隔字符
  20. JSON Web Tokens测试工具

热门文章

  1. Techme INC解读基因魔剪,带来的是机遇还是风险?
  2. NGK与AOFEX交易所达成战略合作,BGV即将上线A网!
  3. Docker使用指南
  4. 从几个问题开始理解CFS调度器
  5. 微信小程序:添加全局的正在加载中图标效果
  6. Hive实现自增序列及常见的Hive元数据问题处理
  7. 【转】理解Serverless
  8. SQL学习笔记——创建数据库显示:文件激活错误,物理文件名不存在&gt;&gt;解决方案
  9. Hadoop的常用命令
  10. Boltdb学习笔记之〇--概述