You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/batching.md
+64Lines changed: 64 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -370,3 +370,67 @@ DataFetcher<?> dataFetcherThatCallsTheDataLoader = new DataFetcher<Object>() {
370
370
}
371
371
};
372
372
```
373
+
374
+
## Chained DataLoaders
375
+
376
+
The automatic dispatching of Chained DataLoaders is a new feature included in GraphQL Java 25.0 onwards. Before this version, DataLoaders in chains needed to be manually dispatched.
377
+
378
+
A Chained DataLoader is where one DataLoader depends on another, within the same DataFetcher.
The DataFetcher in the example above, before version 25.0 would have caused execution to hang, because the second DataLoader ("email") was never dispatched.
403
+
404
+
Prior to version 25.0, users of GraphQL Java needed to manually dispatch DataLoaders to ensure execution completed. From version 25.0, the GraphQL Java engine will automatically dispatch Chained DataLoaders.
405
+
406
+
If you're looking for more examples, and the technical details, please see [our tests](https://github.com/graphql-java/graphql-java/blob/master/src/test/groovy/graphql/ChainedDataLoaderTest.groovy).
407
+
408
+
Note: The GraphQL Java engine can only optimally calculate DataLoader dispatches on a per-level basis. It does not calculate optimal DataLoader dispatching across different levels of an operation's field tree.
409
+
410
+
### A special case: Delayed DataLoaders
411
+
412
+
In a previous code snippet, we demonstrated one DataLoader depending on another DataLoader.
413
+
414
+
Another special case is a "delayed" DataLoader, where a DataLoader depends on a slow async task instead. For example, here are two DataFetchers from [a test example](https://github.com/graphql-java/graphql-java/blob/master/src/test/groovy/graphql/ChainedDataLoaderTest.groovy):
415
+
416
+
```groovy
417
+
def fooDF = { env ->
418
+
return supplyAsync {
419
+
Thread.sleep(1000)
420
+
return "fooFirstValue"
421
+
}.thenCompose {
422
+
return env.getDataLoader("dl").load(it)
423
+
}
424
+
} as DataFetcher
425
+
426
+
def barDF = { env ->
427
+
return supplyAsync {
428
+
Thread.sleep(1000)
429
+
return "barFirstValue"
430
+
}.thenCompose {
431
+
return env.getDataLoader("dl").load(it)
432
+
}
433
+
} as DataFetcher
434
+
```
435
+
436
+
By opting in to Chained DataLoaders, GraphQL Java will also calculate when to dispatch "delayed" DataLoaders. These "delayed" DataLoaders will be enqueued for dispatch after the async task completes.
0 commit comments