简体   繁体   English

使用 graphql 文件为 DGS 客户端生成投影

[英]Generating projection using graphql file for DGS client

I'm trying to write a java-graphql client using Netflix DGS.我正在尝试使用 Netflix DGS 编写一个 java-graphql 客户端。 In the developer documentation , the following sample code is given for generating query using classes generated with DGS codegen.在开发人员文档中,给出了以下示例代码,用于使用 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());

Here all the fields to be queried are given in the code itself, using TicksConnectionProjectionRoot .这里所有要查询的字段都在代码本身中给出,使用TicksConnectionProjectionRoot

But if I want to query 15-20 fields with many nested types then it will be tiresome.但是,如果我想查询 15-20 个具有许多嵌套类型的字段,那就很烦人了。 Is there any way to generate the graphql request or a projection like this using a.graphql file containing the grapqhl query and fields needed.有什么方法可以生成 graphql 请求或使用 a.graphql 文件包含所需的grapqhl查询和字段的投影。

If it is your junit test then I would suggest to use DgsQueryExecutor directly.如果是您的 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 contains actual graph query string the client sends via http request. query.txt包含客户端通过 http 请求发送的实际图形查询字符串。

DgsQueryExecutor has various methods to suit most of testing needs. DgsQueryExecutor 有多种方法可以满足大多数测试需求。

If calling via http then you can use MonoGraphQLClient and pass your query string directly.如果通过 http 调用,那么您可以使用MonoGraphQLClient并直接传递您的查询字符串。

WebClient webClient = WebClient.create(url);
WebClientGraphQLClient client = MonoGraphQLClient.createWithWebClient(webClient);
//The GraphQLResponse contains data and errors.
Mono<GraphQLResponse> graphQLResponseMono = graphQLClient.reactiveExecuteQuery(query);

query is string and you can read it from resource. query是字符串,您可以从资源中读取它。 More about client can be found at https://netflix.github.io/dgs/advanced/java-client/ and https://fullstackcode.dev/2022/02/10/java-graphql-client-using-netflix-dgs-framework/ More about client can be found at https://netflix.github.io/dgs/advanced/java-client/ and https://fullstackcode.dev/2022/02/10/java-graphql-client-using-netflix- dgs-框架/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM