All skills
apollographql avatar

/apollo-kotlin

@93b90fd official
by Apollo GraphQLapollographql/skills115 stars
13

Guide for building applications with Apollo Kotlin, the GraphQL client library for Android and Kotlin. Use this skill when: (1) setting up Apollo Kotlin in a Gradle project for Android, Kotlin/JVM, or KMP, (2) configuring schema download and codegen for GraphQL services, (3) configuring an `ApolloClient` with auth, interceptors, and caching, (4) writing queries, mutations, or subscriptions,

Use this Skill: https://skilld.dev/gh/apollographql/skills/apollo-kotlin

This session only. Nothing lands on disk.

referencescaching.md

≈824 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Caching

This reference covers normalized cache setup and cache policies.

When to use normalized cache

Use a normalized cache when:

  • You need offline support or fast startup.
  • Multiple screens depend on the same objects.
  • You want consistent updates after mutations.

Setup

  • Always use the new Normalized Cache (com.apollographql.cache:*) rather than the classic one (com.apollographql.apollo:*)
  • Always use the latest version
  • To determine the latest version, execute scripts/list-apollo-kotlin-normalized-cache-versions.sh and pick the latest release
  1. Add normalized cache dependencies:
implementation("com.apollographql.cache:normalized-cache:LATEST_CACHE_VERSION") // Memory cache
implementation("com.apollographql.cache:normalized-cache-sqlite:LATEST_CACHE_VERSION") // Disk cache
  1. Configure the cache Apollo Compiler plugin:
apollo {
  service("service") {
    // Other configurations...

    plugin("com.apollographql.cache:normalized-cache-apollo-compiler-plugin:LATEST_CACHE_VERSION") {
      argument("com.apollographql.cache.packageName", packageName.get())
    }
  }
}
  1. Configure the cache in ApolloClient:
val apolloClient = ApolloClient.Builder()
  .serverUrl("https://your.domain/graphql")
  .cache(MemoryCacheFactory()) // or SqlNormalizedCacheFactory() for disk cache
  .build()

Don't forget to add the necessary imports:

// For the generated `cache` extension function, use <the package configured in the apollo configuration in build.gradle.kts>.cache.Cache.cache
// E.g.
import com.example.graphql.cache.Cache.cache

// For MemoryCacheFactory
import com.apollographql.cache.normalized.memory.MemoryCacheFactory

// For SqlNormalizedCacheFactory
import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory

Cache keys

For the normalized cache to work efficiently, always specify the cache keys of your types with the @typePolicy directive.

Use it in an extra.graphqls file next to the schema.graphqls file:

# The directives must be imported
extend schema
@link(
  url: "https://specs.apollo.dev/kotlin_labs/v0.5",
  import: ["@typePolicy", "@fieldPolicy"]
)

extend type User @typePolicy(keyFields: "id")

When objects can be returned by fields that takes their cache keys as argument, declare it like so:

extend type Query @fieldPolicy(forField: "user", keyArgs: "id")

Watching the cache

  • Use the cache as the single source of truth for the UI.
apolloClient.query(GetUserQuery(id))
  .watch()
  .collect { response ->
    // This will be triggered whenever any fields used by GetUserQuery are updated in the cache
    val data = response.dataOrThrow()
    // Update UI with data
  }

Cache policies

The default cache policy is CacheFirst. It can be configured per query:

val response = apolloClient.query(GetUserQuery(id))
  .fetchPolicy(FetchPolicy.NetworkOnly)
  .execute()

The available policies are:

  • CacheFirst: Try cache first, then network.
  • CacheOnly: Always fetch from cache.
  • NetworkOnly: Always fetch from network.
  • NetworkFirst: Try network first, then cache.
  • CacheAndNetwork: Fetch from cache first, then always fetch from network and update cache.

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides a comprehensive guide and supporting scripts for integrating Apollo Kotlin into Android, JVM, and Kotlin Multiplatform projects. All external resources, scripts, and dependencies are sourced from the official Apollo GraphQL organization or well-known services like GitHub.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    5/6 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 93b90fd. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 3 months ago
What it can do
Runs commands
compatibility
JVM 8+, Kotlin 1.9+, Gradle 8+, Android/JVM/Kotlin Multiplatform projects.
metadata
{
  "author": "apollographql",
  "version": "1.0.3"
}
All 1 allowed tools
Bash(./gradlew:*) Bash(gradle:*) Bash(curl:*) Read Write Edit Glob Grep WebFetch
  • apollo
  • graphql
  • kotlin
  • android
  • gradle
  • codegen
  • kmp
  • jvm
  • client

README badge

README badge for apollographql/skills/apollo-kotlin

Instructs an AI agent on setting up and using Apollo Kotlin, the strongly typed GraphQL client for Android, JVM, and Kotlin Multiplatform projects. Covers Gradle configuration, schema codegen, ApolloClient setup with auth and caching, and writing GraphQL operations. Targets Apollo Kotlin v5+ projects.

Generated from the current SKILL.md.

Does this skill work with Android, JVM, and Kotlin Multiplatform projects?
Yes. Apollo Kotlin supports Android, JVM, and Kotlin Multiplatform (KMP) projects. It requires JVM 8+, Kotlin 1.9+, and Gradle 8+.
What version of Apollo Kotlin should I use?
Use Apollo Kotlin v5 or later. The skill includes a migration guide from v4 but does not support v3 or older versions.
Can this skill help with schema download and code generation?
Yes. The skill covers Gradle plugin setup, schema download configuration, and code generation including custom scalar types.
Does this cover authentication and caching?
Yes. The skill includes guidance on configuring ApolloClient with auth, interceptors, and setting up the normalized cache.
Can I use this skill for queries, mutations, and subscriptions?
Yes. The skill covers implementing and executing queries, mutations, and subscriptions in Apollo Kotlin.

Generated from the current SKILL.md. These answers refresh after source changes.