Get started with Kotlin
Add the Gradle plugin
In your app Gradle file, apply the com.apollographql.apollo
plugin:
Using the plugins DSL:
1plugins {
2 // ...
3 id("com.apollographql.apollo").version("x.y.z")
4}
1plugins {
2 // ...
3 id("com.apollographql.apollo").version("x.y.z")
4}
Or using the legacy syntax:
1buildscript {
2 // ...
3 classpath("com.apollographql.apollo:apollo-gradle-plugin:x.y.z")
4}
5
6apply(plugin = "com.apollographql.apollo")
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.
Configure the plugin
1apollo {
2 // instruct the compiler to generate Kotlin models
3 generateKotlinModels.set(true)
4}
1apollo {
2 // instruct the compiler to generate Kotlin models
3 generateKotlinModels.set(true)
4}
Add the runtime dependencies
1dependencies {
2 // The core runtime dependencies
3 implementation("com.apollographql.apollo:apollo-runtime:x.y.z")
4 // Coroutines extensions for easier asynchronicity handling
5 implementation("com.apollographql.apollo:apollo-coroutines-support:x.y.z")
6}
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:
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:
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
Create a directory for your GraphQL files:
src/main/graphql/com/example/
Add your
schema.json
to the directory:src/main/graphql/com/example/schema.json
Put your query in a
.graphql
file, next to the schema:src/main/graphql/com/example/LaunchDetails.graphql
1query LaunchDetails($id:ID!) {
2 launch(id: $id) {
3 id
4 site
5 mission {
6 name
7 missionPatch(size:LARGE)
8 }
9 }
10}
Build your project, this will generate the model
Execute 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:
1// First, create an `ApolloClient`
2// Replace the serverUrl with your GraphQL endpoint
3val apolloClient = ApolloClient.builder()
4 .serverUrl("https://your.domain/graphql/endpoint")
5 .build()
6
7// in your coroutine scope, call `ApolloClient.query(...).toDeferred().await()`
8scope.launch {
9 val response = try {
10 apolloClient.query(LaunchDetailsQuery(id = "83")).toDeferred().await()
11 } catch (e: ApolloException) {
12 // handle protocol errors
13 return@launch
14 }
15
16 val launch = response.data?.launch
17 if (launch == null || response.hasErrors()) {
18 // handle application errors
19 return@launch
20 }
21
22 // launch now contains a typesafe model of your data
23 println("Launch site: ${launch.site}")
24}
What's next
Execute your first mutation
Handle custom scalar types
Factor common patterns using fragments