Skip to content

Commit

Permalink
Prevent merging of fragment fields into selection tree if fragment fi…
Browse files Browse the repository at this point in the history
…eld merging is disabled (apollographql/apollo-ios-dev#571)
  • Loading branch information
AnthonyMDev authored and gh-action-runner committed Jan 21, 2025
1 parent 4c74877 commit 4bc820e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
13 changes: 11 additions & 2 deletions Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,16 @@ public class ApolloCodegen {
ir: IRBuilder,
fileManager: ApolloFileManager
) async throws -> NonFatalErrors {
let mergeNamedFragmentFields = config.experimentalFeatures.fieldMerging.options
.contains(.namedFragments)

return try await nonFatalErrorCollectingTaskGroup() { group in
for fragment in fragments {
group.addTask {
let irFragment = await ir.build(fragment: fragment)
let irFragment = await ir.build(
fragment: fragment,
mergingNamedFragmentFields: mergeNamedFragmentFields
)

let errors = try await FragmentFileGenerator(irFragment: irFragment, config: self.config)
.generate(forConfig: self.config, fileManager: fileManager)
Expand All @@ -310,7 +316,10 @@ public class ApolloCodegen {
group.addTask {
async let identifier = self.operationIdentifierFactory.identifier(for: operation)

let irOperation = await ir.build(operation: operation)
let irOperation = await ir.build(
operation: operation,
mergingNamedFragmentFields: mergeNamedFragmentFields
)

let errors = try await OperationFileGenerator(
irOperation: irOperation,
Expand Down
26 changes: 20 additions & 6 deletions Sources/IR/IR+RootFieldBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,34 @@ class RootFieldBuilder {
static func buildRootEntityField(
forRootField rootField: CompilationResult.Field,
onRootEntity rootEntity: Entity,
inIR ir: IRBuilder
inIR ir: IRBuilder,
mergingNamedFragmentFields: Bool
) async -> Result {
return await RootFieldBuilder(ir: ir, rootEntity: rootEntity)
.build(rootField: rootField)
return await RootFieldBuilder(
ir: ir,
rootEntity: rootEntity,
mergeInNamedFragmentFields: mergingNamedFragmentFields
)
.build(rootField: rootField)
}

private let ir: IRBuilder
private let rootEntity: Entity
private let entityStorage: DefinitionEntityStorage
private let mergeInNamedFragmentFields: Bool
private var referencedFragments: ReferencedFragments = []
@IsEverTrue private var containsDeferredFragment: Bool

private var schema: Schema { ir.schema }

private init(ir: IRBuilder, rootEntity: Entity) {
private init(
ir: IRBuilder,
rootEntity: Entity,
mergeInNamedFragmentFields: Bool
) {
self.ir = ir
self.rootEntity = rootEntity
self.mergeInNamedFragmentFields = mergeInNamedFragmentFields
self.entityStorage = DefinitionEntityStorage(rootEntity: rootEntity)
}

Expand Down Expand Up @@ -398,7 +409,10 @@ class RootFieldBuilder {
spreadIntoParentWithTypePath parentTypeInfo: SelectionSet.TypeInfo,
deferCondition: CompilationResult.DeferCondition? = nil
) async -> NamedFragmentSpread {
let fragment = await ir.build(fragment: fragmentSpread.fragment)
let fragment = await ir.build(
fragment: fragmentSpread.fragment,
mergingNamedFragmentFields: self.mergeInNamedFragmentFields
)
referencedFragments.append(fragment)
referencedFragments.append(contentsOf: fragment.referencedFragments)

Expand All @@ -425,7 +439,7 @@ class RootFieldBuilder {
inclusionConditions: AnyOf(scope.conditions)
)

if fragmentSpread.typeInfo.deferCondition == nil {
if mergeInNamedFragmentFields && fragmentSpread.typeInfo.deferCondition == nil {
mergeAllSelectionsIntoEntitySelectionTrees(from: fragmentSpread)
}

Expand Down
12 changes: 8 additions & 4 deletions Sources/IR/IRBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class IRBuilder {
}

public func build(
operation operationDefinition: CompilationResult.OperationDefinition
operation operationDefinition: CompilationResult.OperationDefinition,
mergingNamedFragmentFields: Bool = true
) async -> Operation {
let rootField = CompilationResult.Field(
name: operationDefinition.operationType.rawValue,
Expand All @@ -38,7 +39,8 @@ public class IRBuilder {
let result = await RootFieldBuilder.buildRootEntityField(
forRootField: rootField,
onRootEntity: rootEntity,
inIR: self
inIR: self,
mergingNamedFragmentFields: mergingNamedFragmentFields
)

return Operation(
Expand Down Expand Up @@ -86,7 +88,8 @@ public class IRBuilder {
}

public func build(
fragment fragmentDefinition: CompilationResult.FragmentDefinition
fragment fragmentDefinition: CompilationResult.FragmentDefinition,
mergingNamedFragmentFields: Bool = true
) async -> NamedFragment {
await builtFragmentStorage.getFragment(named: fragmentDefinition.name) {
let rootField = CompilationResult.Field(
Expand All @@ -102,7 +105,8 @@ public class IRBuilder {
let result = await RootFieldBuilder.buildRootEntityField(
forRootField: rootField,
onRootEntity: rootEntity,
inIR: self
inIR: self,
mergingNamedFragmentFields: mergingNamedFragmentFields
)

return NamedFragment(
Expand Down

0 comments on commit 4bc820e

Please sign in to comment.