Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free
You're viewing documentation for an upcoming version of this software. Switch to the latest stable version.

Introduction to Apollo Kotlin

A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.


📣 Migration guide: if you're using 3, see the migration guide. You can also view the 3.x docs.

Apollo Kotlin is a strongly typed that generates Kotlin models for your GraphQL .

Apollo Kotlin executes operations against a and returns results as operation-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 app uses.

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

This library is designed with Android in mind, but you can use it in any Kotlin application, including KMP (Kotlin Multi Platform).

Features

  • Kotlin Multiplatform code generation
  • Queries, and s
  • Reflection-free parsing
  • Normalized cache
  • HTTP cache
  • Custom types
  • Auto
  • batching
  • File uploads
  • Fake models for tests
  • AppSync and -ws websockets
  • GraphQL AST parser
  • Plugin for Android Studio and IntelliJ

Getting started

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

If you'd like to add Apollo Kotlin to an existing project, follow the steps below.

Add the plugin to your build.gradle.kts:

plugins {
id("com.apollographql.apollo") version "4.0.0-rc.2"
}

Add the runtime dependency:

dependencies {
implementation("com.apollographql.apollo:apollo-runtime:4.0.0-rc.2")
}

Set the package name to use for the generated models:

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

Apollo Kotlin supports three types of files:

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

By default, Apollo Kotlin requires a schema in your module's src/main/graphql (or src/commonMain/graphql for KMP) directory. You can download a schema using with the ./gradlew downloadApolloSchema task. Sometimes introspection 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/HeroQuery.graphql file:

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

Build your project. This generates 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:

Multiplatform

Apollo Kotlin is a Kotlin Multiplatform project.

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

jvmApple¹jswasmJslinuxX64
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

Requirements

Some platforms have specific runtime requirements:

  • JVM 8+
  • Android API level 21+ (apollo-http-cache requires enabling core library desugaring on Android API levels < 26)
  • iOS 13+

At build time, it requires:

  • Gradle 8.0+
  • Kotlin 1.9+ for JVM projects
  • Kotlin 2.0+ for native, JS, and Wasm projects

Proguard / R8 configuration

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

Android Studio / IntelliJ plugin

A plugin for Android Studio and IntelliJ is available to help you work with Apollo Kotlin, providing automatic code generation, integration with the GraphQL IntelliJ Plugin, navigation to GraphQL definitions, migration helpers, and more.

Installation instructions and more information can be found here.

Releases

The latest version is 4.0.0-rc.2.

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.apollo") version "4.0.0-rc.2"
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.apollographql.apollo:apollo-runtime:4.0.0-rc.2")
// Optional: if you want to use the normalized cache
implementation("com.apollographql.apollo:apollo-normalized-cache-sqlite:4.0.0-rc.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.apollo:apollo-api:4.0.0-rc.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 4.0.0-rc.3-SNAPSHOT version for the plugin and libraries.

These snapshots are updated on each push to main.

Weekly snapshots for the Android Studio / IntelliJ plugin are also available.

Evolution policy

You can read about our evolution policy in the dedicated page

Contributing

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

Community integrations

Additional resources

Next
Migrating to v4
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company