americanexpress / nodes

A GraphQL JVM Client - Java, Kotlin, Scala, etc.
Apache License 2.0
307 stars 70 forks source link

Having Multiple Queries in Single Request #113

Open smfaizalkhan opened 4 years ago

smfaizalkhan commented 4 years ago

Hi @chemdrew, @realityforge

I have been using this tool for Our Application,We have a requirment to send mulitple queries in Single Reques

{
     xxxOverview (id:"12345") 
     { 
        allocation{
           shares
            }
       assets{
           ticker
             }
       id
     } 

   universeMeta
  {
                   etfs{
                  isin
               }
       asset{
           ticker
       }
   }

}

On debugging found that the request has param for only one class

public GraphQLRequestEntity.RequestBuilder request(Class clazz) { this.clazz = clazz; return this; }

Is it possible to have an OverLoaded Method ,taking multiple params like var args or any colletion

public GraphQLRequestEntity.RequestBuilder request(Class ... clazz)

smfaizalkhan commented 3 years ago

@chemdrew Can you let me know if there is a way to combine multiple request queries and send it across the WIRE as single request.

Right now i'm using as below.

    portFolioRequestEntity = GraphQLRequestEntity.Builder()
                .url(portFolioOverViewProperties.getConnectionUrl())
                .request(PortFolioOverView.class).headers(headers)  
                .arguments(
                        new Arguments("portfolioOverview", new Argument("id", PORTFOLIO_ID)))  
                .build();
        log.debug("requestEntity is {}", portFolioRequestEntity.getRequest());

        universeRequestEntity = GraphQLRequestEntity.Builder()
                .url(portFolioOverViewProperties.getConnectionUrl())
                .request(UniverseMeta.class).headers(headers)
                .build();
        log.debug("requestEntity is {}", universeRequestEntity.getRequest());

        String portFolioOverviewReq = getRequestEntityAsString(portFolioRequestEntity);
        log.debug("portFolioOverviewReq" + portFolioOverviewReq);

        String universeReq = getRequestEntityAsString(universeRequestEntity);
        log.debug("universeReq" + universeReq);

        String portFolioSummaryReq = "{" + portFolioOverviewReq
                + universeReq + "}";
        log.debug("combinedRequest is {}", portFolioSummaryReq);
        portFolioSummaryRequestEntity = GraphQLRequestEntity.Builder()
                .url(portFolioOverViewProperties.getConnectionUrl())
                .request(portFolioSummaryReq).headers(headers)
                .build();

               private String getRequestEntityAsString(GraphQLRequestEntity graphQLRequestEntity) {
            String requestEntityAsString = graphQLRequestEntity.getRequest().trim().replaceAll("query", "");
           return requestEntityAsString.substring(requestEntityAsString.indexOf('{') + 1, requestEntityAsString.length() - 1);
               } 

Is there any other better way to achieve it?