Update Javadoc for BatchMapping
Closes gh-166
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.core.annotation.AliasFor;
|
||||
*
|
||||
* <pre class="code">
|
||||
* @BatchMapping
|
||||
* public Flux<Author> author(List<Book> books) {
|
||||
* public Mono<Map<Book, Author>> author(List<Book> 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) -> ...);
|
||||
* }
|
||||
*
|
||||
* @SchemaMapping
|
||||
* public Author author(Book book, DataLoader<Long, Author> dataLoader) {
|
||||
* public CompletableFuture<Author> author(Book book, DataLoader<Long, Author> 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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user