Docs
Try Apollo Studio

Get started with Apollo iOS codegen


This short tutorial guides you through the basic concepts of code generation using the Apollo iOS client. It's intended to introduce you the core capabilities of code generation and will walk you through the manual process to generate code using a GraphQL schema and operation. For more details on code generation please refer to the Code generation documentation

Step 1: Setup

To start this tutorial, first create a new directory to work in. Then use Terminal to go into that new directory.

Step 2: Download a GraphQL schema

We'll be using the StarWars GraphQL API in this guide. Use Apollo Studio to view the schema.

Where to download a schema from Apollo Studio

On the right-hand side in Apollo Studio find the drop-down to download the schema, shown in the above screenshot. Apollo Studio supports both JSON or Raw formats. For this tutorial select Raw which downloads the GraphQL schema in the Schema Definition Language (SDL) format.

Clicking Raw will download a file named star-wars-swapi@current.graphql.

Step 3: Move the downloaded schema into your project

Download or move the already downloaded star-wars-swapi@current.graphql file into the directory you created in Step 1.

Rename the file extension from .graphql to .graphqls. Note the s on the end signifying a GraphQL schema file.

Step 4: Create a GraphQL operation

You'll also need to create a GraphQL operation file because the code generation engine requires both a schema and at least one operation to generate code.

We'll be using the allFilms Query operation as an example. Clicking this link will open Explorer in Apollo Studio where you can select the fields you would like to be fetched. Alternatively, the query text below already has fields selected.

Copy the query text and create a new file named AllFilmsQuery.graphql in the directory you created in Step 1. Paste this query into the new file.

query Query {
allFilms {
films {
title
director
releaseDate
speciesConnection {
species {
name
classification
homeworld {
name
}
}
}
}
}
}

Step 5: Download the CLI

Browse to the list of Apollo iOS releases in GitHub and find the latest release. In the Assets list for each release will be a pre-built CLI binary called apollo-ios-cli.tar.gz

Download and unzip this file then move the apollo-ios-cli binary file into the directory you created in Step 1.

For more information on the different ways to install the CLI, see the codegen CLI documentation.

Step 6: Create a codegen configuration

Run the following command:

./apollo-ios-cli init --schema-name StarWarsAPI --module-type swiftPackageManager

The CLI will create a configuration file named apollo-codegen-configuration.json, pre-filled with default values. The file should look similar to this:

{
"schemaName" : "StarWarsAPI",
"input" : {
"operationSearchPaths" : [
"**/*.graphql"
],
"schemaSearchPaths" : [
"**/*.graphqls"
]
},
"output" : {
"testMocks" : {
"none" : {
}
},
"schemaTypes" : {
"path" : "./StarWarsAPI",
"moduleType" : {
"swiftPackageManager" : {
}
}
},
"operations" : {
"inSchemaModule" : {
}
}
}
}

Step 7: Generate code

Now you are ready to generate some code! Run the following command:

./apollo-ios-cli generate

you should see a new file created named Query.graphql.swift, that looks similar to this:

And that is it! You can also review all the additional generated files to get a deeper understanding of how code generation works using the Apollo iOS client.

Edit on GitHub
Previous
Get Started
Next
Introduction