Skip to content

Commit eec5f78

Browse files
gh-action-runnergh-action-runner
authored andcommitted
Squashed 'apollo-ios-codegen/' changes from ec18790f..4b1bc5bc
4b1bc5bc feature: Identifiable conformance for Named Fragments (#595) git-subtree-dir: apollo-ios-codegen git-subtree-split: 4b1bc5bcdecab067326e4ba77e651f662d1a1d19
1 parent d25be65 commit eec5f78

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

Sources/ApolloCodegenLib/Templates/FragmentTemplate.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ struct FragmentTemplate: TemplateRenderer {
2323
"""
2424
\(accessControlModifier(for: .parent))\
2525
struct \(fragment.generatedDefinitionName.asFragmentName): \
26-
\(fragment.renderedSelectionSetType(config)), Fragment {
26+
\(fragment.renderedSelectionSetType(config)), Fragment\
27+
\(if: fragment.isIdentifiable, ", Identifiable")\
28+
{
2729
\(if: includeDefinition, """
2830
\(accessControlModifier(for: .member))\
2931
static var fragmentDefinition: StaticString {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import GraphQLCompiler
2+
3+
extension GraphQLCompositeType {
4+
/// Indicates if the type has a single keyField named `id`.
5+
var isIdentifiable: Bool {
6+
switch(self) {
7+
case let interface as GraphQLInterfaceType:
8+
return interface.keyFields == ["id"]
9+
10+
case let object as GraphQLObjectType:
11+
return object.keyFields == ["id"]
12+
13+
default:
14+
return false
15+
}
16+
}
17+
}

Sources/IR/IR+ComputedSelectionSet.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,14 @@ public struct ComputedSelectionSet {
2020
/// The `TypeInfo` for the selection set of the computed selections
2121
public let typeInfo: IR.SelectionSet.TypeInfo
2222

23-
/// Indicates if the parent type has a single keyField named `id`.
23+
/// Indicates if a field named `id` is selected as well as requiring that the parent type
24+
/// be identifiable.
2425
public var isIdentifiable: Bool {
2526
guard direct?.fields["id"] != nil || merged.fields["id"] != nil else {
2627
return false
2728
}
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
29+
30+
return typeInfo.parentType.isIdentifiable
3931
}
4032

4133
// MARK: Dynamic Member Subscript

Sources/IR/IR+NamedFragment.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ public class NamedFragment: Definition, Hashable, CustomDebugStringConvertible {
2222
public var name: String { definition.name }
2323
public var type: GraphQLCompositeType { definition.type }
2424
public var isLocalCacheMutation: Bool { definition.isLocalCacheMutation }
25+
26+
/// Indicates if a field named `id` is selected as well as requiring that the parent type
27+
/// be identifiable.
28+
public var isIdentifiable: Bool {
29+
guard definition.selectionSet.selections.contains(where: {
30+
guard case .field(let field) = $0 else { return false }
31+
return field.name == "id"
32+
}) else { return false }
33+
34+
return type.isIdentifiable
35+
}
2536

2637
init(
2738
definition: CompilationResult.FragmentDefinition,

0 commit comments

Comments
 (0)