火爆外網的 DGS 框架使用

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,歡迎署名轉載 <<<

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章