Skip to content

Passing per-request headers #72

@mrtoby

Description

@mrtoby

I started to look on how to do authentication and most solutions I see suggest that you pass your JWT token (or similar) in a http header. The HttpClient object is intended to be reused across threads etc, and thus I expect the same for GraphQLHttpClient.

I have seen code examples (somewhere) where DefaultRequestHeaders is configured with a JWT token. This feels really awkward since that is intended for headers that should be common for all requests, e.g. User-Agent and things like that. Not per-user autentication tokens. If you have a service-to-service setup where the service authenticates once, it might make sense to add it in the DefaultRequestHeaders, otherwise not.

In my setup, I will pass per-user authentication tokens and would like to set headers per-request to the GraphQL endpoint. I do not want to use DefaultRequestHeaders since that not would support concurrent use of the HttpClient/GraphQLHttpClient (which is the intended usage pattern of HttpClient) since the DefaultRequestHeaders is a per-client global setting.

My suggestion: Allow to configure http request headers per request somehow.

Perhaps by making a subclass of GraphQLRequest that allow this. The GraphQLHttpClient should still support executing requests of the type GraphQLRequest, but if a subclass is used, it will consider the http-specific settings for this particular request.

Side note: A more pure solution that is transport independent would be to add a token argument to all queries and mutations that need authentication. This would be such a mess though... It could be cleaned up by using variables, but still really messy and very easy to miss passing the token to one of the 20 different queries/fields being invoked.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions