@@ -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 }
0 commit comments