Launch GraphOS Studio

Running code generation in Swift code

For most use cases, the Codegen CLI is the recommended method for generating code and downloading your schema. 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 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 Codegen 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-codegen"),

Important: Because code generation is tied to its usage in the Apollo iOS SDK, make sure you are using the same versions of both the apollo-ios and apollo-ios-codegen packages.


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 from you own Swift project, use the ApolloSchemaDownloader.

The schema 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 schema fetching, see Downloading a schema.

Downloading a Schema
Code Generation Troubleshooting
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy