公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

前言

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

火爆外网的 DGS 框架使用,更方便 GraphQL 的使用

 

条件查询

火爆外网的 DGS 框架使用,更方便 GraphQL 的使用

 

接口调用

curl --location --request POST 'http://localhost:8080/graphql' 
--header 'Content-Type: Application/json' 
--data-raw '{"query":"{n  shows(title: "java", releaseYear: 1995) {n    titlen    releaseYearn  }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");
    }
}

分享到:
标签:框架 DGS
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定