Skip to content

Commit

Permalink
tests: Add merged selection deferred test case (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari authored and gh-action-runner committed Nov 28, 2023
1 parent b810504 commit f427ad6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
82 changes: 82 additions & 0 deletions Tests/ApolloCodegenTests/CodeGenIR/IRRootFieldBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6702,6 +6702,88 @@ class IRRootFieldBuilderTests: XCTestCase {
))
}

func test__deferredFragments__givenDeferredNamedFragmentWithMatchingDeferredTypeCase_buildsInlineFragmentWithMergedFragmentSelection() async throws {
// given
schemaSDL = """
type Query {
allAnimals: [Animal!]
}
interface Animal {
id: String
species: String
}
type Pet implements Animal {
id: String
species: String
}
"""

document = """
query TestOperation($a: Boolean) {
allAnimals {
__typename
...AnimalFragment @defer(label: "root")
... on Pet {
id
}
}
}
fragment AnimalFragment on Animal {
species
}
"""

// when
try await buildSubjectRootField()

// then
let Interface_Animal = try XCTUnwrap(schema[interface: "Animal"])
let Fragment_AnimalFragment = try XCTUnwrap(ir.compilationResult[fragment: "AnimalFragment"])
let Object_Pet = try XCTUnwrap(schema[object: "Pet"])

let allAnimals = self.subject[field: "allAnimals"]
let animalFragment = try await ir.builtFragmentStorage
.getFragmentIfBuilt(named: "AnimalFragment").xctUnwrapped()
let allAnimals_asPet = allAnimals?[as: "Pet"]

expect(allAnimals?.selectionSet).to(shallowlyMatch(
SelectionSetMatcher(
parentType: Interface_Animal,
directSelections: [
.inlineFragment(parentType: Object_Pet),
.deferred(Fragment_AnimalFragment, label: "root"),
]
)
))

expect(animalFragment.rootField.selectionSet).to(shallowlyMatch(
SelectionSetMatcher(
parentType: Interface_Animal,
directSelections: [
.field("species", type: .string()),
]
)
))

expect(allAnimals_asPet).to(shallowlyMatch(
SelectionSetMatcher(
parentType: Object_Pet,
directSelections: [
.field("id", type: .string()),
],
mergedSelections: [
.deferred(Fragment_AnimalFragment, label: "root"),
],
mergedSources: [
try .mock(allAnimals),
]
)
))
}

// MARK: Deferred Fragments - Named Fragments (with @include/@skip)

func test__deferredFragments__givenBothDeferAndIncludeDirectives_onSameNamedFragment_buildsNestedDeferredNamedFragment() async throws {
Expand Down
2 changes: 1 addition & 1 deletion apollo-ios/Sources/Apollo/GraphQLResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension GraphQLResult {

private func convert(value: Any) -> Any {
var val: Any = value
if let value = value as? ApolloAPI.DataDict {
if let value = value as? DataDict {
val = value._data
} else if let value = value as? CustomScalarType {
val = value._jsonValue
Expand Down

0 comments on commit f427ad6

Please sign in to comment.