You are viewing documentation for a previous version of this software.

Switch to the latest stable version.

Using a custom HTTP client


By default, Apollo Kotlin uses the following HTTP clients for different platforms/languages:

PlatformHTTP Client
AndroidOkHttp
JavaScriptKtor
iOS/MacOSNSURLSesssion

You can use a different HTTP client with Apollo Kotlin by creating a custom class that implements the HttpEngine interface.

The HttpEngine interface

The HttpEngine interface defines two functions: execute and dispose. Here's an example implementation that also includes a couple of helper methods:

Kotlin
1class MyHttpEngine(val wrappedClient: MyClient) : HttpEngine {
2  /**
3   * Helper function to map the Apollo requests to MyClient requests
4   */
5  private fun HttpMethod.toMyClientRequest(): MyClientRequest {
6    ...
7  }
8
9  /**
10   * And the other way around
11   */
12  private fun MyClientResponse.toApolloResponse(): HttpResponse {
13    ...
14  }
15
16  override suspend fun execute(request: HttpRequest) = suspendCancellableCoroutine { continuation ->
17
18    val call = wrappedClient.newCall(request.toMyClientRequest())
19    continuation.invokeOnCancellation {
20      // If the coroutine is cancelled, also cancel the HTTP call
21      call.cancel()
22    }
23
24    wrappedClient.enqueue(
25        call,
26        success = { myResponse ->
27          // Success! report the response
28          continuation.resume(myResponse.toApolloResponse())
29        },
30        error = { throwable ->
31          // Error. Wrap in an ApolloException and report the error
32          continuation.resumeWithException(ApolloNetworkException(throwable))
33        }
34    )
35  }
36
37  override fun dispose() {
38    // Dispose any resources here
39  }
40}

This example uses an asynchronous wrappedClient that runs the network request in a separate thread. Note that because HttpEngine.execute itself is called from a background thread, you can safely block in execute().

Using your HttpEngine

After you create your HttpEngine implementation, you can register it with your ApolloClient instance using ApolloClient.Builder.httpEngine:

Kotlin
1// Use your HttpEngine
2val client = ApolloClient.Builder()
3  .serverUrl(serverUrl = "https://com.example/graphql")
4  .httpEngine(httpEngine = MyHttpEngine(wrappedClient))
5  .build()

With this configuration, Apollo Kotlin sends all of its GraphQL operation requests with MyHttpEngine.

Other HTTP customizations

Besides implementing HttpEngine, Apollo Kotlin also supports other methods for customizing HTTP behavior:

  • No runtime: You can opt out of the Apollo Kotlin runtime completely and only use generated models and parsers. Use this option if you don't need any of the runtime features (caching, batching, automatic persisted queries, etc.).

  • HTTP interceptors: If you want to add HTTP headers and/or logging to your requests, HTTP interceptors enable you to do this with minimal code.

Feedback

Edit on GitHub

Forums