2. Add the GraphQL Schema


This tutorial uses a modified version of the GraphQL server you build as part of the Apollo full-stack tutorial. You can visit that server's Apollo Studio Sandbox Explorer to explore its schema without needing to be logged in:

The Sandbox query explorer

You'll know that this Sandbox instance is pointed at our server because its URL, https://apollo-fullstack-tutorial.herokuapp.com/graphql, is in the box at the top left of the page. If Sandbox is properly connected, you'll see a green dot:

A closeup of the URL box with a dot indicating it's connected

The schema defines which GraphQL operations your server can execute. At the top left, click the schema icon to get an overview of your schema:

The schema icon to click

In the Reference tab, you can now see a list of all of the things available to you as a consumer of this API, along with available fields on all objects:

Apollo sandbox showing the schema reference

Setup Codegen CLI

The Apollo iOS SPM package includes the Codegen CLI as an executable target. This ensures you always have a valid CLI version for your Apollo iOS version.

To simplify accessing the Codegen CLI, you can run the included InstallCLI SPM plugin.

This plugin downloads the matching version of the CLI for the library version you are using.

If you use Swift packages through Xcode, right-click your project in the Xcode file explorer and select the Install CLI plugin command. A dialog prompts you to grant the plugin write access to your project directory and network access to download the CLI tool.

Where to find the SPM plugin commands in Xcode

After the plugin downloads, it's unpacked to your project root. You can now run the CLI from the command line with ./apollo-ios-cli.

Note: Xcode 14.3 has a bug where the Install CLI plugin command does not show up in the menu when right-clicking on your project which is being tracked here. If you experience this issue an alternative is to use another version of Xcode, or follow the instructions to get a pre-built binary of the CLI on the Codegen CLI page.

Create your Codegen Configuration

Next we need to setup our codegen configuration file. To do this run the following command in Terminal from project directory:

Bash
1./apollo-ios-cli init --schema-namespace RocketReserverAPI --module-type swiftPackage

This generates a basic apollo-codegen-config.json file for our project.

Download your server's schema

Next we need to download the schema for our project to use. To do so, first we need to update our apollo-codegen-config.json to include a schemeDownloadConfiguration. Add the following JSON to the end of the config file after the output object:

JSON
1"schemaDownloadConfiguration": {
2    "downloadMethod": {
3        "introspection": {
4            "endpointURL": "https://apollo-fullstack-tutorial.herokuapp.com/graphql",
5            "httpMethod": {
6                "POST": {}
7            },
8            "includeDeprecatedInputValues": false,
9            "outputFormat": "SDL"
10        }
11    },
12    "downloadTimeout": 60,
13    "headers": [],
14    "outputPath": "./graphql/schema.graphqls"
15}

For more information about downloading schemas, see the Downloading a Schema documentation.

Now that we have updated our config, we can download the schema by running the following command in Terminal:

Bash
1./apollo-ios-cli fetch-schema

After running this command you should see a graphql folder in your project directory containing a schema.graphqls file.

In the next step you will write your first query.

Feedback

Edit on GitHub

Ask Community