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:
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:
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 builds the CLI and creates a symbolic link to the executable in your project root.
If you use Swift packages through Xcode, you can right-click on your project in the Xcode file explorer, revealing an Install CLI plugin command. Selecting this command presents a dialog allowing you to grant the plugin "write" access to your project directory.
After the plugin installs, it creates a symbolic link to the Codegen CLI (named apollo-ios-cli
) in your project root folder. You can now run the CLI from the command line with ./apollo-ios-cli
.
Note: Because the
apollo-ios-cli
in your project root is only a symbolic link, it only works if the compiled CLI executable exists. This is generally located in your Xcode Derived Data or the.build
folder. If these are cleared, you can rerun the Install CLI plugin to rebuild the CLI executable.
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:
1./apollo-ios-cli init --schema-namespace RocketReserverAPI --module-type swiftPackageManager
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:
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:
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.