Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent merging of fragment fields into selection tree if fragment field merging is disabled #571

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public class IRBuilderTestWrapper {
operation operationDefinition: CompilationResult.OperationDefinition,
mergingStrategy: MergedSelections.MergingStrategy = .all
) async -> IRTestWrapper<IR.Operation> {
let operation = await irBuilder.build(operation: operationDefinition)
let operation = await irBuilder.build(
operation: operationDefinition,
mergingNamedFragmentFields: mergingStrategy.contains(.namedFragments)
)
return IRTestWrapper(
irObject: operation,
computedSelectionSetCache: .init(
Expand All @@ -38,7 +41,10 @@ public class IRBuilderTestWrapper {
fragment fragmentDefinition: CompilationResult.FragmentDefinition,
mergingStrategy: MergedSelections.MergingStrategy = .all
) async -> IRTestWrapper<IR.NamedFragment> {
let fragment = await irBuilder.build(fragment: fragmentDefinition)
let fragment = await irBuilder.build(
fragment: fragmentDefinition,
mergingNamedFragmentFields: mergingStrategy.contains(.namedFragments)
)
return IRTestWrapper(
irObject: fragment,
computedSelectionSetCache: .init(
Expand Down
13 changes: 11 additions & 2 deletions apollo-ios-codegen/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 apollo-ios-codegen/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 apollo-ios-codegen/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
Loading