Skip to content

Commit

Permalink
Squashed 'apollo-ios-codegen/' changes from f20dccb0..6db35551
Browse files Browse the repository at this point in the history
6db35551 fix: `DataDict` initialization of `deferredFragments` (#557)

git-subtree-dir: apollo-ios-codegen
git-subtree-split: 6db35551c7bc0879b61539c08338253a7b4a146b
  • Loading branch information
gh-action-runner authored and gh-action-runner committed Dec 16, 2024
1 parent ff19d0b commit e28c196
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ struct SelectionSetTemplate {
private func InitializerTemplate(
_ selectionSet: ComputedSelectionSet
) -> TemplateString {
let containsDeferredFragment = (selectionSet.direct?.inlineFragments.containsDeferredFragment ?? false) ||
(selectionSet.direct?.namedFragments.containsDeferredFragment ?? false)

return """
\(renderAccessControl())init(
\(InitializerSelectionParametersTemplate(selectionSet))
Expand All @@ -612,7 +615,14 @@ struct SelectionSetTemplate {
data: [
\(InitializerDataDictTemplate(selectionSet))
],
fulfilledFragments: \(InitializerFulfilledFragments(selectionSet))
fulfilledFragments: [
\(InitializerFulfilledFragments(selectionSet))
]\(if: containsDeferredFragment, """
,
deferredFragments: [
\(InitializerDeferredFragments(selectionSet))
]
""")
))
}
"""
Expand Down Expand Up @@ -708,9 +718,41 @@ struct SelectionSetTemplate {
}

return """
[
\(fulfilledFragments.map { "ObjectIdentifier(\($0).self)" })
]
\(fulfilledFragments.map { "ObjectIdentifier(\($0).self)" })
"""
}

private func InitializerDeferredFragments(
_ selectionSet: ComputedSelectionSet
) -> TemplateString? {
guard let directSelections = selectionSet.direct else { return nil }

var deferredFragments: OrderedSet<String> = []

let nameGenerator: (_ typeInfo: SelectionSet.TypeInfo) -> String = { typeInfo in
SelectionSetNameGenerator.generatedSelectionSetName(
for: typeInfo,
format: .fullyQualified,
pluralizer: config.pluralizer
)
}

for inlineFragmentSpread in directSelections.inlineFragments.values.elements {
if inlineFragmentSpread.typeInfo.isDeferred {
let selectionSetName = nameGenerator(inlineFragmentSpread.typeInfo)
deferredFragments.append(selectionSetName)
}
}

for namedFragment in directSelections.namedFragments.values.elements {
if namedFragment.typeInfo.isDeferred {
let selectionSetName = nameGenerator(namedFragment.typeInfo)
deferredFragments.append(selectionSetName)
}
}

return """
\(deferredFragments.map { "ObjectIdentifier(\($0).self)" })
"""
}

Expand Down

0 comments on commit e28c196

Please sign in to comment.