@@ -299,34 +299,68 @@ public class ApolloCodegen {
299
299
let mergeNamedFragmentFields = config. experimentalFeatures. fieldMerging. options
300
300
. contains ( . namedFragments)
301
301
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
+
302
324
return try await nonFatalErrorCollectingTaskGroup ( ) { group in
303
325
for fragment in fragments {
326
+ let fragmentConfig = fragment. isLocalCacheMutation ? cacheMutationContext : self . config
327
+
304
328
group. addTask {
305
329
let irFragment = await ir. build (
306
330
fragment: fragment,
307
- mergingNamedFragmentFields: mergeNamedFragmentFields
331
+ mergingNamedFragmentFields: fragment . isLocalCacheMutation ? true : mergeNamedFragmentFields
308
332
)
309
333
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
+ )
312
341
return ( irFragment. name, errors)
313
342
}
314
343
}
315
344
316
345
for operation in operations {
346
+ let operationConfig = operation. isLocalCacheMutation ? cacheMutationContext : self . config
347
+
317
348
group. addTask {
318
349
async let identifier = self . operationIdentifierFactory. identifier ( for: operation)
319
350
320
351
let irOperation = await ir. build (
321
352
operation: operation,
322
- mergingNamedFragmentFields: mergeNamedFragmentFields
353
+ mergingNamedFragmentFields: operation . isLocalCacheMutation ? true : mergeNamedFragmentFields
323
354
)
324
355
325
356
let errors = try await OperationFileGenerator (
326
357
irOperation: irOperation,
327
358
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
+ )
330
364
return ( irOperation. name, errors)
331
365
}
332
366
}
0 commit comments