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:
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:
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:
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:
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:
Select the
xcodeproj
file in the Project Navigator, and then select theRocketReserver
application target:A list of tabs appears. Select the Build Phases tab:
Click the
+
button above the list of existing phases and select New Run Script Phase:This adds a new Run Script build phase to the bottom of your list of build phases.
Drag the newly created phase up between "Dependencies" and "Compile Sources":
Double-click the name of the build phase to rename it to Apollo:
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.
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:Text1"${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 aschema.json
file at the same level of your project as theAppDelegate.swift
file.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 downloadedschema.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: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.