【问题标题】:Generating projection using graphql file for DGS client使用 graphql 文件为 DGS 客户端生成投影
【发布时间】:2022-07-27 06:04:07
【问题描述】:

我正在尝试使用 Netflix DGS 编写 java-graphql 客户端。 在开发者documentation 中,给出了以下示例代码,用于使用 DGS codegen 生成的类生成查询。

GraphQLQueryRequest graphQLQueryRequest =
                new GraphQLQueryRequest(
                    new TicksGraphQLQuery.Builder()
                        .first(first)
                        .after(after)
                        .build(),
                    new TicksConnectionProjectionRoot()
                        .edges()
                            .node()
                                .date()
                                .route()
                                    .name()
                                    .votes()
                                        .starRating()
                                        .parent()
                                    .grade());

这里所有要查询的字段都在代码中给出,使用TicksConnectionProjectionRoot

但是如果我想查询 15-20 个包含许多嵌套类型的字段,那就太麻烦了。有什么方法可以使用包含 Grapqhl 查询和所需字段的 .graphql 文件生成类似的 graphql 请求或投影。

【问题讨论】:

    标签: java spring-boot graphql netflix-dgs


    【解决方案1】:

    如果是您的 junit 测试,那么我建议直接使用 DgsQueryExecutor。

    ExecutionResult result = dgsQueryExecutor.execute(getQuery("/graphql/request/query1.txt"));
        
    public static String getQuery(String path) throws IOException {
        InputStream stream = MyClass.class.getResourceAsStream(path)
        return IOUtils.toString(stream, StandardCharsets.UTF_8);
                
    }
    

    query.txt 包含客户端通过 http 请求发送的实际图形查询字符串。

    DgsQueryExecutor 有多种方法可以满足大多数测试需求。

    【讨论】:

      猜你喜欢
      • 2020-10-25
      • 2019-07-26
      • 2020-05-03
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2020-12-04
      相关资源
      最近更新 更多