Skip to main content

GraphQL API Integration with Android Project

Apollo Android is a GraphQL client that generates Java and Kotlin models from GraphQL queries. These models give you a type-safe API to work with GraphQL servers.

Add the Gradle plugin

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

Using the plugins DSL:

plugins {
// ...
id("com.apollographql.apollo").version("x.y.z")
}

Or using the legacy syntax:

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 models
generateKotlinModels.set(true)
}

Add the runtime dependencies

dependencies {
// The core runtime dependencies
implementation("com.apollographql.apollo:apollo-runtime:x.y.z")
// Coroutines extensions for easier asynchronicity handling
implementation("com.apollographql.apollo:apollo-coroutines-support:x.y.z")
}

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.

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:

note

Always remember to replace API_SECRET with the correct value that you copied from apito console. Go to this page if you do not know where to find your api secrets for your project

shell
./gradlew downloadApolloSchema \
--endpoint="https://api.apito.io/secured/graphql" \
--schema="app/src/main/graphql/com/example" \
--header="Authorization: Bearer API_SECRET"

Add your query

  1. Create a directory for your GraphQL files: src/main/graphql/com/example/
  2. Add your schema.json to the directory: src/main/graphql/com/example/schema.json
  3. Put your query in a .graphql file, next to the schema: src/main/graphql/com/example/LaunchDetails.graphql
src/main/graphql/com/example/LaunchDetails.graphql
query LaunchDetails($id:ID!) {
launch(id: $id) {
id
site
mission {
name
missionPatch(size:LARGE)
}
}
}
  1. Build your project, this will generate the model

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:

// First, create an `ApolloClient`
// Replace the serverUrl with your GraphQL endpoint
val apolloClient = ApolloClient.builder()
.serverUrl("https://api.apito.io/secured/graphql")
.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 errors
return@launch
}

val launch = response.data?.launch
if (launch == null || response.hasErrors()) {
// handle application errors
return@launch
}

// launch now contains a typesafe model of your data
println("Launch site: ${launch.site}")
}