Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Emit Identifiable conformance on SelectionSets #548

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,49 @@ class SelectionSetTemplateTests: XCTestCase {
// then
expect(actual).to(equalLineByLine(expected, atLine: 7, ignoringExtraLines: true))
}

func test__render_selections__givenFieldWithNonScalarID_doesNotAddIdentifiableConformance() async throws {
// given
schemaSDL = """
type Query {
allAnimals: [Animal!]
}

type ComplexID {
first: String!
second: String!
}

type Animal {
id: ComplexID!
name: String
}
"""

document = """
query TestOperation {
allAnimals {
id { first }
name
}
}
"""

let expected = """
public struct AllAnimal: TestSchema.SelectionSet {
"""

// when
try await buildSubjectAndOperation()
let allAnimals = try XCTUnwrap(
operation[field: "query"]?[field: "allAnimals"]?.selectionSet
)

let actual = subject.test_render(childEntity: allAnimals.computed)

// then
expect(actual).to(equalLineByLine(expected, atLine: 2, ignoringExtraLines: true))
}

func test__render_selections__givenEntityFieldWithNameNotMatchingType_rendersFieldSelections() async throws {
// given
Expand Down Expand Up @@ -10601,7 +10644,7 @@ class SelectionSetTemplateTests: XCTestCase {
public var comments: [Comment]? { __data["comments"] }

/// AllAuthor.PostsInfoById.Awarding.Comment
public struct Comment: TestSchema.SelectionSet {
public struct Comment: TestSchema.SelectionSet, Identifiable {
"""

let expectedTypeAlias = """
Expand Down Expand Up @@ -10697,7 +10740,7 @@ class SelectionSetTemplateTests: XCTestCase {
public var comments: [Comment]? { __data["comments"] }

/// AllAuthor.PostsInfoById.Awarding.Comment
public struct Comment: TestSchema.SelectionSet {
public struct Comment: TestSchema.SelectionSet, Identifiable {
public let __data: DataDict
public init(_dataDict: DataDict) { __data = _dataDict }

Expand Down Expand Up @@ -11761,7 +11804,7 @@ class SelectionSetTemplateTests: XCTestCase {
expect(rendered_allAnimals_deferredAsRoot).to(equalLineByLine(
"""
/// AllAnimal.Root
public struct Root: TestSchema.InlineFragment {
public struct Root: TestSchema.InlineFragment, Identifiable {
public let __data: DataDict
public init(_dataDict: DataDict) { __data = _dataDict }

Expand Down Expand Up @@ -11813,7 +11856,7 @@ class SelectionSetTemplateTests: XCTestCase {
expect(rendered_allAnimals_deferredAsRoot).to(equalLineByLine(
"""
/// AllAnimal.Root
public struct Root: TestSchema.InlineFragment {
public struct Root: TestSchema.InlineFragment, Identifiable {
public let __data: DataDict
public init(_dataDict: DataDict) { __data = _dataDict }

Expand Down Expand Up @@ -11870,7 +11913,7 @@ class SelectionSetTemplateTests: XCTestCase {
expect(rendered_allAnimals_asDog_deferredAsRoot).to(equalLineByLine(
"""
/// AllAnimal.AsDog.Root
public struct Root: TestSchema.InlineFragment {
public struct Root: TestSchema.InlineFragment, Identifiable {
public let __data: DataDict
public init(_dataDict: DataDict) { __data = _dataDict }

Expand Down Expand Up @@ -11935,7 +11978,7 @@ class SelectionSetTemplateTests: XCTestCase {
expect(rendered_allAnimals_asDog_deferredAsOne).to(equalLineByLine(
"""
/// AllAnimal.AsDog.One
public struct One: TestSchema.InlineFragment {
public struct One: TestSchema.InlineFragment, Identifiable {
public let __data: DataDict
public init(_dataDict: DataDict) { __data = _dataDict }

Expand All @@ -11949,7 +11992,7 @@ class SelectionSetTemplateTests: XCTestCase {
expect(rendered_allAnimals_asDog_deferredAsTwo).to(equalLineByLine(
"""
/// AllAnimal.AsDog.Two
public struct Two: TestSchema.InlineFragment {
public struct Two: TestSchema.InlineFragment, Identifiable {
public let __data: DataDict
public init(_dataDict: DataDict) { __data = _dataDict }

Expand Down Expand Up @@ -12021,7 +12064,7 @@ class SelectionSetTemplateTests: XCTestCase {
expect(rendered_allAnimals_asDog_deferredAsOne).to(equalLineByLine(
"""
/// AllAnimal.AsDog.One
public struct One: TestSchema.InlineFragment {
public struct One: TestSchema.InlineFragment, Identifiable {
public let __data: DataDict
public init(_dataDict: DataDict) { __data = _dataDict }

Expand All @@ -12035,7 +12078,7 @@ class SelectionSetTemplateTests: XCTestCase {
expect(rendered_allAnimals_asCat_deferredAsTwo).to(equalLineByLine(
"""
/// AllAnimal.AsCat.Two
public struct Two: TestSchema.InlineFragment {
public struct Two: TestSchema.InlineFragment, Identifiable {
public let __data: DataDict
public init(_dataDict: DataDict) { __data = _dataDict }

Expand Down Expand Up @@ -12114,7 +12157,7 @@ class SelectionSetTemplateTests: XCTestCase {
expect(rendered_allAnimals_asDog_deferredAsOuter).to(equalLineByLine(
"""
/// AllAnimal.AsDog.Outer
public struct Outer: TestSchema.InlineFragment {
public struct Outer: TestSchema.InlineFragment, Identifiable {
public let __data: DataDict
public init(_dataDict: DataDict) { __data = _dataDict }

Expand Down
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
9 changes: 9 additions & 0 deletions apollo-ios-codegen/Sources/IR/IR+ComputedSelectionSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public struct ComputedSelectionSet {

/// The `TypeInfo` for the selection set of the computed selections
public let typeInfo: IR.SelectionSet.TypeInfo

/// Indicates if an `id` field is present with a scalar type.
public var isIdentifiable: Bool {
let idField = direct?.fields["id"] ?? merged.fields["id"]
if let type = idField?.type.innerType, case .scalar = type {
return true
}
return false
}

// MARK: Dynamic Member Subscript

Expand Down