2. Add the GraphQL schema


This tutorial uses a modified version of the GraphQL server you build as part of the Apollo full-stack tutorial. You can visit that server's GraphOS Studio Sandbox Explorer to explore its schema without needing to be logged in:

The Sandbox query explorer

You'll know that this Sandbox instance is pointed at our server because its URL, https://apollo-fullstack-tutorial.herokuapp.com, is in the box at the top left of the page. If Sandbox is properly connected, you'll see a green dot:

A closeup of the URL box with a dot indicating it's connected

The schema defines which GraphQL operations your server can execute. At the top left, click the schema icon to get an overview of your schema:

The schema icon to click

In the Reference tab, you can now see a list of all of the things available to you as a consumer of this API, along with available fields on all objects:

Apollo sandbox showing the schema reference

Download your server's schema

Apollo Kotlin requires a schema to generate type-safe models and code from your queries. There are multiple ways to get a schema. For example, you can go to the SDL tab and download the raw SDL schema using GraphOS Studio Sandbox.

In this tutorial, we will use the Gradle task that is created by the Apollo plugin automatically. Since GraphQL supports introspection, this will work with any GraphQL endpoint that has introspection enabled.

First, add the URL of the GraphQL endpoint and the desired location of the schena to the Apollo Gradle configuration:

Kotlin
app/build.gradle.kts
1apollo {
2  service("service") {
3    packageName.set("com.example.rocketreserver")
4    introspection {
5      endpointUrl.set("https://apollo-fullstack-tutorial.herokuapp.com/graphql")
6      schemaFile.set(file("src/main/graphql/schema.graphqls"))
7    }
8  }
9}

Then, from the root of the project, run this command in Android Studio's Terminal tab:

shell
(shell)
1./gradlew :app:downloadServiceApolloSchemaFromIntrospection

This will download a schema.graphqls file from your endpoint to app/src/main/graphql/schema.graphqls.

If you installed the Android Studio Plugin, you can also download the schema from Tools | Apollo | Download Schema.

Next, write your first query that uses this schema.

Feedback

Edit on GitHub

Forums