Get started with Multiplatform (Experimental)


Note: Multiplatform support is experimental and still under heavy development, expect the API to change. If you have feedback, we'd love to hear it. Please see Contributing.md for how to get in touch.

You can also check out the multiplatform samples for more details.

Kotlin multiplatform allows to use the same queries and generated models on both Android and iOS. On Android it uses OkHttp to handle the HTTP calls. On iOS, it uses NSUrlSession.

In addition to jvm, the supported native platforms currently are:

  • iosArm64

  • iosX64

Furthermore, the js target is also available for the apollo-api dependency - meaning you could use the models and operations in a JavaScript project.

Add the Gradle plugin

In your app Gradle file, apply the com.apollographql.apollo plugin:

Using the plugins DSL:

Kotlin
Groovy
1plugins {
2  // ...
3  id("com.apollographql.apollo").version("x.y.z")
4}

Or using the legacy syntax:

Kotlin
Groovy
1buildscript {
2  // ...
3  classpath("com.apollographql.apollo:apollo-gradle-plugin:x.y.z")
4}
5
6apply(plugin = "com.apollographql.apollo")

The plugin is hosted on the Gradle plugin portal, Jcenter and Maven Central.

Add the runtime dependencies

Multiplatform uses a different artifact from apollo-runtime. Its goal is to offer a coroutine-first API and is named apollo-runtime-kotlin:

Kotlin
build.gradle.kts
1kotlin {
2  // targets
3  jvm()
4  iosArm64("ios")
5
6  sourceSets {
7    commonMain {
8      dependencies {
9        //
10        implementation("com.apollographql.apollo:apollo-runtime-kotlin:x.y.z")
11      }
12    }
13  }
14}

Multithreaded coroutines

If using the x.y.z-native-mt branch of coroutines, gradle will replace the -native-mt version with the non-mt version as outlined here. To prevent this happening add the following to your root build.gradle.kts:

Kotlin
1allprojects {
2    configurations {
3        all {
4            resolutionStrategy {
5                force("org.jetbrains.kotlinx:kotlinx-coroutines-core:x.y.z-native-mt")
6            }
7        }
8    }
9}

This ensures that the same version is used everywhere and that Gradle doesn't fail because -native-mt is a pre-release version (1.5.1-native-mt < 1.5.1)

Download your schema.json file

Apollo Android requires your GraphQL server's schema as a schema.json file. You can obtain the contents of this file by running an introspection query on your server.

Note: If you don't have a GraphQL server yet, you can use the server from the tutorial: https://apollo-fullstack-tutorial.herokuapp.com/graphql.

The Apollo Gradle plugin exposes a downloadApolloSchema task to help you obtain your schema. Provide this task your server's GraphQL endpoint and the output location for the schema.json file:

Bash
1./gradlew downloadApolloSchema \
2  --endpoint="https://your.domain/graphql/endpoint" \
3  --schema="src/main/graphql/com/example/schema.json"

If your GraphQL endpoint requires authentication, you can pass custom HTTP headers:

Bash
1./gradlew downloadApolloSchema \
2  --endpoint="https://your.domain/graphql/endpoint" \
3  --schema="app/src/main/graphql/com/example/schema.json" \
4  --header="Authorization: Bearer $TOKEN"

Add your query

Add your schema.json and other .graphql files under src/commonMain/graphql. Build your module and generated code will be available under commonMain sourceSet. That means you can use them both in commonMain or platform specific Kotlin code. Once the Kotlin plugin builds the iOS Framework, generated code can even be called from Swift code.

Executing your query

You use an instance of the ApolloClient class to interact with your server and cache.

To make a query using your generated models:

Kotlin
1// First, create an `ApolloClient`
2// Replace the serverUrl with your GraphQL endpoint
3  val apolloClient = ApolloClient(
4      networkTransport = ApolloHttpNetworkTransport(
5          serverUrl = "https://your.domain/graphql/endpoint",
6          headers = mapOf(
7              "Accept" to "application/json",
8              "Content-Type" to "application/json",
9          )
10      )
11  )
12
13// in your coroutine scope, call `ApolloClient.query(...).execute()`
14scope.launch {
15  // execute() returns a Flow, here we only take the first item
16  val response = apolloClient.query(LaunchDetailsQuery(id = "83")).execute().single()
17
18  // launch now contains a typesafe model of your data
19  println("Launch site: ${response.data?.launch?.site"})
20}
Feedback

Edit on GitHub

Forums