关于前端:火爆外网的-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,欢送署名转载 <<<

【腾讯云】轻量 2核2G4M,首年65元

阿里云限时活动-云数据库 RDS MySQL  1核2G配置 1.88/月 速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据