Skip to content

Commit

Permalink
Disable Fragment Field Merging (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev authored and gh-action-runner committed Aug 14, 2024
1 parent 269760c commit cbe61c9
Show file tree
Hide file tree
Showing 32 changed files with 4,609 additions and 343 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ extension IR.NamedFragment {
inclusionConditions: nil,
givenAllTypesInSchema: .init([type], schemaRootTypes: .mock())
)
],
isUserDefined: true
]
)
let rootEntityField = IR.EntityField.init(
rootField,
Expand Down Expand Up @@ -111,8 +110,7 @@ extension IR.Operation {
forType: .mock(),
inclusionConditions: nil,
givenAllTypesInSchema: .init([], schemaRootTypes: .mock()))
],
isUserDefined: true
]
)
let rootField = IR.EntityField(
.mock(),
Expand Down
21 changes: 16 additions & 5 deletions Tests/ApolloCodegenInternalTestHelpers/IRBuilderTestWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,30 @@ public class IRBuilderTestWrapper {
}

public func build(
operation operationDefinition: CompilationResult.OperationDefinition
operation operationDefinition: CompilationResult.OperationDefinition,
mergingStrategy: MergedSelections.MergingStrategy = .all
) async -> IRTestWrapper<IR.Operation> {
let operation = await irBuilder.build(operation: operationDefinition)
return IRTestWrapper(
irObject: operation,
computedSelectionSetCache: .init(entityStorage: operation.entityStorage)
computedSelectionSetCache: .init(
mergingStrategy: mergingStrategy,
entityStorage: operation.entityStorage
)
)
}

public func build(
fragment fragmentDefinition: CompilationResult.FragmentDefinition
fragment fragmentDefinition: CompilationResult.FragmentDefinition,
mergingStrategy: MergedSelections.MergingStrategy = .all
) async -> IRTestWrapper<IR.NamedFragment> {
let fragment = await irBuilder.build(fragment: fragmentDefinition)
return IRTestWrapper(
irObject: fragment,
computedSelectionSetCache: .init(entityStorage: fragment.entityStorage)
computedSelectionSetCache: .init(
mergingStrategy: mergingStrategy,
entityStorage: fragment.entityStorage
)
)
}

Expand All @@ -58,7 +66,10 @@ public class IRBuilderTestWrapper {

return IRTestWrapper(
irObject: fragment,
computedSelectionSetCache: .init(entityStorage: fragment.entityStorage)
computedSelectionSetCache: .init(
mergingStrategy: .all,
entityStorage: fragment.entityStorage
)
)
}
}
Expand Down
44 changes: 30 additions & 14 deletions Tests/ApolloCodegenInternalTestHelpers/IRTestWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import OrderedCollections
///
/// This wrapper provides the subscripts for accessing child selections by automatically computing and storing the `ComputedSelectionSet` results as they are accessed in unit tests.
///
/// `IRTestWrapper` types should never be initialized directly. They should be created using an
/// - Warning: `IRTestWrapper` types should never be initialized directly. They should be created using an
/// `IRBuilderTestWrapper`.
@dynamicMemberLookup
public class IRTestWrapper<T: CustomDebugStringConvertible>: CustomDebugStringConvertible {
public let irObject: T
let computedSelectionSetCache: ComputedSelectionSetCache

public var mergingStrategy: MergedSelections.MergingStrategy {
computedSelectionSetCache.mergingStrategy
}

init(
irObject: T,
computedSelectionSetCache: ComputedSelectionSetCache
Expand Down Expand Up @@ -73,11 +77,9 @@ public class SelectionSetTestWrapper: IRTestWrapper<IR.SelectionSet> {
)
}

override func childSelectionSet(
with conditions: ScopeCondition
) -> SelectionSetTestWrapper? {
override func childSelectionSet(with conditions: ScopeCondition) -> SelectionSetTestWrapper? {
self.computed.childSelectionSet(
with: conditions,
with: conditions,
computedSelectionSetCache: computedSelectionSetCache
)
}
Expand All @@ -92,7 +94,9 @@ public class SelectionSetTestWrapper: IRTestWrapper<IR.SelectionSet> {
// MARK: -
extension IRTestWrapper {

public subscript(as typeCase: String) -> SelectionSetTestWrapper? {
public subscript(
as typeCase: String
) -> SelectionSetTestWrapper? {
guard let scope = self.scopeCondition(type: typeCase, conditions: nil) else {
return nil
}
Expand Down Expand Up @@ -203,7 +207,10 @@ extension IRTestWrapper<IR.NamedFragmentSpread> {
public var rootField: IRTestWrapper<IR.Field> {
return IRTestWrapper<IR.Field>(
irObject: irObject.fragment.rootField,
computedSelectionSetCache: .init(entityStorage: irObject.fragment.entityStorage)
computedSelectionSetCache: .init(
mergingStrategy: self.mergingStrategy,
entityStorage: irObject.fragment.entityStorage
)
)
}

Expand Down Expand Up @@ -257,10 +264,13 @@ extension SelectionSetTestWrapper {
}

public subscript(fragment fragment: String) -> IRTestWrapper<IR.NamedFragmentSpread>? {
IRTestWrapper<IR.NamedFragmentSpread>(
irObject:
computed.direct?.namedFragments[fragment] ?? computed.merged.namedFragments[fragment],
computedSelectionSetCache: computedSelectionSetCache
guard let fragment = computed.direct?.namedFragments[fragment] ?? computed.merged.namedFragments[fragment] else { return nil }
return IRTestWrapper<IR.NamedFragmentSpread>(
irObject: fragment,
computedSelectionSetCache: .init(
mergingStrategy: self.mergingStrategy,
entityStorage: fragment.fragment.entityStorage
)
)
}
}
Expand All @@ -269,20 +279,26 @@ extension SelectionSetTestWrapper {

class ComputedSelectionSetCache {
private var selectionSets: [SelectionSet.TypeInfo: ComputedSelectionSet] = [:]
public let mergingStrategy: MergedSelections.MergingStrategy
public let entityStorage: DefinitionEntityStorage

init(entityStorage: DefinitionEntityStorage) {
init(
mergingStrategy: MergedSelections.MergingStrategy,
entityStorage: DefinitionEntityStorage
) {
self.mergingStrategy = mergingStrategy
self.entityStorage = entityStorage
}

func computed(for selectionSet: SelectionSet) -> ComputedSelectionSet{
func computed(for selectionSet: SelectionSet) -> ComputedSelectionSet {
if let selectionSet = selectionSets[selectionSet.typeInfo] {
return selectionSet
}

let selectionSet = ComputedSelectionSet.Builder(
directSelections: selectionSet.selections?.readOnlyView,
typeInfo: selectionSet.typeInfo,
typeInfo: selectionSet.typeInfo,
mergingStrategy: mergingStrategy,
entityStorage: entityStorage
).build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extension ApolloCodegenConfiguration {
public static func mock(
_ moduleType: ApolloCodegenConfiguration.SchemaTypesFileOutput.ModuleType,
options: ApolloCodegenConfiguration.OutputOptions = .init(),
experimentalFeatures: ExperimentalFeatures = .init(),
schemaNamespace: String = "TestSchema",
to path: String = "MockModulePath"
) -> Self {
Expand All @@ -42,7 +43,8 @@ extension ApolloCodegenConfiguration {
output: .init(
schemaTypes: .init(path: path, moduleType: moduleType)
),
options: options
options: options,
experimentalFeatures: experimentalFeatures
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ extension ComputedSelectionSet: ScopedChildSelectionSetAccessible {
with conditions: IR.ScopeCondition,
computedSelectionSetCache: ComputedSelectionSetCache
) -> SelectionSetTestWrapper? {
let selectionSet = direct?.inlineFragments[conditions]?.selectionSet ??
merged.inlineFragments[conditions]?.selectionSet
let selectionSet =
direct?
.inlineFragments[conditions]?
.selectionSet ??
merged
.inlineFragments[conditions]?
.selectionSet

return SelectionSetTestWrapper(
irObject: selectionSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ final class AnimalKingdomIRCreationTests: XCTestCase {
try await buildOperation()

// when
let actual = rootSelectionSet.computed.merged
let actual = self.rootSelectionSet.computed.merged

// then
expect(actual).to(beEmpty())
Expand Down
Loading

0 comments on commit cbe61c9

Please sign in to comment.