apollographql / apollo-kotlin

:robot:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.7k stars 651 forks source link
android apollographql graphql graphql-client kotlin kotlin-multiplatform multiplatform

Apollo Kotlin

GitHub license Join the community Slack Discord CI Maven Central Gradle Plugin OSS Snapshots Revved up by Develocity

☑️ Apollo Clients User Survey
What do you like best about Apollo Kotlin? What needs to be improved? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Kotlin usage and allow us to serve you better.

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

Apollo Kotlin executes operations against a GraphQL server 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 GraphQL definitions your app uses.

Because generated types are operation-specific, you can only access data that you actually specify as part of an operation. If you don't ask for a particular field in an operation, 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 Kotlin (including multiplatform) and Java app.

Features

Multiplatform

Apollo Kotlin is a Kotlin Multiplatform project.

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

jvm Apple¹ js wasmJs linuxX64
apollo-api (models)
apollo-runtime (network, query batching, apq, ...) 🚫
apollo-normalized-cache 🚫
apollo-adapters 🚫
apollo-normalized-cache-sqlite 🚫 🚫 🚫
apollo-http-cache 🚫 🚫 🚫 🚫

¹: Apple currently includes:

Maintainers

Documentation

Check the project website for in depth documentation.

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 these steps:

Add the plugin to your build.gradle.kts:

plugins {
  id("com.apollographql.apollo3") version "4.0.0-beta.7"
}

Add the runtime dependency:

dependencies {
  implementation("com.apollographql.apollo3:apollo-runtime:4.0.0-beta.7")
}

Set the package name to use for the generated models:

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

Apollo Kotlin supports three types of files:

By default, Apollo Kotlin requires a schema in your module's src/main/graphql directory. You can download a schema using introspection 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 query in a ${module}/src/main/graphql/HeroQuery.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 runtime requirements:

At build time, it requires:

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 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 "4.0.0-beta.7"
}

repositories {
  mavenCentral()
}

dependencies {
  implementation("com.apollographql.apollo3:apollo-runtime:4.0.0-beta.7")

  // Optional: if you want to use the normalized cache
  implementation("com.apollographql.apollo3:apollo-normalized-cache-sqlite:4.0.0-beta.7")
  // 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:4.0.0-beta.7")
}

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-beta.8-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.

Stability of different artifacts

Apollo Kotlin is very modular and publishes several artifacts.

Contributing

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

Community integrations

Additional resources

Who is Apollo?

Apollo builds open-source software and a graph platform to unify GraphQL across your apps and services. We help you ship faster with:

Learn how to build with Apollo

Check out the Odyssey learning platform, the perfect place to start your GraphQL journey with videos and interactive code challenges. Join the Apollo Community to interact with and get technical help from the GraphQL community.