The Codegen CLI


The Codegen CLI provides a command line tool that streamlines the process of running code generation. The CLI can be ran manually from Terminal (or any other shell program) or can be called into from bash scripts.

The Codegen CLI has three primary commands:

  • Initialize: Initializes an apollo-codegen-configuration.json file that can be used to configure how the CLI generates code.

  • Fetch Schema: Fetches your GraphQL schema and writes it to a file. The schema is required in order to run code generation.

  • Generate: Runs the code generation engine using the configuration in your apollo-codegen-configuration.json file.

  • Generate Operation Manifest: Generates the operation manifest for persisted queries using the configuration in your apollo-codegen-configuration.json file.

For detailed usage documentation of these commands, see the Usage section.

Installation

When Apollo iOS is included as a dependency through Swift Package Manager (SPM), the CLI is built and packaged with the dependency automatically. This ensures you always have a valid version of the CLI for the version of Apollo iOS you are using and you never have to worry about installation or updates.

To learn how to run the Codegen CLI with your chosen package manager, open the appropriate section:

SPM with Package.swift
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:
Bash
1swift 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.
SPM with Xcode Project
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 downloads the matching version of the CLI for the library version you are using.If you use Swift packages through Xcode, right-click your project in the Xcode file explorer and select the Install CLI plugin command. A dialog prompts you to grant the plugin write access to your project directory and network access to download the CLI tool.