首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >火爆外网的 DGS 框架使用

火爆外网的 DGS 框架使用

作者头像
冷冷
发布2021-03-04 10:21:59
8020
发布2021-03-04 10:21:59
举报
文章被收录于专栏:冷冷冷冷

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 前端调试

  • 访问: http://localhost:8080/graphiql
  • 条件查询

接口调用

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,欢迎署名转载 <<<

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 组件依赖
  • 定义接口 schema
  • 定义数据抽取规则
  • UI 前端调试
  • 接口调用
  • java 调用
  • 本节源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档