火爆外网的 DGS 框架使用心得~

Java斗帝之路 · · 513 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

image

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

image

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

组件依赖

  • graphql-dgs-spring-boot-starter

<pre class="custom" style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 15px; position: relative; overflow: auto; line-height: 1.75; color: rgb(51, 51, 51); background: rgb(248, 248, 248); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency> <groupId>com.netflix.graphql.dgs</groupId> <artifactId>graphql-dgs-spring-boot-starter</artifactId> <version>3.5.1</version> </dependency> 复制代码</pre>

  • DGS 必须从 jcenter 下载,不然部分依赖无法下载。踩坑很久

<pre class="custom" style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 15px; position: relative; overflow: auto; line-height: 1.75; color: rgb(51, 51, 51); background: rgb(248, 248, 248); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><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> 复制代码</pre>

定义接口 schema

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

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

<pre class="custom" style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 15px; position: relative; overflow: auto; line-height: 1.75; color: rgb(51, 51, 51); background: rgb(248, 248, 248); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">`type Query {
shows(title: String ,releaseYear: Int): [Show]
}

type Show {
title: String
releaseYear: Int
}
复制代码`</pre>

定义数据抽取规则

<pre class="custom" style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 15px; position: relative; overflow: auto; line-height: 1.75; color: rgb(51, 51, 51); background: rgb(248, 248, 248); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">`@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)
);
}

复制代码`</pre>

UI 前端调试

image
  • 条件查询
image

接口调用

<pre class="custom" style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 15px; position: relative; overflow: auto; line-height: 1.75; color: rgb(51, 51, 51); background: rgb(248, 248, 248); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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}' 复制代码</pre>

java 调用

<pre class="custom" style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 15px; position: relative; overflow: auto; line-height: 1.75; color: rgb(51, 51, 51); background: rgb(248, 248, 248); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">`@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");
}
}

复制代码`</pre>

本节源码

源码: https://github.com/lltx/dgs-demo
DGS 官网: https://netflix.github.io/dgs
>>> 源码 https://gitee.com/log4j/pig,欢迎署名转载 <<<


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:Java斗帝之路

查看原文:火爆外网的 DGS 框架使用心得~

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

513 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传