4. Running code generation
4m

Overview

Now that we have gotten familiar with Sandbox Explorer and have our development environment configured, we will start to write and generate code from them.

In this lesson, we will:

  • Copy a into our project
  • Generate Kotlin code from that
  • Examine how the code changes when we alter the

Copy a query into Android Studio

Let's begin by adding the 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.

Copying the operation in Sandbox

For your convenience, here's the exact text we want:

(Sandbox Explorer)
query LaunchList {
launches {
cursor
hasMore
launches {
id
site
}
}
}

Let's copy the , then return to Android Studio. We'll start by creating a new file for this to live in.

Add the query to your project

  1. Right click on the src/main/graphql/ folder. This folder should contain your schema.graphqls. Select New > GraphQL File:

The new file dialog in Android Studio

  1. Name the file LaunchList.graphql. Make sure it's saved at the same level as your schema.graphqls file.

  2. Copy your final from Sandbox Explorer and paste it into LaunchList.graphql.

Code generation

Build your project to have the 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 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 that your requests:

LaunchListQuery.kt
import com.example.rocketreserver.type.Query as CompiledQuery
public class LaunchListQuery() : Query<LaunchListQuery.Data> {
override fun equals(other: Any?): Boolean = other != null && other::class == this::class
override fun hashCode(): Int = this::class.hashCode()
override fun id(): String = OPERATION_ID
override fun document(): String = OPERATION_DOCUMENT
override fun name(): String = OPERATION_NAME
override 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.Data
public 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: String
get() = "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.

Task!

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 .

Previous

Share your questions and comments about this lesson

This course is currently in

beta
. Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.