Docs
Launch GraphOS Studio

Getting Started with Apollo iOS


Follow the steps below to add to your app:

1. Install the Apollo frameworks

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

2. Add a schema file to your target directory

For to generate models for your , you need a local copy of your 's schema.

See Downloading a schema for more details.

3. Create .graphql files for your GraphQL operations

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

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

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

Whenever you make changes to your 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 .

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 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 called HeroName:

query HeroName {
hero {
name
}
}

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
Introduction
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company