Skip to content

Commit

Permalink
feature: Operation definition template supports the @defer directive (
Browse files Browse the repository at this point in the history
#31)

Co-authored-by: Anthony Miller <[email protected]>
  • Loading branch information
calvincestari and AnthonyMDev authored Sep 26, 2023
1 parent 443aca7 commit 3fbe180
Show file tree
Hide file tree
Showing 25 changed files with 573 additions and 104 deletions.
6 changes: 4 additions & 2 deletions Tests/ApolloCodegenInternalTestHelpers/IR+Mocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ extension IR.Operation {

public static func mock(
definition: CompilationResult.OperationDefinition? = nil,
referencedFragments: OrderedSet<IR.NamedFragment> = []
referencedFragments: OrderedSet<IR.NamedFragment> = [],
hasDeferredFragments: Bool = false
) -> IR.Operation {
let definition = definition ?? .mock()
return IR.Operation.init(
Expand All @@ -114,7 +115,8 @@ extension IR.Operation {
givenAllTypesInSchema: .init([]))
])
),
referencedFragments: referencedFragments
referencedFragments: referencedFragments,
hasDeferredFragments: hasDeferredFragments
)
}

Expand Down
265 changes: 258 additions & 7 deletions Tests/ApolloCodegenTests/CodeGenIR/IRRootFieldBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ class IRRootFieldBuilderTests: XCTestCase {
var document: String!
var ir: IRBuilder!
var operation: CompilationResult.OperationDefinition!
var subject: IR.EntityField!
var computedReferencedFragments: IR.RootFieldBuilder.ReferencedFragments!
var result: IR.RootFieldBuilder.Result!

var subject: IR.EntityField! {
result.rootField
}
var computedReferencedFragments: IR.RootFieldBuilder.ReferencedFragments! {
result.referencedFragments
}

var schema: IR.Schema { ir.schema }

Expand All @@ -28,8 +34,7 @@ class IRRootFieldBuilderTests: XCTestCase {
schemaSDL = nil
document = nil
operation = nil
subject = nil
computedReferencedFragments = nil
result = nil
super.tearDown()
}

Expand All @@ -39,7 +44,7 @@ class IRRootFieldBuilderTests: XCTestCase {
ir = try .mock(schema: schemaSDL, document: document)
operation = try XCTUnwrap(ir.compilationResult.operations.first)

let result = IR.RootFieldBuilder.buildRootEntityField(
result = IR.RootFieldBuilder.buildRootEntityField(
forRootField: .mock(
"query",
type: .nonNull(.entity(operation.rootType)),
Expand All @@ -48,8 +53,6 @@ class IRRootFieldBuilderTests: XCTestCase {
onRootEntity: IR.Entity(source: .operation(operation)),
inIR: ir
)
subject = result.rootField
computedReferencedFragments = result.referencedFragments
}

// MARK: - Children Computation
Expand Down Expand Up @@ -4324,4 +4327,252 @@ class IRRootFieldBuilderTests: XCTestCase {
expect(self.computedReferencedFragments).to(equal(expected))
}

// MARK: - Deferred Fragments

func test__deferredFragments__givenNoDeferredFragment_hasDeferredFragmentsFalse() throws {
// given
schemaSDL = """
type Query {
allAnimals: [Animal!]
}
interface Animal {
id: String
species: String
genus: String
}
type Dog implements Animal {
id: String
species: String
genus: String
name: String
}
"""

document = """
query TestOperation {
allAnimals {
__typename
id
... on Dog {
species
}
}
}
"""

// when
try buildSubjectRootField()

// then
expect(self.result.hasDeferredFragments).to(beFalse())
}

func test__deferredFragments__givenDeferredInlineFragment_hasDeferredFragmentsTrue() throws {
// given
schemaSDL = """
type Query {
allAnimals: [Animal!]
}
interface Animal {
id: String
species: String
genus: String
}
type Dog implements Animal {
id: String
species: String
genus: String
name: String
}
"""

document = """
query TestOperation {
allAnimals {
__typename
id
... on Dog @defer(label: "root") {
species
}
}
}
"""

// when
try buildSubjectRootField()

// then
expect(self.result.hasDeferredFragments).to(beTrue())
}

func test__deferredFragments__givenDeferredInlineFragmentWithCondition_hasDeferredFragmentsTrue() throws {
// given
schemaSDL = """
type Query {
allAnimals: [Animal!]
}
interface Animal {
id: String
species: String
genus: String
}
type Dog implements Animal {
id: String
species: String
genus: String
name: String
}
"""

document = """
query TestOperation {
allAnimals {
__typename
id
... on Dog @defer(if: "a", label: "root") {
species
}
}
}
"""

// when
try buildSubjectRootField()

// then
expect(self.result.hasDeferredFragments).to(beTrue())
}

func test__deferredFragments__givenDeferredInlineFragmentWithConditionFalse_hasDeferredFragmentsFalse() throws {
// given
schemaSDL = """
type Query {
allAnimals: [Animal!]
}
interface Animal {
id: String
species: String
genus: String
}
type Dog implements Animal {
id: String
species: String
genus: String
name: String
}
"""

document = """
query TestOperation {
allAnimals {
__typename
id
... on Dog @defer(if: false, label: "root") {
species
}
}
}
"""

// when
try buildSubjectRootField()

// then
expect(self.result.hasDeferredFragments).to(beFalse())
}

func test__deferredFragments__givenDeferredNamedFragment_onDifferentTypeCase_hasDeferredFragmentsTrue() throws {
// given
schemaSDL = """
type Query {
allAnimals: [Animal!]
}
interface Animal {
id: String
species: String
genus: String
}
type Dog implements Animal {
id: String
species: String
genus: String
name: String
}
"""

document = """
query TestOperation {
allAnimals {
__typename
id
...DogFragment @defer
}
}
fragment DogFragment on Dog {
species
}
"""

// when
try buildSubjectRootField()

// then
expect(self.result.hasDeferredFragments).to(beTrue())
}

func test__deferredFragments__givenDeferredInlineFragment_withinNamedFragment_hasDeferredFragmentsTrue() throws {
// given
schemaSDL = """
type Query {
allAnimals: [Animal!]
}
interface Animal {
id: String
species: String
genus: String
}
type Dog implements Animal {
id: String
species: String
genus: String
name: String
}
"""

document = """
query TestOperation {
allAnimals {
__typename
id
...DogFragment
}
}
fragment DogFragment on Animal {
... on Dog @defer(label: "root") {
species
}
}
"""

// when
try buildSubjectRootField()

// then
expect(self.result.hasDeferredFragments).to(beTrue())
}

}
Loading

0 comments on commit 3fbe180

Please sign in to comment.