GraphQL Summit is back for three days of insights, hands-on learning, and fun to celebrate the GraphQL community. Join us in San Diego Oct 3-5.
Try Apollo Studio

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()

By default, Apollo Kotlin comes with ClientAwarenessInterceptor and LoggingInterceptor.

Reusing OkHttp interceptors on JVM only projects

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

val okHttpClient = OkHttpClient.Builder()
val apolloClient = ApolloClient.Builder()
Edit on GitHub
Custom HTTP clients