Is there any way to attach a header at the request level to HttpGraphQlClient without client regeneration. I want to attach individual session id of the user to the graphql request.
The solution I found in the official documentation is this:
Once
HttpGraphQlClientis created, you can begin to execute requests using the same API, independent of the underlying transport. If you need to change any transport specific details, usemutate()on an existingHttpGraphQlClientto create a new instance with customized settings.
So my class looks like this:
@Component
public class MyGraphqlClient {
@Autowired
private HttpGraphQlClient graphQlClient; // a preconfigured bean, used as a prototype
public Mono<MyResponse> getCompany(String input) {
return graphQlClient.mutate()
.header("x-my-header", "MY_VALUE") // Here I set my request-specific header
.build()
.documentName("NAME OF THE RESOURCE in resources/graphql-documents")
.variable("input", input)
.retrieve("QUERY_NAME")
.toEntity(MyResponse.class)
;
}
}
It is not completely "without client regeneration" as you requested, but at least the pre-generated client is re-used as a prototype, and I don't have to set up all its parameters again. And it's in accordance with the official documentation.
Nevertheless, I would like to have a better solution :) It still creates a new client, but at least "not as much" as creating it from scratch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With