Docs
Launch GraphOS Studio

Introduction to Apollo Kotlin

A strongly-typed, caching GraphQL client for Java and Kotlin multiplatform


📣 Apollo Kotlin 3 is generally available. If you're using Apollo Android 2.x, see the migration guide. You can also view the 2.x docs.

Apollo Kotlin (formerly Apollo Android) is a that generates Kotlin and Java models from GraphQL queries.

executes queries and against a and returns results as -specific Kotlin types. This means you don't have to deal with parsing JSON, or passing around Maps and making clients cast values to the right type manually. You also don't have to write model types yourself, because these are generated from the definitions your UI uses.

Because generated types are -specific, you can only access data that you actually specify as part of a query. If you don't ask for a particular in a query, you can't access the corresponding property on the returned data structure.

This library is designed primarily with Android in mind, but you can use it in any Java/Kotlin app, including multiplatform.

Features

  • Java and Kotlin Multiplatform code generation
  • Queries, and s
  • Reflection-free parsing
  • Normalized cache
  • Custom types
  • HTTP cache
  • Auto
  • batching
  • File uploads
  • Espresso IdlingResource
  • Fake models for tests
  • AppSync and -ws websockets
  • AST parser

Multiplatform

is a Kotlin Multiplatform project.

Here's the current matrix of supported features per platform:

jvmApple¹jslinuxX64
apollo-api (models)
apollo-runtime (network, query batching, apq, ...)🚫
apollo-normalized-cache🚫
apollo-adapters🚫
apollo-normalized-cache-sqlite🚫🚫
apollo-http-cache🚫🚫🚫

¹: Apple currently includes:

  • macosX64
  • macosArm64
  • iosArm64
  • iosX64
  • iosSimulatorArm64
  • watchosArm32
  • watchosArm64
  • watchosSimulatorArm64
  • tvosArm64
  • tvosX64
  • tvosSimulatorArm64

Getting started

If you are new to , check out the tutorial that will guide you through building an Android app using Apollo, Kotlin and coroutines.

If you'd like to add to an existing project, follow these steps:

Add the plugin to your build.gradle.kts:

plugins {
id("com.apollographql.apollo3") version "3.8.2"
}

Add the runtime dependency:

dependencies {
implementation("com.apollographql.apollo3:apollo-runtime:3.8.2")
}

Set the package name to use for the generated models:

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

supports three types of files:

  • .graphqls schema files: describes the types in your backend using the syntax.
  • .json schema files: describes the types in your backend using the Json syntax.
  • .graphql executable files: describes your queries and in the syntax.

By default, requires a schema in your module's src/main/graphql directory. You can download a schema using with the ./gradlew downloadApolloSchema task. Sometimes is disabled and you will have to ask your backend team to provide a schema. Copy this schema to your module:

cp ${schema} ${module}/src/main/graphql/

Write a in a ${module}/src/main/graphql/GetRepository.graphql file:

query HeroQuery($id: String!) {
hero(id: $id) {
id
name
appearsIn
}
}

Build your project. This will generate a HeroQuery class that you can use with an instance of ApolloClient:

// Create a client
val apolloClient = ApolloClient.Builder()
.serverUrl("https://example.com/graphql")
.build()
// Execute your query. This will suspend until the response is received.
val response = apolloClient.query(HeroQuery(id = "1")).execute()
println("Hero.name=${response.data?.hero?.name}")

To learn more about other Apollo Kotlin APIs:

Requirements

Some platforms have specific requirements:

  • Android API level 21+ (apollo-http-cache and apollo-adapters require enabling core library desugaring on Android API levels < 26)
  • JDK 8+ (JDK 11+ when using Android Gradle Plugin 7.0+)
  • iOS 13+

For building, it requires:

  • Gradle 5.6
  • Kotlin 1.5+ (1.7+ for native)

Proguard / R8 configuration

As the code generated by doesn't use any reflection, it can safely be optimized / obfuscated by Proguard or R8, so no particular exclusions need to be configured.

IntelliJ Plugin

The JS Graphql IntelliJ Plugin provides auto-completion, error highlighting, and go-to-definition functionality for your .graphql files. You can create a .graphqlconfig file to use scratch files to work with your schema outside product code (such as to write temporary queries to test ). Make sure you check "" in the plugin's settings, so such as @nonnull and typePolicy are recognized.

Releases

The latest version is Maven Central

Check the changelog for the release history.

Releases are hosted on Maven Central. The plugin is additionally hosted on the Gradle Plugin Portal

plugins {
id("com.apollographql.apollo3") version "3.8.2"
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.apollographql.apollo3:apollo-runtime:3.8.2")
// optional: if you want to use the normalized cache
implementation("com.apollographql.apollo3:apollo-normalized-cache-sqlite:3.8.2")
// optional: if you just want the generated models and parsers and write your own HTTP code/cache code, you can remove apollo-runtime
// and use apollo-api instead
implementation("com.apollographql.apollo3:apollo-api:3.8.2")
}

Snapshots

Latest development changes are available in Sonatype's snapshots repository:

// build.gradle.kts
repositories {
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
mavenCentral()
// other repositories...
}
// settings.gradle.kts
pluginManagement {
repositories {
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
gradlePluginPortal()
mavenCentral()
// other repositories...
}
}

And then use the 3.8.3-SNAPSHOT version for the plugin and libraries.

Contributing

If you'd like to contribute, please see Contributing.md.

Community integrations

Additional resources

Next
Migrating to v3
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company