Skip to content

Commit

Permalink
Refactor initializer template for deferred fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari committed Dec 16, 2024
1 parent 83bde9e commit 78d8d16
Showing 1 changed file with 46 additions and 4 deletions.
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 78d8d16

Please sign in to comment.