From ca10bb63bcc3350bcf0d4bd01eb9eaa6d1c7f765 Mon Sep 17 00:00:00 2001 From: gh-action-runner Date: Fri, 24 Jan 2025 17:36:02 +0000 Subject: [PATCH] Squashed 'apollo-ios-codegen/' changes from 38ea6149..c254f6c7 c254f6c7 feature: Emit Identifiable conformance on SelectionSets (apollographql/apollo-ios-dev#584) git-subtree-dir: apollo-ios-codegen git-subtree-split: c254f6c7b30b57b9c84d700b761d3af329f79bc3 --- .../Templates/SelectionSetTemplate.swift | 5 ++++- Sources/IR/IR+ComputedSelectionSet.swift | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift b/Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift index 98448edbb..7bafaf63c 100644 --- a/Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift +++ b/Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift @@ -102,7 +102,9 @@ struct SelectionSetTemplate { """ \(SelectionSetNameDocumentation(selectionSet)) \(renderAccessControl())\ - struct \(fieldSelectionSetName): \(SelectionSetType()) { + struct \(fieldSelectionSetName): \(SelectionSetType())\ + \(if: selectionSet.isIdentifiable, ", Identifiable")\ + { \(BodyTemplate(context)) } """ @@ -118,6 +120,7 @@ struct SelectionSetTemplate { \(renderAccessControl())\ struct \(inlineFragment.renderedTypeName): \(SelectionSetType(asInlineFragment: true))\ \(if: inlineFragment.isCompositeInlineFragment, ", \(config.ApolloAPITargetName).CompositeInlineFragment")\ + \(if: inlineFragment.isIdentifiable, ", Identifiable")\ { \(BodyTemplate(context)) } diff --git a/Sources/IR/IR+ComputedSelectionSet.swift b/Sources/IR/IR+ComputedSelectionSet.swift index d9cf5e7f0..dec4127fc 100644 --- a/Sources/IR/IR+ComputedSelectionSet.swift +++ b/Sources/IR/IR+ComputedSelectionSet.swift @@ -1,4 +1,5 @@ import Foundation +import GraphQLCompiler import OrderedCollections import Utilities @@ -18,6 +19,24 @@ public struct ComputedSelectionSet { /// The `TypeInfo` for the selection set of the computed selections public let typeInfo: IR.SelectionSet.TypeInfo + + /// Indicates if the parent type has a single keyField named `id`. + public var isIdentifiable: Bool { + guard direct?.fields["id"] != nil || merged.fields["id"] != nil else { + return false + } + if let type = typeInfo.parentType as? GraphQLObjectType, + type.keyFields == ["id"] { + return true + } + + if let type = typeInfo.parentType as? GraphQLInterfaceType, + type.keyFields == ["id"] { + return true + } + + return false + } // MARK: Dynamic Member Subscript