Apollo Kotlin Documentation

repository·main·Indexed 26 days ago

https://github.com/apollographql/apollo-kotlin

A type-safe GraphQL client for Kotlin, Android, and Kotlin Multiplatform (KMP). It features code generation, in-memory and SQLite caching, and integration with the Apollo GraphQL platform. The library includes modules for AST manipulation (apollo-ast), a low-level compiler API (apollo-compiler), HTTP caching (apollo-http-cache), and a runtime for executing queries via ApolloClient (apollo-runtime).

Tokens
70.1K
Snippets
179
Records
358
Agent score
86%

What's inside Apollo Kotlin

  1. Overview of apollo-compiler

    main

    apollo-compiler is a low-level compiler API designed to generate Java and Kotlin models from GraphQL operations. It is the underlying engine used by the apollo-gradle-plugin. The module utilizes JavaPoet and KotlinPoet for code generation.

    End-users typically consume this module through Gradle or Maven plugins. It also provides the ApolloCompilerPlugin interface for extending compiler functionality.

  2. Overview of caching options in Apollo Kotlin

    main

    Apollo Kotlin provides several caching mechanisms depending on whether you need to deduplicate data, optimize network requests, or persist data to disk.

    Supported Cache Types

    • Normalized Caches: Deduplicates GraphQL response data, allowing the cache to act as a single source of truth for your UI. It can dynamically react to data changes. You can use an in-memory cache for speed and a SQLite cache for persistence, often combining both.
    • HTTP Cache: Caches response data at the HTTP level. It is easier to implement and uses less CPU than normalized caches, but it duplicates data and cannot serve as a single source of truth for the UI.
    • External Caches: You can integrate Apollo Kotlin with existing databases like Room or SQLDelight. In this pattern, you use Apollo's generated models for network responses and map them to your own database models.
    • Persisted Queries (APQ): A server-side optimization where the client sends a hash of the query instead of the full query string. This reduces request size, enables the use of GET requests, and allows for CDN-level caching. This can be implemented via Automatic Persisted Queries (APQ) or custom persisted query IDs.
  3. Use test network transports for mocking GraphQL responses

    main

    The apollo-testing-support module provides specialized network transports designed for testing GraphQL queries without requiring a running mock server. You can use QueueTestNetworkTransport or MapTestNetworkTransport to simulate network responses directly within your tests.

    For detailed implementation guidance, refer to the official documentation on Mocking GraphQL responses.

  4. Use apollo-api for minimal model compilation and parsing

    main

    apollo-api provides the essential symbols required to compile generated GraphQL code and parse responses. It is a lightweight alternative to the full runtime.

    Note: This module does not include networking or caching capabilities. If you require a complete Apollo client implementation with networking and caching, use apollo-runtime instead.

    For instructions on how to use these models without the full runtime, refer to the official guide on using models without apollo-runtime.

  5. Understand Apollo Kotlin's modular architecture

    main

    Apollo Kotlin is highly modular, allowing you to include only the features you need to keep binary size and compile times small. The library is split into main modules for runtime, caching, and tooling, as well as deprecated modules that have moved to Apollo Galaxy.

    Key functional areas include:

    • Runtime & API: apollo-runtime (main entry point) and apollo-api (minimal symbols).
    • Caching: apollo-http-cache (HTTP level) and apollo-normalized-cache (application level).
    • Tooling: apollo-gradle-plugin and apollo-compiler.
    • Testing: apollo-testing-support for mocking responses.
  6. Understand GraphQL Type Definitions in Apollo Kotlin

    main

    The library uses specific terminology to describe GraphQL types:

    • Raw type: The named type without any List or NonNull wrappers.
    • Leaf type: A type that contains no subfields (e.g., a Scalar or an Enum).
    • Composite type: A type that contains subfields (e.g., an Interface, Object, or Union).
    • Concrete type: A synonym for an Object type.
    • Possible types: The set of concrete types that satisfy a specific type condition.
    • Polymorphic field: A field that can return multiple different shapes.
  7. Use Apollo Kotlin Compose (Experimental)

    main

    Apollo Kotlin Compose is an experimental framework designed for developers using Jetpack Compose. It works alongside a special compiler plugin to provide APIs optimized for declarative UI.

    Key goals include:

    • Fragment colocation
    • Error boundaries
    • Deep integration with the Jetpack Compose UI framework.
  8. Understand Schema Definitions and Directives

    main

    Apollo Kotlin distinguishes between different types of definitions (directives/metadata) in the schema:

    • API schema: The server schema as seen via introspection (JSON or SDL).
    • Server definition: Definitions present in the API schema (e.g., @include, @oneOf, __Schema).
    • Client definition: Definitions added to the API schema by the client via @link or concatenation.
    • Linked definition: A definition added to a schema using @link (e.g., @targetName, @typePolicy).
    • Checked definitions: Definitions recognized by Apollo Kotlin that must follow a specific shape to prevent compiler crashes or unexpected behavior (e.g., @oneOf, @semanticNonNull, @targetName).