You are viewing documentation for a previous version of this software.

Switch to the latest stable version.

2. Obtain your 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

Download your server's schema

The Apollo iOS SDK needs a local copy of your server's schema to generate code from it. To accomplish this, the Apollo CLI includes a schema:download command that enables you to fetch the schema from a GraphQL server.

To use the Apollo CLI from Xcode, add a Run Script build phase to your app:

  1. Select the xcodeproj file in the Project Navigator, and then select the RocketReserver application target:

    Selecting application target
  2. A list of tabs appears. Select the Build Phases tab:

    Build phases menu item
  3. Click the + button above the list of existing phases and select New Run Script Phase:

    Creating a new run script build phase

    This adds a new Run Script build phase to the bottom of your list of build phases.

  4. Drag the newly created phase up between "Dependencies" and "Compile Sources":

    Where to drag the run script
  5. Double-click the name of the build phase to rename it to Apollo:

    UI for renaming
  6. Expand the Apollo phase. Paste the Swift Package Manager Run Script from Add a code generation build step into the text area. This script uses your schema to generate the code that the Apollo iOS SDK uses to interact with your server.

  7. Before the script can generate code, it needs a local copy of your GraphQL server's schema. For now, using a #, comment out the last line of the script you pasted and add the following line below it:

    Text
    1"${SCRIPT_PATH}"/run-bundled-codegen.sh schema:download --endpoint="https://apollo-fullstack-tutorial.herokuapp.com/graphql"

    This line runs the Apollo CLI's schema:download command, which downloads the schema to a schema.json file at the same level of your project as the AppDelegate.swift file.

  8. Build your project to execute the script. In Finder, navigate to the folder that contains your AppDelegate.swift file. The folder should now include the downloaded schema.json file. Drag this file from Finder into Xcode. When Xcode offers to add the schema file, make sure all targets are unchecked to reduce the size of your application bundle:

    All targets unchecked in dialog

    You don't need to add the schema to any targets, because it is only used to generate code which is added to targets.

Your server's schema is now available locally. Next, you'll create your first operation against the schema.

Feedback

Edit on GitHub

Forums