Skip to content

Commit

Permalink
Fix crash in codegen when only selection is __typename
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Jul 1, 2022
1 parent dd87224 commit 85e7015
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/ApolloCodegenLib/IR/IR+EntitySelectionTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ extension IR {
into selections: IR.MergedSelections
) {
guard let nextTypePathNode = typePath.next else {
guard case let .fieldScope(node) = child else { fatalError() }
guard case let .fieldScope(node) = child else { return }
node.mergeSelections(matchingScopePath: typePath, into: selections)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,41 @@ class FragmentTemplateTests: XCTestCase {
expect(actual).to(equalLineByLine(expected, atLine: 14, ignoringExtraLines: true))
}

func test__render__givenFragmentWithOnlyTypenameField_generatesFragmentDefinition_withNoSelections() throws {
// given
document = """
fragment TestFragment on Animal {
__typename
}
"""

try buildSubjectAndFragment()

let expected = """
struct TestFragment: TestSchema.SelectionSet, Fragment {
public static var fragmentDefinition: StaticString { ""\"
fragment TestFragment on Animal {
__typename
}
""\" }
public let __data: DataDict
public init(data: DataDict) { __data = data }
public static var __parentType: ParentType { .Object(TestSchema.Animal.self) }
public static var selections: [Selection] { [
] }
}
"""

// when
let actual = renderSubject()

// then
expect(actual).to(equalLineByLine(expected))
}

/// MARK: - Local Cache Mutation Tests
func test__render__givenFragment__asLocalCacheMutation_generatesFragmentDeclarationDefinitionAsMutableSelectionSetAndBoilerplate() throws {
// given
Expand Down

0 comments on commit 85e7015

Please sign in to comment.