Skip to content

Commit ca10bb6

Browse files
gh-action-runnergh-action-runner
authored andcommitted
Squashed 'apollo-ios-codegen/' changes from 38ea6149..c254f6c7
c254f6c7 feature: Emit Identifiable conformance on SelectionSets (#584) git-subtree-dir: apollo-ios-codegen git-subtree-split: c254f6c7b30b57b9c84d700b761d3af329f79bc3
1 parent 19a6607 commit ca10bb6

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Sources/ApolloCodegenLib/Templates/SelectionSetTemplate.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ struct SelectionSetTemplate {
102102
"""
103103
\(SelectionSetNameDocumentation(selectionSet))
104104
\(renderAccessControl())\
105-
struct \(fieldSelectionSetName): \(SelectionSetType()) {
105+
struct \(fieldSelectionSetName): \(SelectionSetType())\
106+
\(if: selectionSet.isIdentifiable, ", Identifiable")\
107+
{
106108
\(BodyTemplate(context))
107109
}
108110
"""
@@ -118,6 +120,7 @@ struct SelectionSetTemplate {
118120
\(renderAccessControl())\
119121
struct \(inlineFragment.renderedTypeName): \(SelectionSetType(asInlineFragment: true))\
120122
\(if: inlineFragment.isCompositeInlineFragment, ", \(config.ApolloAPITargetName).CompositeInlineFragment")\
123+
\(if: inlineFragment.isIdentifiable, ", Identifiable")\
121124
{
122125
\(BodyTemplate(context))
123126
}

Sources/IR/IR+ComputedSelectionSet.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import GraphQLCompiler
23
import OrderedCollections
34
import Utilities
45

@@ -18,6 +19,24 @@ public struct ComputedSelectionSet {
1819

1920
/// The `TypeInfo` for the selection set of the computed selections
2021
public let typeInfo: IR.SelectionSet.TypeInfo
22+
23+
/// Indicates if the parent type has a single keyField named `id`.
24+
public var isIdentifiable: Bool {
25+
guard direct?.fields["id"] != nil || merged.fields["id"] != nil else {
26+
return false
27+
}
28+
if let type = typeInfo.parentType as? GraphQLObjectType,
29+
type.keyFields == ["id"] {
30+
return true
31+
}
32+
33+
if let type = typeInfo.parentType as? GraphQLInterfaceType,
34+
type.keyFields == ["id"] {
35+
return true
36+
}
37+
38+
return false
39+
}
2140

2241
// MARK: Dynamic Member Subscript
2342

0 commit comments

Comments
 (0)