How to pass request headers when using java netflix graphql client?

334 views Asked by At

I am using com.netflix.graphql.dgs:graphql-dgs-client:4.9.16 along with HttpClient (JDK 17)

I am creating graphql client using below.

GraphQLCLient graphQLClient = GraphQLClient.createCustom(
        host-url,
        requestExecutor::exec);

And my request executor is based on HttpClient.

The requestExecutor.exec(String url, Map<String, ? extends List<String>> headers, String body) method can accept the headers but I am not seeing a graphql client method that can pass or accept httpheader.

What I am currently using is -

var request = new GraphQLQueryRequest(
        OurStoreGraphQLQuery.newRequest().oneStoreId(oneStoreId).build(),
        RESPONSE_PROJECTION; 

GraphQLResponse response = graphQLClient.executeQuery(request.serialize());

Neither of the above i.e. GraphQLQueryRequest constructor or executeQuery method has a overloaded equivalent that accept HttpHeader and in-turn pass it to requestExecutor::exec method.

  1. What is the point of exec method accepting headers if it can not be passed by the user code with out creating a new instance of RequestExecutor. -- from where is the headers parameter picked up, how can i add more headers to the above headers param??

Probably I can use the below style as mentioned in java-client/#plug-in-your-own-http-client docs -

CustomGraphQLClient client = GraphQLClient.createCustom(url,  (url, headers, body) -> {
    HttpHeaders httpHeaders = new HttpHeaders();
    headers.forEach(httpHeaders::addAll);
    ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(body, httpHeaders),String.class);
    return new HttpResponse(exchange.getStatusCodeValue(), exchange.getBody());
});

GraphQLResponse graphQLResponse = client.executeQuery(query, emptyMap(), "SubmitReview");
String submittedBy = graphQLResponse.extractValueAsObject("submitReview.submittedBy", String.class);

-- but looks like i will have to create a custom client instance each time my header changes. Is that right?? -- Using the above suggested approach, it does not look like i can use the same requestExecutor i.e requestExecutor::exec in a generic approach.

0

There are 0 answers