Apollo iOS 1.7 migration guide

From 1.6 to 1.7


This guide describes the process of migrating your code from version 1.6 to version 1.7 of Apollo iOS. Please follow the relevant migration guides if you're on a version other than 1.6.

Affected Users

Version 1.7 is a minor version bump, and will require no migration for most users.

  • For users using the Apollo Codegen CLI to run their code generation, this version will be a seamless upgrade with no changes to your existing code required.

  • For users that are using the ApolloCodegenLib directly to run code generation from a Swift executable, a simple migration is required.

While we strive to make the upgrade path for minor versions seamless, these improvements could not be made without requiring this migration. For those users affected, follow this migration guide to update to 1.7.

ApolloCodegenLib now uses Swift Concurrency

To improve the performance of the code generation, the ApolloCodegenLib now uses async/await. Code generation is now parallelized and should complete much faster for users with a large number of GraphQL files.

This means that the entry point function, ApolloCodegen.build(with configuration:) is now an async function. You will need to make your call sites into this function use async/await. In most cases, this requires minimal code changes.

Using async/await in a Swift executable

This migration guide will provide an example of how to migrate a Swift executable target that uses swift-argument-parser. While you can use the ApolloCodegenLib and build a Swift executable without it, swift-argument-parser usage is recommended.

To migrate your code you will need to make a few changes:

  1. Change your ParsableCommand to an AsyncParseableCommand

  2. Make your run() function async

  3. await on the ApolloCodegen.build(with configuration:) function call

Consider the following example executable:

Swift
v1.6 CustomCodegenScript.swift
1import Foundation
2import ApolloCodegenLib
3import ArgumentParser
4
5@main
6struct CustomCodegenScript: ParsableCommand {
7    func run() throws {
8      let codegenConfiguration = ApolloCodegenConfiguration(
9        schemaNamespace: "MySchema",
10        input: ApolloCodegenConfiguration.FileInput(
11          schemaPath: "./myschema.graphqls",
12          operationSearchPaths: ["./GraphQLFiles/**/*.graphql"]
13        ),
14        output: ApolloCodegenConfiguration.FileOutput(
15          schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
16            path: ./Generated/Schema,
17            moduleType: .swiftPackageManager
18          ),
19          operations: .inSchemaModule
20        )
21      )
22
23      try ApolloCodegen.build(with: codegenConfiguration)
24    }
25}

You can make this executable command support Swift Concurrency by changing it to:

Swift
v1.7 CustomCodegenScript.swift
1import Foundation
2import ApolloCodegenLib
3import ArgumentParser
4
5@main
6struct CustomCodegenScript: AsyncParsableCommand {
7    func run() async throws {
8      let codegenConfiguration = ApolloCodegenConfiguration(
9        schemaNamespace: "MySchema",
10        input: ApolloCodegenConfiguration.FileInput(
11          schemaPath: "./myschema.graphqls",
12          operationSearchPaths: ["./GraphQLFiles/**/*.graphql"]
13        ),
14        output: ApolloCodegenConfiguration.FileOutput(
15          schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
16            path: ./Generated/Schema,
17            moduleType: .swiftPackageManager
18          ),
19          operations: .inSchemaModule
20        )
21      )
22
23      try await ApolloCodegen.build(with: codegenConfiguration)
24    }
25}

Replace use of main.swift with @main

Notice in the example above, the command struct uses the @main annotation. Some projects may instead use a main.swift file in which they run a root command.

Swift
v1.6 main.swift
1import Foundation
2import ApolloCodegenLib
3import ArgumentParser
4
5struct CustomCodegenScript: ParsableCommand {
6    func run() throws {
7      let codegenConfiguration = ApolloCodegenConfiguration(
8        schemaNamespace: "MySchema",
9        input: ApolloCodegenConfiguration.FileInput(
10          schemaPath: "./myschema.graphqls",
11          operationSearchPaths: ["./GraphQLFiles/**/*.graphql"]
12        ),
13        output: ApolloCodegenConfiguration.FileOutput(
14          schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
15            path: ./Generated/Schema,
16            moduleType: .swiftPackageManager
17          ),
18          operations: .inSchemaModule
19        )
20      )
21
22      try ApolloCodegen.build(with: codegenConfiguration)
23    }
24}
25
26CustomCodegenScript.main()

main.swift file usage is not compatible with AsyncParsableCommand. You will need to:

  1. Change the name of the main.swift file to the the name of your command, for example: CustomCodegenScript.swift

  2. Add the @main annotation to the root command of your executable

  3. Remove the call to your script's main() function.

The run() function of the command marked with @main will automatically be called when you run your Swift executable target.

Feedback

Edit on GitHub

Forums