Update Javadoc for BatchMapping

Closes gh-166
This commit is contained in:
Rossen Stoyanchev
2021-10-15 16:19:11 +01:00
parent acd060c50e
commit d5d57c9a2c
2 changed files with 14 additions and 8 deletions

View File

@@ -290,11 +290,11 @@ default it is marked as `INTERNAL_ERROR`.
Given a `Book` and its `Author`, we can create one `DataFetcher` for books and another
for the author of a book. This means books and authors aren't automatically loaded
together, which enables queries to select the subset of data they need. However, when
loading multiple books, the author for each book is loaded individually, and this is
an issue known as the N+1 select problem.
together, which enables queries to select the subset of data they need. However, loading
multiple books, results in loading each author individually, and this is a performance
issue known as the N+1 select problem.
To address the issue, GraphQL Java provides a
GraphQL Java provides a
https://www.graphql-java.com/documentation/v16/batching/[batching feature] that allows
related entities, such as the authors for all books, to be loaded together. This is how
the underlying mechanism works in GraphQL Java:
@@ -310,7 +310,7 @@ defers until it is ready to batch load all related entities as one.
- `DataLoader` additionally maintains a cache of previously loaded entities that can
further improve efficiency when the same entity is in multiple places of the response.
Spring GraphQL exposes a `BatchLoaderRegistry` that accepts and stores registrations of
Spring GraphQL provides a `BatchLoaderRegistry` that accepts and stores registrations of
batch loading functions. The `ExecutionGraphQlService` accepts the registry as input and
uses it to make per request `DataLoader` registrations. A `DataFetcher` then looks up the
`DataLoader` for an entity and uses it to load instances, or in an annotated controller,

View File

@@ -29,7 +29,7 @@ import org.springframework.core.annotation.AliasFor;
*
* <pre class="code">
* &#064;BatchMapping
* public Flux&lt;Author&gt; author(List&lt;Book&gt; books) {
* public Mono&lt;Map&lt;Book, Author&gt;&gt; author(List&lt;Book&gt; books) {
* // ...
* }
* </pre>
@@ -47,17 +47,23 @@ import org.springframework.core.annotation.AliasFor;
* public class BookController {
*
* public BookController(BatchLoaderRegistry registry) {
* registry.forTypePair(Long.class, Author.class).registerBatchLoader((ids, environment) -> ...);
* registry.forTypePair(Long.class, Author.class).registerMappedBatchLoader((ids, environment) -> ...);
* }
*
* &#064;SchemaMapping
* public Author author(Book book, DataLoader&lt;Long, Author&gt; dataLoader) {
* public CompletableFuture&lt;Author&gt; author(Book book, DataLoader&lt;Long, Author&gt; dataLoader) {
* return dataLoader.load(book.getAuthorId());
* }
*
* }
* </pre>
*
* <p>In addition to returning {@code Mono<Map<K,T>>}, an {@code @BatchMapping}
* method can also return {@code Flux<T>}. However, in that case the returned
* sequence of values must match the number and order of the input keys. See
* {@link org.dataloader.BatchLoader} and {@link org.dataloader.MappedBatchLoader}
* for more details.
*
* @author Rossen Stoyanchev
* @since 1.0.0
*/