Skip to content

Commit f9dc31a

Browse files
gh-action-runnergh-action-runner
authored andcommitted
Squashed 'apollo-ios-codegen/' changes from 7b4652c1..1b1661ba
1b1661ba fix: Local cache mutation generation when field merging disabled (#654) git-subtree-dir: apollo-ios-codegen git-subtree-split: 1b1661ba166f6cbb857c003d016e5891f4a6c554
1 parent 19f4d62 commit f9dc31a

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

Sources/ApolloCodegenLib/ApolloCodegen.swift

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,34 +299,68 @@ public class ApolloCodegen {
299299
let mergeNamedFragmentFields = config.experimentalFeatures.fieldMerging.options
300300
.contains(.namedFragments)
301301

302+
/// A `ConfigurationContext` to use when generated local cache mutations.
303+
///
304+
/// Local cache mutations require some codegen options to be overridden to generate valid objects.
305+
/// This context overrides only the necessary properties, copying all other values from the user-provided `context`.
306+
lazy var cacheMutationContext: ConfigurationContext = {
307+
ConfigurationContext(
308+
config: ApolloCodegenConfiguration(
309+
schemaNamespace: self.config.schemaNamespace,
310+
input: self.config.input,
311+
output: self.config.output,
312+
options: self.config.options,
313+
experimentalFeatures: ApolloCodegenConfiguration.ExperimentalFeatures(
314+
fieldMerging: .all,
315+
legacySafelistingCompatibleOperations: self.config.experimentalFeatures.legacySafelistingCompatibleOperations
316+
),
317+
schemaDownload: self.config.schemaDownload,
318+
operationManifest: self.config.operationManifest
319+
),
320+
rootURL: self.config.rootURL
321+
)
322+
}()
323+
302324
return try await nonFatalErrorCollectingTaskGroup() { group in
303325
for fragment in fragments {
326+
let fragmentConfig = fragment.isLocalCacheMutation ? cacheMutationContext : self.config
327+
304328
group.addTask {
305329
let irFragment = await ir.build(
306330
fragment: fragment,
307-
mergingNamedFragmentFields: mergeNamedFragmentFields
331+
mergingNamedFragmentFields: fragment.isLocalCacheMutation ? true : mergeNamedFragmentFields
308332
)
309333

310-
let errors = try await FragmentFileGenerator(irFragment: irFragment, config: self.config)
311-
.generate(forConfig: self.config, fileManager: fileManager)
334+
let errors = try await FragmentFileGenerator(
335+
irFragment: irFragment,
336+
config: fragmentConfig
337+
).generate(
338+
forConfig: fragmentConfig,
339+
fileManager: fileManager
340+
)
312341
return (irFragment.name, errors)
313342
}
314343
}
315344

316345
for operation in operations {
346+
let operationConfig = operation.isLocalCacheMutation ? cacheMutationContext : self.config
347+
317348
group.addTask {
318349
async let identifier = self.operationIdentifierFactory.identifier(for: operation)
319350

320351
let irOperation = await ir.build(
321352
operation: operation,
322-
mergingNamedFragmentFields: mergeNamedFragmentFields
353+
mergingNamedFragmentFields: operation.isLocalCacheMutation ? true : mergeNamedFragmentFields
323354
)
324355

325356
let errors = try await OperationFileGenerator(
326357
irOperation: irOperation,
327358
operationIdentifier: await identifier,
328-
config: self.config
329-
).generate(forConfig: self.config, fileManager: fileManager)
359+
config: operationConfig
360+
).generate(
361+
forConfig: operationConfig,
362+
fileManager: fileManager
363+
)
330364
return (irOperation.name, errors)
331365
}
332366
}

Sources/ApolloCodegenLib/CodegenConfiguration/ApolloCodegenConfiguration.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,30 +1434,35 @@ extension ApolloCodegenConfiguration.OperationsFileOutput {
14341434
extension ApolloCodegenConfiguration {
14351435
/// Determine whether the operations files are output to the schema types module.
14361436
func shouldGenerateSelectionSetInitializers(for operation: IR.Operation) -> Bool {
1437-
guard experimentalFeatures.fieldMerging == .all else { return false }
1438-
14391437
if operation.definition.isLocalCacheMutation {
14401438
return true
14411439

1442-
} else if options.selectionSetInitializers.contains(.operations) {
1443-
return true
1444-
14451440
} else {
1446-
return options.selectionSetInitializers.contains(definitionNamed: operation.definition.name)
1441+
guard experimentalFeatures.fieldMerging == .all else { return false }
1442+
1443+
if options.selectionSetInitializers.contains(.operations) {
1444+
return true
1445+
1446+
} else {
1447+
return options.selectionSetInitializers.contains(definitionNamed: operation.definition.name)
1448+
}
14471449
}
14481450
}
14491451

14501452
/// Determine whether the operations files are output to the schema types module.
14511453
func shouldGenerateSelectionSetInitializers(for fragment: IR.NamedFragment) -> Bool {
1452-
guard experimentalFeatures.fieldMerging == .all else { return false }
1453-
1454-
if options.selectionSetInitializers.contains(.namedFragments) { return true }
1455-
14561454
if fragment.definition.isLocalCacheMutation {
14571455
return true
1458-
}
14591456

1460-
return options.selectionSetInitializers.contains(definitionNamed: fragment.definition.name)
1457+
} else {
1458+
guard experimentalFeatures.fieldMerging == .all else { return false }
1459+
1460+
if options.selectionSetInitializers.contains(.namedFragments) {
1461+
return true
1462+
} else {
1463+
return options.selectionSetInitializers.contains(definitionNamed: fragment.definition.name)
1464+
}
1465+
}
14611466
}
14621467
}
14631468

0 commit comments

Comments
 (0)