Get started with Kotlin
Add the Gradle plugin
In your app Gradle file, apply the com.apollographql.apollo
plugin:
Using the
plugins {// ...id("com.apollographql.apollo").version("x.y.z")}
plugins {// ...id("com.apollographql.apollo").version("x.y.z")}
Or using the
buildscript {// ...classpath("com.apollographql.apollo:apollo-gradle-plugin:x.y.z")}apply(plugin = "com.apollographql.apollo")
buildscript {// ...classpath("com.apollographql.apollo:apollo-gradle-plugin:x.y.z")}apply plugin: "com.apollographql.apollo"
The plugin is hosted on the Gradle plugin portal, Jcenter and Maven Central.
Configure the plugin
apollo {// instruct the compiler to generate Kotlin modelsgenerateKotlinModels.set(true)}
apollo {// instruct the compiler to generate Kotlin modelsgenerateKotlinModels.set(true)}
Add the runtime dependencies
dependencies {// The core runtime dependenciesimplementation("com.apollographql.apollo:apollo-runtime:x.y.z")// Coroutines extensions for easier asynchronicity handlingimplementation("com.apollographql.apollo:apollo-coroutines-support:x.y.z")}
Download your schema.json
file
schema.json
fileApollo 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
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:
./gradlew downloadApolloSchema \--endpoint="https://your.domain/graphql/endpoint" \--schema="src/main/graphql/com/example/schema.json"
If your GraphQL endpoint requires authentication, you can pass custom HTTP headers:
./gradlew downloadApolloSchema \--endpoint="https://your.domain/graphql/endpoint" \--schema="app/src/main/graphql/com/example/schema.json" \--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
query LaunchDetails($id:ID!) {launch(id: $id) {idsitemission {namemissionPatch(size:LARGE)}}}
- 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:
// First, create an `ApolloClient`// Replace the serverUrl with your GraphQL endpointval apolloClient = ApolloClient.builder().serverUrl("https://your.domain/graphql/endpoint").build()// in your coroutine scope, call `ApolloClient.query(...).toDeferred().await()`scope.launch {val response = try {apolloClient.query(LaunchDetailsQuery(id = "83")).toDeferred().await()} catch (e: ApolloException) {// handle protocol errorsreturn@launch}val launch = response.data?.launchif (launch == null || response.hasErrors()) {// handle application errorsreturn@launch}// launch now contains a typesafe model of your dataprintln("Launch site: ${launch.site}")}
What's next
- Execute your first mutation
- Handle custom scalar types
- Factor common patterns using fragments