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 schema. The CLI uses the ApolloCodegenLib to execute these operation.

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 operations 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.

Swift
Package.swift
1.target(
2    name: "MyApp",
3    dependencies: [
4        .product(name: "ApolloCodegenLib", package: "apollo-ios-codegen"),
5    ]
6)

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.

Usage

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 ApolloCodegen.build(with 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.

Swift
1import ApolloCodegenLib
2
3let configuration = ApolloCodegenConfiguration(
4    schemaNamespace: "MyGraphAPI",
5    input: ApolloCodegenConfiguration.FileInput(
6		schemaSearchPaths: ["**/*.graphqls"],
7		operationSearchPaths: ["**/*.graphql"]
8	),
9    output: ApolloCodegenConfiguration.FileOutput(
10		schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
11			path: "./generated/schema/",
12			moduleType: .swiftPackageManager
13        ),
14		operations: .inSchemaModule,
15		testMocks: .none
16	),
17    options: ApolloCodegenConfiguration.OutputOptions(
18		additionalInflectionRules: [
19			.pluralization(
20				singularRegex: "animal",
21				replacementRegex: "animals"
22			)
23		],
24		queryStringLiteralFormat: .multiline,
25		deprecatedEnumCases: .include,
26		schemaDocumentation: .include,
27		apqs: .disabled,
28		cocoapodsCompatibleImportStatements: false,
29		warningsOnDeprecatedUsage: .include,
30		conversionStrategies: ApolloCodegenConfiguration.ConversionStrategies(
31			enumCases: .camelCase
32		),
33		pruneGeneratedFiles: true
34	)
35)
36
37try! ApolloCodegen.build(with: configuration)

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

Downloading a schema

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

The schema downloader can be configured with an ApolloSchemaDownloadConfiguration.

Swift
1import ApolloCodegenLib
2
3let configuration = ApolloSchemaDownloadConfiguration(
4    using: .introspection(endpointURL: endpoint, outputFormat: .SDL),
5    outputPath: outputURL.path
6)
7
8try! ApolloSchemaDownloader.fetch(configuration: configuration)

For more information on schema fetching, see Downloading a schema.

Feedback

Edit on GitHub

Forums