I am developing a native iOS application using Apollo Client SDK. I want to know the exact way on passing in a header. I read about the interceptor but couldn’t actually figure that out.
Lets say my header values are dynamic based on user choices, how would I create an interceptor and use it.
For example my header needs language and my app allows users to change language with in the app and in one of my fetch call I need to pass this in as header param. Any pointers will be appreciated
Any pointers, it would be great help?
You’ll want to create a custom interceptor to do this! Check out the docs at Creating a client - Apollo GraphQL Docs for more information. If that doesn’t clear it up for you, please reply with a more specific question about what part of the custom interceptor you are struggling with.
@designatednerd I thought we had an example for this, but perhaps we should add an example to the docs of a custom interceptor that sets HTTP request headers?
The sample UserManagementInterceptor
shows how you can add tokens with the addTokenAndProceed
function. It does assume that you have some kind of singleton user manager, rather than trying to pass in information per-request.
You probably have something similar in your application somewhere that can tell what language your user has set, you should be able to add that header param in a similar fashion.
How do you associate an interceptor with queries? Say for example I want to sent one set of headers on a particular request and another set on another request. or lets say how UserManagementInterceptor know that it shouldn’t attach the token to a particular request.
Since operations (including queries) are typed, you can tell the UserManagementInterceptor
to check the type of the HTTPRequest
’s operation
property.
If it’s more than a couple types, we’ve seen people have success declaring an empty Unauthenticated
protocol and just add that to the operations that don’t need to be authenticated - then you can filter on that rather than any particular operation’s type.
Great, It would be nice if you can add some of these recommendations or approaches to the document also . It was bit confusing