Skip to content

Add nonisolated to generated code for Swift 6.2 MainActor default isolation#925

Merged
AnthonyMDev merged 4 commits intomainfrom
mainactor-isolation
Mar 27, 2026
Merged

Add nonisolated to generated code for Swift 6.2 MainActor default isolation#925
AnthonyMDev merged 4 commits intomainfrom
mainactor-isolation

Conversation

@AnthonyMDev
Copy link
Copy Markdown
Contributor

Summary

Closes apollographql/apollo-ios#3601

  • Adds a new markTypesNonisolated option to ApolloCodegenConfiguration.OutputOptions that emits the nonisolated keyword on all generated type declarations
  • Defaults to true when the codegen tool is compiled with Swift 6.2+ (#if compiler(>=6.2)), false otherwise
  • Users on older Swift versions see no change; users on Swift 6.2+ get MainActor-compatible generated code automatically
  • When enabled, generated types opt out of the module's default actor isolation, preventing compilation errors when the user's target sets SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor

Templates modified

OperationDefinitionTemplate, SelectionSetTemplate, FragmentTemplate, InputObjectTemplate, OneOfInputObjectTemplate, EnumTemplate, LocalCacheMutationDefinitionTemplate, SchemaMetadataTemplate, SchemaConfigurationTemplate, MockObjectTemplate, ObjectTemplate, InterfaceTemplate, UnionTemplate

Generated output (when enabled)

nonisolated public struct AllAnimalsQuery: GraphQLQuery {
  nonisolated public struct Data: AnimalKingdomAPI.SelectionSet {
    // ...
  }
}

Test plan

  • Template unit tests verify nonisolated is emitted when option is true and omitted when false
  • All regenerated test API code compiles (AnimalKingdomAPI, StarWarsAPI, GitHubAPI, SubscriptionAPI, UploadAPI)
  • TestCodeGenConfigurations build and pass with nonisolated annotations
  • Verify end-to-end with SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor on a test target

🤖 Generated with Claude Code

@netlify
Copy link
Copy Markdown

netlify Bot commented Mar 25, 2026

Deploy Preview for apollo-ios-docc canceled.

Name Link
🔨 Latest commit d42e545
🔍 Latest deploy log https://app.netlify.com/projects/apollo-ios-docc/deploys/69c58bf8f55ec900080b9a26

@apollo-librarian
Copy link
Copy Markdown

apollo-librarian Bot commented Mar 25, 2026

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: d6c22a49163f48cfda36560c
Build Logs: View logs


✅ AI Style Review — No Changes Detected

No MDX files were changed in this pull request.

Review Log: View detailed log

This review is AI-generated. Please use common sense when accepting these suggestions, as they may not always be accurate or appropriate for your specific context.

AnthonyMDev and others added 2 commits March 26, 2026 12:16
On Swift 6.2, the default for markTypesNonisolated is true, which
causes existing template tests to fail because they don't expect
the nonisolated keyword in output. Explicitly set false in all test
OutputOptions inits so existing tests maintain their expected output.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add "markTypesNonisolated" to expected JSON in codable encode/decode tests
- Add "markTypesNonisolated": true to SPM codable test JSON (default on Swift 6.2)
- Change nonisolatedModifier return type from TemplateString to String to prevent
  empty TemplateString from stripping indentation on disabled lines
- Fix nonisolated template test expectations (schemaDocumentation, atLine)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@AnthonyMDev AnthonyMDev merged commit 60c3945 into main Mar 27, 2026
21 checks passed
@AnthonyMDev AnthonyMDev deleted the mainactor-isolation branch March 27, 2026 18:04
BobaFetters pushed a commit to apollographql/apollo-ios that referenced this pull request Mar 27, 2026
BobaFetters pushed a commit that referenced this pull request Mar 27, 2026
1d9fce8aa Add nonisolated to generated code for Swift 6.2 MainActor default isolation (#925)

git-subtree-dir: apollo-ios
git-subtree-split: 1d9fce8aa1cd2ac9bc30c161f2640942612af8b3
BobaFetters pushed a commit that referenced this pull request Mar 27, 2026
…wift 6.2 MainActor default isolation

git-subtree-dir: apollo-ios
git-subtree-mainline: 0b2263b
git-subtree-split: 1d9fce8aa1cd2ac9bc30c161f2640942612af8b3
BobaFetters pushed a commit that referenced this pull request Mar 27, 2026
4a08171c2 Add nonisolated to generated code for Swift 6.2 MainActor default isolation (#925)

git-subtree-dir: apollo-ios-codegen
git-subtree-split: 4a08171c2d5c9fb5eabc6b1e8370fa481ad60184
BobaFetters pushed a commit that referenced this pull request Mar 27, 2026
…de for Swift 6.2 MainActor default isolation

git-subtree-dir: apollo-ios-codegen
git-subtree-mainline: 3786eb1
git-subtree-split: 4a08171c2d5c9fb5eabc6b1e8370fa481ad60184
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Support Swift 6.2 Default Actor Isolation - MainActor

1 participant