Introduction to Apollo iOS
A strongly typed, Swift-first GraphQL client
Apollo iOS is an open-source GraphQL client for native client applications, written in Swift.
Apollo iOS executes queries and mutations against a GraphQL server and returns results as pre-generated, operation-specific Swift types. This means you don't have to deal with forming spec-compliant GraphQL requests, parsing JSON responses, or manually validating and type-casting response data.
Apollo iOS also includes caching mechanisms that are designed specifically for GraphQL data, enabling you to execute your GraphQL queries against locally cached data directly.
Apollo iOS consists of three primary components:
When used together, these components dramatically improve the process of requesting, consuming, and persisting GraphQL data.
Apollo iOS generates operation objects for your GraphQL operations, providing a type-safe way to construct operations and provide values for input variables in your Swift code.
Each generated operation contains a set of robust, strongly-typed models for its response. These generated models help you access your GraphQL response data in a type-safe and flexible format. You don't need to deal with parsing JSON responses or passing around dictionaries of values that require manual casting. You also don't need to write model types yourself, because models are generated from the GraphQL operations you define.
Because generated response models are operation-specific, they include properties only for the GraphQL fields included in their corresponding operation. This means you can rely on the Swift type checker to flag data access errors at compile time.
In addition to operation-specific models, Apollo iOS generates models for your GraphQL fragments. Using these fragment models alongside the operation-specific models enables you to share common models across multiple GraphQL operations.
Models are also generated for your schema's input types, enums, and custom scalars, all of which improve safety and speed when working with GraphQL data in pure Swift code.
Apollo iOS normalizes operation results to build a client-side cache of your data, which is updated with every operation you execute. Our normalized cache is built specificially with GraphQL in mind. It de-duplicates GraphQL response data, merging data from separate GraphQL operations into normalized objects so that it can stay up-to-date with your backend with as few operations as possible.
The normalization of objects across operations means the cache acts as a source of truth for your graph, enabling your application to react to changes as they're fetched.
To learn more about the normalization process, see our blog post Demystifying Cache Normalization.
Apollo iOS provides a networking API that helps you execute GraphQL operations against any spec-compliant GraphQL server. The GraphQL execution pipeline automatically caches your response data in the normalized cache and returns the response data using your generated, type-safe operation models.
Apollo iOS handles all the heavy lifting of networking with GraphQL, including:
- Construction of spec-compliant GraphQL network requests
- Checking for existing data in the normalized cache
- Network transport execution
- Parsing and validation of GraphQL network responses
- Exposing GraphQL errors as type-safe Swift errors
- Writing response data to the normalized cache
- Mapping response data onto type-safe, generated operation models
Apollo Kotlin is a GraphQL client for native Android apps written in Java and Kotlin. It offers Kotlin Multi-Platform integration as well.