Overview
Now that we have gotten familiar with Sandbox Explorer and have our development environment configured, we will start to write GraphQL operations and generate code from them.
In this lesson, we will:
- Copy a GraphQL query into our project
- Generate Kotlin code from that query
- Examine how the code changes when we alter the query
Copy a query into Android Studio
Let's begin by adding the query we ran in Sandbox to our project. Jump back to Sandbox. On the upper right-hand side of the Operation panel, we'll find a menu with three dots. Clicking this, we'll see a dropdown with an option to Copy operation.
For your convenience, here's the exact text we want:
query LaunchList {launches {cursorhasMorelaunches {idsite}}}
Let's copy the operation, then return to Android Studio. We'll start by creating a new file for this query to live in.
Add the query to your project
- Right click on the
src/main/graphql/
folder. This folder should contain yourschema.graphqls
. Select New > GraphQL File:
Name the file
LaunchList.graphql
. Make sure it's saved at the same level as yourschema.graphqls
file.Copy your final query from Sandbox Explorer and paste it into
LaunchList.graphql
.
Code generation
Build your project to have the Apollo Kotlin plugin generate your first model. The plugin defines a task named generateApolloSources
to generate the models, but you don't actually need to run it! It will be executed automatically when building your project. Autocomplete won't work until you build your project. That is because autocomplete requires the generated code to work. For your convenience, the Apollo GraphQL plugin for Android Studio automatically runs code generation when you save .graphql
files in your project.
Examine the generated code
From the menu, select Navigate > Class and start typing LaunchList
; Android Studio should suggest to open LaunchListQuery.kt
. The file should be in app/build/generated/source/apollo/service/com/example/rocketreserver/LaunchListQuery.kt
.
The LaunchListQuery.kt
file defines a top-level class, LaunchListQuery
, with many nested classes. If you compare the classes to the JSON data returned in Sandbox Explorer, you see that the structure matches. These classes include properties only for the fields that your query requests:
import com.example.rocketreserver.type.Query as CompiledQuerypublic class LaunchListQuery() : Query<LaunchListQuery.Data> {override fun equals(other: Any?): Boolean = other != null && other::class == this::classoverride fun hashCode(): Int = this::class.hashCode()override fun id(): String = OPERATION_IDoverride fun document(): String = OPERATION_DOCUMENToverride fun name(): String = OPERATION_NAMEoverride fun serializeVariables(writer: JsonWriter,customScalarAdapters: CustomScalarAdapters,withDefaultValues: Boolean,) {// This operation doesn't have any variable}override fun adapter(): Adapter<Data> = LaunchListQuery_ResponseAdapter.Data.obj()override fun rootField(): CompiledField = CompiledField.Builder(name = "data",type = CompiledQuery.type).selections(selections = LaunchListQuerySelections.__root).build()@ApolloAdaptableWith(LaunchListQuery_ResponseAdapter.Data::class)public data class Data(public val launches: Launches,) : Query.Datapublic data class Launches(public val cursor: String,public val hasMore: Boolean,public val launches: List<Launch?>,)public data class Launch(public val id: String,public val site: String?,)public companion object {public const val OPERATION_ID: String ="5ba8d0c7288c32cedf8b78449eabaa7021536f0dc4cd807e28a15d121be3c014"/*** The minimized GraphQL document being sent to the server to save a few bytes.* The un-minimized version is:** ```* query LaunchList {* launches {* cursor* hasMore* launches {* id* site* }* }* }* ```*/public val OPERATION_DOCUMENT: Stringget() = "query LaunchList { launches { cursor hasMore launches { id site } } }"public const val OPERATION_NAME: String = "LaunchList"}}
Try commenting out the id
property in LaunchList.graphql
, saving, then building again. When the build completes, the Launch
class now only includes the requested site
property.
Uncomment id
and rebuild to restore the property.
Up next
Now that you've generated code and had a chance to see what's in there, it's time to get everything working end to end! Next, we'll execute the query.
Share your questions and comments about this lesson
This course is currently in
You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.