Docs
Launch GraphOS Studio

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

v1.6 CustomCodegenScript.swift
import Foundation
import ApolloCodegenLib
import ArgumentParser
@main
struct CustomCodegenScript: ParsableCommand {
func run() throws {
let codegenConfiguration = ApolloCodegenConfiguration(
schemaNamespace: "MySchema",
input: ApolloCodegenConfiguration.FileInput(
schemaPath: "./myschema.graphqls",
operationSearchPaths: ["./GraphQLFiles/**/*.graphql"]
),
output: ApolloCodegenConfiguration.FileOutput(
schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
path: ./Generated/Schema,
moduleType: .swiftPackageManager
),
operations: .inSchemaModule
)
)
try ApolloCodegen.build(with: codegenConfiguration)
}
}

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

v1.7 CustomCodegenScript.swift
import Foundation
import ApolloCodegenLib
import ArgumentParser
@main
struct CustomCodegenScript: AsyncParsableCommand {
func run() async throws {
let codegenConfiguration = ApolloCodegenConfiguration(
schemaNamespace: "MySchema",
input: ApolloCodegenConfiguration.FileInput(
schemaPath: "./myschema.graphqls",
operationSearchPaths: ["./GraphQLFiles/**/*.graphql"]
),
output: ApolloCodegenConfiguration.FileOutput(
schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
path: ./Generated/Schema,
moduleType: .swiftPackageManager
),
operations: .inSchemaModule
)
)
try await ApolloCodegen.build(with: codegenConfiguration)
}
}

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.

v1.6 main.swift
import Foundation
import ApolloCodegenLib
import ArgumentParser
struct CustomCodegenScript: ParsableCommand {
func run() throws {
let codegenConfiguration = ApolloCodegenConfiguration(
schemaNamespace: "MySchema",
input: ApolloCodegenConfiguration.FileInput(
schemaPath: "./myschema.graphqls",
operationSearchPaths: ["./GraphQLFiles/**/*.graphql"]
),
output: ApolloCodegenConfiguration.FileOutput(
schemaTypes: ApolloCodegenConfiguration.SchemaTypesFileOutput(
path: ./Generated/Schema,
moduleType: .swiftPackageManager
),
operations: .inSchemaModule
)
)
try ApolloCodegen.build(with: codegenConfiguration)
}
}
CustomCodegenScript.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.

Previous
v1.6
Next
Code Generation
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company