4. Running code generation
Now that we have both a schema and a query file, it's time to run code generation. To run code generation run the following command in Terminal from your project directory:
1./apollo-ios-cli generate
You should now see a newRocketReserverAPI
folder in your project directory which contains the Swift package containing your generated source code.
Add the generated SPM package to your project
With the code generated, next we need to add the generated SPM package to the project.
The default configuration for code generation creates a new Swift package for the schema module and operations. For more information on code generation such as differentmodule types you can generate ordifferent ways to run code generation see the documentation.
In Xcode go toFile > Add Packages..., in the Add Package dialog selectAdd Local...
Select the
RocketReserverAPI
folder in the file dialog, then clickAdd Package
You should now see the
RocketReserverAPI
package included in your project
Next, in Xcode select the project, then the
RocketReserver
target, and go toBuild Phases. Under theLink Binary With Libraries section click the+ sign
In the dialog that pops up, select the
RocketReserverAPI
library and clickAdd
Examine the generated code
In the Xcode project hierarchy, navigate toRocketReserver/Packages/RocketReseverAPI/Sources/Operations/Queries/LaunchListQuery.graphql.swift
. It defines a root class,LaunchListQuery
, with many nested structs below it. If you compare the structs to the JSON data returned in Sandbox Explorer, you see that the structure matches. These structs include properties only for the fields that your query requests.
Try commenting out theid
property inLaunchList.graphql
using a#
, saving, then running code generation again. When the code generation completes, the innermostLaunch
now only includes the built-in__typename
and the requestedsite
property.
Uncommentid
inLaunchList.graphql
and re-run code generation to restore the property.
Now that you've generated code and had a chance to see what's in there, it's time to get everything working end to end! Next, you willExecute your first query.