Set up Apollo Kotlin
Apollo Kotlin includes two main components: a Gradle plugin that reads your schema and operation files to generate type-safe Kotlin code, and a runtime client that executes requests using the generated code. This page covers adding dependencies, configuring the Gradle plugin, and setting up the Apollo client.
Add dependencies
Add the following to your version catalog (gradle/libs.versions.toml):
[versions]apollo = "4.1.0"apolloAppsync = "1.0.0"
[libraries]apollo-runtime = { group = "com.apollographql.apollo", name = "apollo-runtime", version.ref = "apollo" }apollo-appsync = { group = "com.amplifyframework", name = "apollo-appsync", version.ref = "apolloAppsync" }apollo-appsync-amplify = { group = "com.amplifyframework", name = "apollo-appsync-amplify", version.ref = "apolloAppsync" }
[plugins]apollo = { id = "com.apollographql.apollo", version.ref = "apollo" }Add the Apollo plugin to your root build.gradle.kts:
plugins { // ... existing plugins alias(libs.plugins.apollo) apply false}Add the plugin and dependencies to your app build.gradle.kts:
plugins { // ... existing plugins alias(libs.plugins.apollo)}
dependencies { // Apollo Kotlin implementation(libs.apollo.runtime)
// AWS AppSync Apollo Extensions implementation(libs.apollo.appsync) implementation(libs.apollo.appsync.amplify)
// Keep your existing Amplify Auth dependencies (if using Amplify for auth) // implementation(libs.amplify.auth.cognito) // implementation(libs.amplify.api)}Configure the Apollo Gradle plugin
Read the Apollo Kotlin Getting Started section for more information, and the Gradle Plugin configuration documentation for all configuration options.
Your Apollo configuration should look similar to this:
apollo { service("example") { packageName.set("com.example.appsync") schemaFile.set(file("src/main/graphql/schema.json"))
// Map AppSync custom scalars to Kotlin String. // Without this, fields like createdAt and updatedAt will be // generated as `Any` instead of `String`. // IMPORTANT: Only include scalars that are actually used in your schema. // Apollo will error on unknown scalars. Check your downloaded schema.json // to see which custom scalars your API uses. mapScalarToKotlinString("AWSDateTime") mapScalarToKotlinString("AWSTimestamp") // Add any of the following only if your schema uses them: // mapScalarToKotlinString("AWSDate") // mapScalarToKotlinString("AWSTime") // mapScalarToKotlinString("AWSEmail") // mapScalarToKotlinString("AWSJSON") // mapScalarToKotlinString("AWSURL") // mapScalarToKotlinString("AWSPhone") // mapScalarToKotlinString("AWSIPAddress") }}Configure the Apollo client
The runtime component of Apollo must be configured to connect to AWS AppSync, including handling authorization modes and the subscription protocol. The AWS AppSync Apollo Extensions library implements the required logic.
With Amplify Gen 2 config (amplify_outputs.json)
If your project uses Amplify Gen 2 (or you have converted your Gen 1 config — see the note below), you can use ApolloAmplifyConnector to read the endpoint and configure authorization automatically.
For API Key auth:
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))val apolloClient = ApolloClient.Builder() .serverUrl(connector.endpoint.serverUrl.toString()) .addHttpInterceptor(AppSyncInterceptor(connector.apiKeyAuthorizer())) .build()For Cognito User Pool auth (owner-based authorization):
val connector = ApolloAmplifyConnector(context, AmplifyOutputs(R.raw.amplify_outputs))val apolloClient = ApolloClient.Builder() .serverUrl(connector.endpoint.serverUrl.toString()) .addHttpInterceptor(AppSyncInterceptor(connector.cognitoUserPoolAuthorizer())) .build()With Amplify Gen 1 config (amplifyconfiguration.json) — manual endpoint
If you want to keep using your existing Gen 1 amplifyconfiguration.json for Amplify Auth without converting config formats, you can configure the Apollo client manually. You are still using Amplify (for authentication) — only the Apollo endpoint configuration is done manually.
For API Key auth:
val endpoint = AppSyncEndpoint("<your-appsync-endpoint>")val authorizer = ApiKeyAuthorizer("<your-api-key>")val apolloClient = ApolloClient.Builder() .serverUrl(endpoint.serverUrl.toString()) .addHttpInterceptor(AppSyncInterceptor(authorizer)) .build()For Cognito User Pool auth (using Amplify Auth for token fetching):
If you are still using Amplify for Cognito authentication but configuring Apollo manually, you can use ApolloAmplifyConnector.fetchLatestCognitoAuthToken to get the auth token. This method uses callback-based Java Consumer parameters, not Kotlin coroutines, so you need to wrap it with suspendCoroutine:
import kotlin.coroutines.resumeimport kotlin.coroutines.resumeWithExceptionimport kotlin.coroutines.suspendCoroutine
val endpoint = AppSyncEndpoint("<your-appsync-endpoint>")val authorizer = AuthTokenAuthorizer { suspendCoroutine { cont -> ApolloAmplifyConnector.fetchLatestCognitoAuthToken( { token -> cont.resume(token) }, { error -> cont.resumeWithException(error) } ) }}
val apolloClient = ApolloClient.Builder() .serverUrl(endpoint.serverUrl.toString()) .addHttpInterceptor(AppSyncInterceptor(authorizer)) .build()