Join us for GraphQL Summit, October 10-12 in San Diego. Use promo code ODYSSEY for $400 off your pass.
Launch GraphOS Studio

Running code generation in Swift code

For most use cases, the Apollo iOS Codegen CLI is the recommended method for generating code and downloading your . The CLI uses the ApolloCodegenLib to execute these .

To enable advanced use, the ApolloCodegenLib can be included as a dependency of your own Swift projects. This enables you to use Swift scripting to perform s that cannot be done with the CLI. Calling into the ApolloCodegenLib directly allows you to integrate Apollo into your development process however you would like.

Installing ApolloCodegenLib

The ApolloCodegenLib is included as a product in the Apollo iOS SPM Package.

At this time, ApolloCodegenLib only supports macOS. You cannot run code generation scripts on other platforms.

To use it within your own Swift project, just include it as a dependency.

name: "MyApp",
dependencies: [
.product(name: "ApolloCodegenLib", package: "apollo-ios"),

Important: Because code generation is tied to its usage in the Apollo iOS SDK, you make sure you are using the ApolloCodegenLib from the same version of the package as the application that uses your generated code.


Once you've installed the ApolloCodegenLib dependency in your project, you can use it to:

Running code generation

To configure and run code generation using ApolloCodegenLib, create an ApolloCodegenConfiguration, then pass it to configuration:withRootURL:itemsToGenerate:) function.

By default, the Code Generation Engine will resolve all file paths in your ApolloCodegenConfiguration relative to the current working directory. The rootURL parameter can be used to provide a local file path URL of the root of the project that you want to run code generation on. This will resolve file paths in your configuration relative to the provided rootURL. The itemsToGenerate parameter allows you to customize what items will be generated. If you don't provide options this will default to [.code] which will just generate code files in the same way code generation worked prior to this option set being added in version 1.4.0.

import ApolloCodegenLib
let configuration = ApolloCodegenConfiguration(
schemaNamespace: "MyGraphAPI",
input: ApolloCodegenConfiguration.FileInput(
schemaSearchPaths: ["**/*.graphqls"],
operationSearchPaths: ["**/*.graphql"]
output: ApolloCodegenConfiguration.FileOutput(
schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
path: "./generated/schema/",
moduleType: .swiftPackageManager
operations: .inSchemaModule,
testMocks: .none
options: ApolloCodegenConfiguration.OutputOptions(
additionalInflectionRules: [
singularRegex: "animal",
replacementRegex: "animals"
queryStringLiteralFormat: .multiline,
deprecatedEnumCases: .include,
schemaDocumentation: .include,
apqs: .disabled,
cocoapodsCompatibleImportStatements: false,
warningsOnDeprecatedUsage: .include,
conversionStrategies: ApolloCodegenConfiguration.ConversionStrategies(
enumCases: .camelCase
pruneGeneratedFiles: true
try! configuration)

For more information on the configuring code generation, see the configuration documentation.

Downloading a schema

If you would like to download a GraphQL from you own Swift project, use the ApolloSchemaDownloader.

The downloader can be configured with an ApolloSchemaDownloadConfiguration.

import ApolloCodegenLib
let configuration = ApolloSchemaDownloadConfiguration(
using: .introspection(endpointURL: endpoint, outputFormat: .SDL),
outputPath: outputURL.path
try! ApolloSchemaDownloader.fetch(configuration: configuration)

For more information on fetching, see Downloading a schema.

Downloading a Schema
Fetching Data
Edit on GitHubEditForumsDiscord