Join us for GraphQL Summit, October 10-12 in San Diego. Use promo code ODYSSEY for $400 off your pass.
Launch GraphOS Studio
You're viewing documentation for an upcoming version of this software. Switch to the latest stable version.


HTTP interceptors

Apollo HTTP interceptors

Apollo Kotlin supports multi platform HttpInterceptor very similar to OkHttp Interceptors. Use them to add authentication headers, log the network calls or anything else.

The interface is a single method. For an example, implementing an authentication interceptors can be done with:

class AuthorizationInterceptor(val token: String) : HttpInterceptor {
override suspend fun intercept(request: HttpRequest, chain: HttpInterceptorChain): HttpResponse {
return chain.proceed(request.newBuilder().addHeader("Authorization", "Bearer $token").build())

Then add the interceptor to your HttpNetworkTransport:

val apolloClient = ApolloClient.Builder()

Apollo Kotlin comes bundled with ClientAwarenessInterceptor and LoggingInterceptor that you can set on your ApolloClient.

OkHttp interceptors

If your project is an Android or JVM only project and you already have an OkHttp Interceptor, you can also reuse it:

val okHttpClient = OkHttpClient.Builder()
val apolloClient = ApolloClient.Builder()

GraphQL interceptors

Apollo also supports interceptors at the GraphQL level: ApolloInterceptor. They are useful to customize s before they are sent, or to react to the response, in a centralized way. For example, you could use them to keep track of certain errors, or to implement authentication if the server handles it at the GraphQL level rather than HTTP. Under the hood, Apollo implements the normalized cache, and APQ features with ApolloInterceptor.

Like HttpInterceptor, ApolloInterceptor also has a single method, but the API is Flow based. Remember that the Flow can emit several times, for instance in the case of Subscriptions.

Here's an example of a logging interceptor:

class LoggingApolloInterceptor: ApolloInterceptor {
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
return chain.proceed(request).onEach { response ->
println("Received response for ${}: ${}")

Then add the interceptor to your ApolloClient:

val apolloClient = ApolloClient.Builder()
Custom HTTP clients
Edit on GitHubEditForumsDiscord