Add tokens with Apollo iOS client (GraphQL)

Konstantin Bezzemelnyi 🇺🇦
3 min readJul 5, 2022

--

In my previous articles I showed how you can integrate GraphQL-based backend with your iOS app using Apollo and covered making queries and mutations.

In this article I would like to explain how you add tokens for your network requests using Apollo interceptor.

I will continue with the code from previous articles with already integrated app. Clone the starter code from here.

(You reach the final code at the end of the article).

Photo by Kelly Sikkema on Unsplash

1. Put Token in Request

Create a new TokenInterceptor Swift file.

Next, we should create an interceptor.
As for any interceptor you wish to use with Apollo client, you should implement ApolloInterceptor protocol.

ApolloInterceptor (Apollo Sources)

So, this is, basically, a protocol with one method we should implement.

Let’s do this!

In this implementation of protocol you should:

  1. Add initializer injection for token.
  2. Implement interceptAsync method. The most common use case to handle authorization:

Add token by adding new header named Authorization with “Bearer: \(token)” value.

2. Adjust Apollo Client

Now we need to improve Apollo client in our ViewController.

There are two initializers to setup the class. For something more advanced than usual client for endpoint we will go with the first of them:

Apollo Client initializers

As you see, we have to create NetworkTransport and Apollo store for it.

NetworkTransport is just a protocol that has two provided implementations: RequestChainNetworkTransport and UploadingNetworkTransport.

I chose RequestChainNetworkTransport as we don’t need to upload anything yet. Pass endpointURL as a parameter:

And we are almost there. InterceptorProvider is the main thing in adding interceptors. This is exactly a protocol for easy adding interceptors for a given operation.

3. Setup Network Interceptor

To implement InterceptorProvider create a separate NetworkInterceptorsProvider class:

In the code above:

  1. Subclass DefaultInterceptorProvider
  2. Inject all additional network interceptors in initializer
  3. Add them to interceptors of super class (that are default for each request) and return in overriden func interceptors<Opertaion>(:).

4. Add interceptors to Apollo Client

Finish adding interceptor by using just created NetworkInterceptorsProvider for ApolloClient setup:

Of course, in real code we would save and retrieve Authorization tokens from safe storage (such as Keychain).

That’s it!

Does it work?

Try running the app, and you see nothing. I mean, you see no changes.

But if you check headers of request:

Add this to intercept function of TokenInterceptor

you will see this:

Token being added to request

That’s it. We have added authorization token!

Hope this article gave you more understanding of interceptors in Apollo and how handling Authorization, as well.

Thanks for reading the article!

Here is a final code for you.

You may clap, if this was useful for you 😉.

--

--