Join us for GraphQL Summit, October 10-12 in San Diego. Use promo code ODYSSEY for $400 off your pass.
Docs
Launch GraphOS Studio

Getting Started with Apollo iOS


Follow the steps below to add Apollo iOS to your app:

1. Install the Apollo frameworks

You can add Apollo iOS into your project using Swift Package Manager or CocoaPods.

2. Add a schema file to your target directory

For Apollo iOS to generate models for your GraphQL s, you need a local copy of your GraphQL server's .

See Downloading a schema for more details.

3. Create .graphql files for your GraphQL operations

Apollo iOS generates code from the GraphQL queries and s defined in your target's files. To use Apollo iOS, you'll need to define at least one GraphQL operation.

GraphQL and definitions traditionally have the file extension .graphql. The generated models will have the file extension .graphql.swift.

See Defining operations for more details.

4. Setup and run code generation

Apollo iOS code generation uses your .graphql files to generate API code that helps you execute GraphQL s and parse and cache operation responses.

Whenever you make changes to your GraphQL definitions, you'll need to run the code generation engine to re-generate your GraphQL models.

The easiest way to do this is with the Codegen CLI provided with Apollo iOS.

For more advanced usage and configuration (including use with modularized projects), see Code Generation.

To use Apollo's code generation and downloader from within any Swift script or library, check out Running code generation in Swift code.

5. Create an ApolloClient

Before you can execute GraphQL s in your app, you need to initialize an ApolloClient instance.

import Foundation
import Apollo
let apolloClient = ApolloClient(url: URL(string: "http://localhost:4000/graphql")!)

See Creating a client for more details.

6. Fetch a query

ApolloClient can fetch your generated definitions, and return the response as a type-safe generated data model.

For example, if you define a query called HeroName:

query HeroName {
hero {
name
}
}

Apollo iOS will generate a HeroNameQuery class that you can construct and pass to ApolloClient.fetch(query:):

apolloClient.fetch(query: HeroNameQuery()) { result in
guard let data = try? result.get().data else { return }
print(data.hero.name) // Luke Skywalker
}

See Fetching data for more details.

Previous
Introduction
Next
Project Configuration
Edit on GitHubEditForumsDiscord