From e28c19678e867c50016f4dc752d9ed4968984152 Mon Sep 17 00:00:00 2001 From: gh-action-runner Date: Mon, 16 Dec 2024 22:04:50 +0000 Subject: [PATCH] Squashed 'apollo-ios-codegen/' changes from f20dccb0..6db35551 6db35551 fix: `DataDict` initialization of `deferredFragments` (apollographql/apollo-ios-dev#557) git-subtree-dir: apollo-ios-codegen git-subtree-split: 6db35551c7bc0879b61539c08338253a7b4a146b --- .../Templates/SelectionSetTemplate.swift | 50 +++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift b/Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift index aed458cb4..98448edbb 100644 --- a/Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift @@ -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)) @@ -612,7 +615,14 @@ struct SelectionSetTemplate { data: [ \(InitializerDataDictTemplate(selectionSet)) ], - fulfilledFragments: \(InitializerFulfilledFragments(selectionSet)) + fulfilledFragments: [ + \(InitializerFulfilledFragments(selectionSet)) + ]\(if: containsDeferredFragment, """ + , + deferredFragments: [ + \(InitializerDeferredFragments(selectionSet)) + ] + """) )) } """ @@ -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 = [] + + 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)" }) """ }