Docs
Try Apollo 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 operations, you need a local copy of your GraphQL server's schema.

See Downloading a schema for more details.

3. Create .graphql files for your GraphQL operations

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

GraphQL operation and fragment 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 operations and parse and cache operation responses.

Whenever you make changes to your GraphQL operation 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 schema 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 operations 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 operation 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.

Edit on GitHub
Previous
Introduction
Next
Code Generation