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

Fix: Added missing fulfilledFragments to generated initializers #3067

Merged
merged 4 commits into from
Jun 12, 2023
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
52 changes: 40 additions & 12 deletions Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,29 +483,27 @@ struct SelectionSetTemplate {
private func InitializerFulfilledFragments(
_ selectionSet: IR.SelectionSet
) -> TemplateString {
var fulfilledFragments: [String] = ["Self"]
var fulfilledFragments: OrderedSet<String> = []

var next = selectionSet.scopePath.last.value.scopePath.head
while next.next != nil {
defer { next = next.next.unsafelyUnwrapped }
var currentNode = Optional(selectionSet.scopePath.last.value.scopePath.head)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the renaming here from just using next

while let node = currentNode {
defer { currentNode = node.next }

let selectionSetName = SelectionSetNameGenerator.generatedSelectionSetName(
for: selectionSet,
to: next,
to: node,
format: .fullyQualified,
pluralizer: config.pluralizer
)

fulfilledFragments.append(selectionSetName)
}

let allFragments = IteratorSequence(selectionSet.selections.makeFragmentIterator())
for fragment in allFragments {
if let conditions = fragment.inclusionConditions,
!selectionSet.typeInfo.scope.matches(conditions) {
continue
}
fulfilledFragments.append(fragment.definition.name.firstUppercased)
for source in selectionSet.selections.merged.mergedSources {
fulfilledFragments
.append(contentsOf: source.generatedSelectionSetNamesOfFullfilledFragments(
pluralizer: config.pluralizer
))
}

return """
Expand Down Expand Up @@ -693,6 +691,34 @@ fileprivate extension IR.MergedSelections.MergedSource {
return selectionSetNameComponents.joined(separator: ".")
}

func generatedSelectionSetNamesOfFullfilledFragments(
pluralizer: Pluralizer
) -> [String] {
let entityRootNameInFragment = SelectionSetNameGenerator
.generatedSelectionSetName(
for: self,
to: typeInfo.scopePath.last.value.scopePath.head,
format: .fullyQualified,
pluralizer: pluralizer
)

var fulfilledFragments: [String] = [entityRootNameInFragment]

var selectionSetNameComponents: [String] = [entityRootNameInFragment]

var mergedFragmentEntityConditionPathNode = typeInfo.scopePath.last.value.scopePath.head
while let node = mergedFragmentEntityConditionPathNode.next {
defer {
mergedFragmentEntityConditionPathNode = node
}
selectionSetNameComponents.append(
SelectionSetNameGenerator.ConditionPath.path(for: node)
)
fulfilledFragments.append(selectionSetNameComponents.joined(separator: "."))
}
return fulfilledFragments
}

}

fileprivate struct SelectionSetNameGenerator {
Expand Down Expand Up @@ -722,11 +748,13 @@ fileprivate struct SelectionSetNameGenerator {

static func generatedSelectionSetName(
for mergedSource: IR.MergedSelections.MergedSource,
to toNode: LinkedList<IR.ScopeCondition>.Node? = nil,
format: Format,
pluralizer: Pluralizer
) -> String {
generatedSelectionSetName(
for: mergedSource.typeInfo,
to: toNode,
format: format,
pluralizer: pluralizer
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class FragmentTemplateTests: XCTestCase {
"species": species,
],
fulfilledFragments: [
ObjectIdentifier(Self.self)
ObjectIdentifier(TestFragment.self)
]
))
}
Expand Down Expand Up @@ -460,7 +460,7 @@ class FragmentTemplateTests: XCTestCase {
"species": species,
],
fulfilledFragments: [
ObjectIdentifier(Self.self)
ObjectIdentifier(TestFragment.self)
]
))
}
Expand Down Expand Up @@ -567,7 +567,7 @@ class FragmentTemplateTests: XCTestCase {
"species": species,
],
fulfilledFragments: [
ObjectIdentifier(Self.self)
ObjectIdentifier(TestFragment.self)
]
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class OperationDefinitionTemplateTests: XCTestCase {
"species": species,
],
fulfilledFragments: [
ObjectIdentifier(Self.self)
ObjectIdentifier(TestOperationQuery.Data.AllAnimal.self)
]
))
}
Expand Down Expand Up @@ -381,7 +381,7 @@ class OperationDefinitionTemplateTests: XCTestCase {
"species": species,
],
fulfilledFragments: [
ObjectIdentifier(Self.self)
ObjectIdentifier(TestOperationQuery.Data.AllAnimal.self)
]
))
}
Expand Down
Loading