You're viewing documentation for a version of this software that is in development. Switch to the latest stable version
/
Launch Apollo Studio

Get started


Add the Gradle plugin

Get the latest version of Apollo Android from the Releases page.

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

Using the plugins DSL:

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

Or using the legacy syntax:

buildscript {
  // ...
  classpath("com.apollographql.apollo3:apollo-gradle-plugin:x.y.z")
}

apply(plugin = "com.apollographql.apollo3")

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

By default the plugin generates models in the root package name. You can configure this in the apollo {} block:

apollo {
  packageName.set("com.example")
}

Add the runtime dependencies

dependencies {
  // The core runtime dependencies
  implementation("com.apollographql.apollo3:apollo-runtime:$version")
}

Or for a multiplatform project:

kotlin {
  sourceSets {
    val commonMain by getting {
      dependencies {
        implementation("com.apollographql.apollo3:apollo-runtime:$version")
      }
    }
  }
}

Download your schema file

Apollo Android requires your GraphQL server's schema as either an introspection or SDL schema. Usually, it's a schema.[graphqls|json|sdl] file. This page shows how to obtain a schema from your server using introspection.

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:

(shell)
# Create a directory for your GraphQL files:
mkdir -p app/src/main/graphql/com/example/

# --schema is interpreted relative to the current working directory. This example
# assumes the root project directory and an Android app in `app`
./gradlew downloadApolloSchema \
  --endpoint="https://your.domain/graphql/endpoint" \
  --schema="app/src/main/graphql/com/example/schema.graphqls"

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

(shell)
./gradlew downloadApolloSchema \
  --endpoint="https://your.domain/graphql/endpoint" \
  --schema="app/src/main/graphql/com/example/schema.graphqls" \
  --header="Authorization: Bearer $TOKEN"

Add your query

Put your query in a .graphql file, next to the schema: app/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)
    }
  }
}

Build your project, this will generate the models. Either hit the green triangle in Android Studio or type:

./gradlew build

Execute your query

Create a ApolloClient:

// First, create an `ApolloClient`
// Replace the serverUrl with your GraphQL endpoint
val apolloClient = ApolloClient(serverUrl = "https://your.domain/graphql")

Apollo Android uses coroutines to handle concurrency. If you're using Java, see RxJava2 for an alternative way to execute your queries.

// Replace `runBlocking {}` by `viewModelScope.launch {}` or any other coroutine scope builder
runBlocking {
  val response = try {
    apolloClient.query(LaunchDetailsQuery(id = "83"))
  } catch (e: ApolloException) {
    // handle network errors
    return@runBlocking
  }

  val launch = response.data?.launch
  if (launch == null || response.hasErrors()) {
    // A response containing errors and possibly partial data is received
    // Display the partial data or handle GraphQL errors here
    return@runBlocking
  }

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

What's next

Edit on GitHub