简体   繁体   中英

Generating projection using graphql file for DGS client

I'm trying to write a java-graphql client using Netflix DGS. In the developer documentation , the following sample code is given for generating query using classes generated with 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 .

But if I want to query 15-20 fields with many nested types then it will be tiresome. 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.

If it is your junit test then I would suggest to use DgsQueryExecutor directly.

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.

DgsQueryExecutor has various methods to suit most of testing needs.

If calling via http then you can use MonoGraphQLClient and pass your query string directly.

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. 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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