Any targets in your application that will use ApolloClient need to have a dependency on the Apollo product.
Package.swift
1
.target(
2
name:"MyApp",
3
dependencies:[
4
.product(name:"Apollo", package:"apollo-ios"),
5
]
6
)
Note: Targets that only use Apollo's generated models don't need to be linked to the Apollo product.
If using Swift Package Manager within an Xcode project or workspace, use the Xcode project configuration UI to add the Apollo iOS package.
1
Go to File > Add Packages...
2
In the dialog that appears, paste the URL of the Apollo iOS GitHub repo (https://github.com/apollographql/apollo-ios.git) into the search bar, then select the apollo-ios package that appears:
3
Select which version you want to use (see version history), then click Add Package.
Note: Xcode might not automatically select the latest version number, please check.
4
Select which packages you want to use. If you're getting started, we recommend selecting just the main Apollo library for now. You can always add other packages later if you need them.
Notes:
Do not select the Apollo-Dynamic product unless your project is configured to use dynamic linking of the Apollo iOS framework. Most projects do not need to link to this product.
Do not select the apollo-ios-cli package. This product is the CLI executable for code generation. If you link it to your project as a dependency it will cause build errors.
Apollo iOS generates code from the GraphQL queries and mutations defined in your target's files. To use Apollo iOS, you'll need to define at least one operation GraphQL operation.
GraphQL operation and fragment definitions traditionally have the file extension .graphql. The generated models will have the file extension .graphql.swift.
Apollo iOS code generation uses your .graphql files to generate API code that helps you execute GraphQL operations and parse and cache operation responses.
Whenever you make changes to your GraphQL operation definitions, you'll need to run the code generation engine to re-generate your GraphQL models.
The easiest way to do this is with the Codegen CLI provided with Apollo iOS.
For more advanced usage and configuration (including use with modularized projects), see Code Generation.
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 apollo-cli-install SPM plugin. This plugin builds the CLI and creates a symbolic link to the executable in your project root.
If using a Package.swift file, you can install the CLI by running:
1
swift package --allow-writing-to-package-directory apollo-cli-install
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 using ./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.
The Codegen CLI uses a JSON file to configure the code generation engine. You can use the Codegen CLI's init command to create this file with default values.
From your project's root directory, run the following command with your customized values:
${MySchemaName} provides a name for the namespace of your generated schema files.
${ModuleType} configures how your generated schema types are included in your project.
This is a crucial decision to make before configuring code generation. To determine the right option for your project, see Project Configuration.
To get started quickly, you can use the embeddedInTarget option.
Using embeddedInTarget, you must supply a target name using the --target-name command line option.
Running this command creates an apollo-codegen-config.json file.
By default, a directory containing your generated schema files is within a directory with the schema name you provided (i.e., MySchemaName). Your generated operation and fragment files are in a subfolder within the same directory.
If you created your target in an Xcode project or workspace, you'll need to manually add the generated files to your target.
Note: Because adding generated files to your Xcode targets must be done manually each time you generate new files, we highly recommend defining your project targets with SPM. Alternatively, you can generate your operations into the package that includes your schema files. For more information see the documentation for Code Generation Configuration.
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.
The Codegen CLI uses a JSON file to configure the code generation engine. You can use the Codegen CLI's init command to create this file with default values.
From your project's root directory, run the following command with your customized values:
${MySchemaName} provides a name for the namespace of your generated schema files.
${ModuleType} configures how your generated schema types are included in your project.
This is a crucial decision to make before configuring code generation. To determine the right option for your project, see Project Configuration.
To get started quickly, you can use the embeddedInTarget option.
Using embeddedInTarget, you must supply a target name using the --target-name command line option.
Running this command creates an apollo-codegen-config.json file.
By default, a directory containing your generated schema files is within a directory with the schema name you provided (i.e., MySchemaName). Your generated operation and fragment files are in a subfolder within the same directory.
If you created your target in an Xcode project or workspace, you'll need to manually add the generated files to your target.
Note: Because adding generated files to your Xcode targets must be done manually each time you generate new files, we highly recommend defining your project targets with SPM. Alternatively, you can generate your operations into the package that includes your schema files. For more information see the documentation for Code Generation Configuration.
If you use Cocoapods, Apollo iOS compiles the Codegen CLI into an executable shell application during pod install (located in Pods/Apollo/apollo-ios-cli).
After installing the Apollo iOS pod, you can run the Codegen CLI from the directory of your Podfile:
Note: If you are using :path in your Podfile to link to a local copy of Apollo iOS, the CLI will not be automatically available. You will need to manually build the Codegen CLI. See the CLI installation guide for directions on how to do that.
The Codegen CLI uses a JSON file to configure the code generation engine. You can use the Codegen CLI's init command to create this file with default values.
From your project's root directory, run the following command with your customized values:
${MySchemaName} provides a name for the namespace of your generated schema files.
${ModuleType} configures how your generated schema types are included in your project.
This is a crucial decision to make before configuring code generation. To determine the right option for your project, see Project Configuration.
To get started quickly, you can use the embeddedInTarget option.
Using embeddedInTarget, you must supply a target name using the --target-name command line option.
Running this command creates an apollo-codegen-config.json file.
By default, a directory containing your generated schema files is within a directory with the schema name you provided (i.e., MySchemaName). Your generated operation and fragment files are in a subfolder within the same directory.
If you created your target in an Xcode project or workspace, you'll need to manually add the generated files to your target.
Note: Because adding generated files to your Xcode targets must be done manually each time you generate new files, we highly recommend defining your project targets with SPM. Alternatively, you can generate your operations into the package that includes your schema files. For more information see the documentation for Code Generation Configuration.