Skip to content

Commit 1babf52

Browse files
authored
Merge pull request #4270 from timward60/fix/defer-dataloader-dispatch-field-count
2 parents 121e207 + 67133cb commit 1babf52

2 files changed

Lines changed: 178 additions & 2 deletions

File tree

src/main/java/graphql/execution/incremental/DeferredExecutionSupport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ public Set<IncrementalCall<? extends IncrementalPayload>> createCalls() {
128128

129129
private DeferredFragmentCall createDeferredFragmentCall(DeferredExecution deferredExecution) {
130130
int level = parameters.getPath().getLevel() + 1;
131-
AlternativeCallContext alternativeCallContext = new AlternativeCallContext(level, deferredFields.size());
132-
133131
List<MergedField> mergedFields = deferredExecutionToFields.get(deferredExecution);
132+
AlternativeCallContext alternativeCallContext = new AlternativeCallContext(level, mergedFields.size());
134133

135134
List<Supplier<CompletableFuture<DeferredFragmentCall.FieldWithExecutionResult>>> calls = FpKit.arrayListSizedTo(mergedFields);
136135
for (MergedField currentField : mergedFields) {

src/test/groovy/graphql/execution/instrumentation/dataloader/DeferWithDataLoaderTest.groovy

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,183 @@ class DeferWithDataLoaderTest extends Specification {
388388

389389
}
390390

391+
@Unroll
392+
def "multiple separate defer fragments with dataloader fields dispatch correctly"() {
393+
given:
394+
def query = """
395+
query {
396+
shops {
397+
id
398+
name
399+
... @defer(label: "deferred1") {
400+
departments {
401+
name
402+
}
403+
}
404+
... @defer(label: "deferred2") {
405+
expensiveDepartments {
406+
name
407+
}
408+
}
409+
}
410+
}
411+
"""
412+
413+
def expectedInitialData = [
414+
data : [
415+
shops: [
416+
[id: "shop-1", name: "Shop 1"],
417+
[id: "shop-2", name: "Shop 2"],
418+
[id: "shop-3", name: "Shop 3"],
419+
]
420+
],
421+
hasNext: true
422+
]
423+
424+
when:
425+
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
426+
.query(query)
427+
.dataLoaderRegistry(dataLoaderRegistry)
428+
.graphQLContext([(ENABLE_INCREMENTAL_SUPPORT): true])
429+
.build()
430+
executionInput.getGraphQLContext().putAll(contextKey == null ? Collections.emptyMap() : [(contextKey): true])
431+
432+
IncrementalExecutionResult result = graphQL.execute(executionInput)
433+
434+
then:
435+
result.toSpecification() == expectedInitialData
436+
437+
when:
438+
def incrementalResults = getIncrementalResults(result)
439+
440+
then:
441+
assertIncrementalResults(incrementalResults, [["shops", 0], ["shops", 1], ["shops", 2], ["shops", 0], ["shops", 1], ["shops", 2]])
442+
443+
when:
444+
def combined = combineExecutionResults(result.toSpecification(), incrementalResults)
445+
then:
446+
combined.errors == null
447+
combined.data == [
448+
shops: [
449+
[id : "shop-1", name: "Shop 1",
450+
departments : [[name: "Department 1"],
451+
[name: "Department 2"],
452+
[name: "Department 3"]],
453+
expensiveDepartments: [[name: "Department 1"],
454+
[name: "Department 2"],
455+
[name: "Department 3"]]],
456+
[id : "shop-2", name: "Shop 2",
457+
departments : [[name: "Department 4"],
458+
[name: "Department 5"],
459+
[name: "Department 6"]],
460+
expensiveDepartments: [[name: "Department 4"],
461+
[name: "Department 5"],
462+
[name: "Department 6"]]],
463+
[id : "shop-3", name: "Shop 3",
464+
departments : [[name: "Department 7"],
465+
[name: "Department 8"],
466+
[name: "Department 9"]],
467+
expensiveDepartments: [[name: "Department 7"],
468+
[name: "Department 8"],
469+
[name: "Department 9"]]]]
470+
]
471+
472+
where:
473+
contextKey << [ENABLE_DATA_LOADER_CHAINING, ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, null]
474+
}
475+
476+
@Unroll
477+
def "mixed deferred and non-deferred fields with dataloaders dispatch correctly"() {
478+
given:
479+
def query = """
480+
query {
481+
shops {
482+
id
483+
name
484+
departments {
485+
name
486+
}
487+
... @defer(label: "deferred1") {
488+
expensiveDepartments {
489+
name
490+
}
491+
}
492+
}
493+
}
494+
"""
495+
496+
def expectedInitialData = [
497+
data : [
498+
shops: [
499+
[id: "shop-1", name: "Shop 1",
500+
departments: [[name: "Department 1"],
501+
[name: "Department 2"],
502+
[name: "Department 3"]]],
503+
[id: "shop-2", name: "Shop 2",
504+
departments: [[name: "Department 4"],
505+
[name: "Department 5"],
506+
[name: "Department 6"]]],
507+
[id: "shop-3", name: "Shop 3",
508+
departments: [[name: "Department 7"],
509+
[name: "Department 8"],
510+
[name: "Department 9"]]],
511+
]
512+
],
513+
hasNext: true
514+
]
515+
516+
when:
517+
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
518+
.query(query)
519+
.dataLoaderRegistry(dataLoaderRegistry)
520+
.graphQLContext([(ENABLE_INCREMENTAL_SUPPORT): true])
521+
.build()
522+
executionInput.getGraphQLContext().putAll(contextKey == null ? Collections.emptyMap() : [(contextKey): true])
523+
524+
IncrementalExecutionResult result = graphQL.execute(executionInput)
525+
526+
then:
527+
result.toSpecification() == expectedInitialData
528+
529+
when:
530+
def incrementalResults = getIncrementalResults(result)
531+
532+
then:
533+
assertIncrementalResults(incrementalResults, [["shops", 0], ["shops", 1], ["shops", 2]])
534+
535+
when:
536+
def combined = combineExecutionResults(result.toSpecification(), incrementalResults)
537+
then:
538+
combined.errors == null
539+
combined.data == [
540+
shops: [
541+
[id : "shop-1", name: "Shop 1",
542+
departments : [[name: "Department 1"],
543+
[name: "Department 2"],
544+
[name: "Department 3"]],
545+
expensiveDepartments: [[name: "Department 1"],
546+
[name: "Department 2"],
547+
[name: "Department 3"]]],
548+
[id : "shop-2", name: "Shop 2",
549+
departments : [[name: "Department 4"],
550+
[name: "Department 5"],
551+
[name: "Department 6"]],
552+
expensiveDepartments: [[name: "Department 4"],
553+
[name: "Department 5"],
554+
[name: "Department 6"]]],
555+
[id : "shop-3", name: "Shop 3",
556+
departments : [[name: "Department 7"],
557+
[name: "Department 8"],
558+
[name: "Department 9"]],
559+
expensiveDepartments: [[name: "Department 7"],
560+
[name: "Department 8"],
561+
[name: "Department 9"]]]]
562+
]
563+
564+
where:
565+
contextKey << [ENABLE_DATA_LOADER_CHAINING, ENABLE_DATA_LOADER_EXHAUSTED_DISPATCHING, null]
566+
}
567+
391568
@Unroll
392569
@RepeatUntilFailure(maxAttempts = 20, ignoreRest = false)
393570
def "dataloader in initial result and chained dataloader inside nested defer block"() {

0 commit comments

Comments
 (0)