Skip to content

Commit

Permalink
feature: Emit Identifiable conformance on SelectionSets (apollographq…
Browse files Browse the repository at this point in the history
  • Loading branch information
x-sheep authored and gh-action-runner committed Jan 24, 2025
1 parent 38ea614 commit c254f6c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ struct SelectionSetTemplate {
"""
\(SelectionSetNameDocumentation(selectionSet))
\(renderAccessControl())\
struct \(fieldSelectionSetName): \(SelectionSetType()) {
struct \(fieldSelectionSetName): \(SelectionSetType())\
\(if: selectionSet.isIdentifiable, ", Identifiable")\
{
\(BodyTemplate(context))
}
"""
Expand All @@ -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))
}
Expand Down
19 changes: 19 additions & 0 deletions Sources/IR/IR+ComputedSelectionSet.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import GraphQLCompiler
import OrderedCollections
import Utilities

Expand All @@ -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

Expand Down

0 comments on commit c254f6c

Please sign in to comment.