Is it possible to do multiple queries in one request with Netflix DGS?

567 Views Asked by At

I am doing the query like this:

    GraphQLQueryRequest req = new GraphQLQueryRequest(query, projection);
    String serialized = req.serialize();
    GraphQLResponse response = getClient().executeQuery(serialized);

The response seems to be prepared to return multiple entities. How do I request multiple queries?

2

There are 2 best solutions below

0
On

If you are try to call the same query twice or more, you should use an alias e.g.:

        this.dgsQueryExecutor.execute("""
                {
                    request1 : doctor {
                     id
                     name
                   },
                    request2 : doctor {
                     id
                     name
                   }
                }
                """);

Otherwise, you can just call two queries like:

        this.dgsQueryExecutor.execute("""
                {
                    doctor {
                     id
                     name
                   },
                    patient {
                     id
                     name
                   }
                }
                """);
1
On

I made the following pr to support multi query requests with DGS:

https://github.com/Netflix/dgs-codegen/pull/426

Implementation might look like something like this.

public GraphQLResponse multiQueryMethod(List<GraphQLQueryRequest> requests) {
    GraphQLMultiQueryRequest request = new GraphQLMultiQueryRequest(
            requests
    );

    return yourClient.executeQuery(request.serialize());
}

I added the ability to add an alias to the request.

You will need to import the following package: implementation "com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:5.11.1"