Support DataLoader in EntityMapping methods
Closes gh-1095
This commit is contained in:
@@ -80,11 +80,8 @@ xref:federation.adoc#federation.entity-mapping.signature[Method Signature] for s
|
||||
method argument and return value types.
|
||||
<2> `@SchemaMapping` methods can be used for the rest of the graph.
|
||||
|
||||
An `@EntityMapping` method can batch load federated entities of a given type. To do that,
|
||||
declare the `@Argument` method parameter as a list, and return the corresponding entity
|
||||
instances as a list in the same order.
|
||||
|
||||
For example:
|
||||
You can load federated entities of the same type together by accepting a `List` of id's,
|
||||
and returning a `List` or `Flux` of entities:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
@@ -108,6 +105,35 @@ look up the correct value in the "representation" input map. You can also set th
|
||||
argument name through the annotation.
|
||||
<2> `@BatchMapping` methods can be used for the rest of the graph.
|
||||
|
||||
You can load federated entities with a `DataLoader`:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
@Controller
|
||||
private static class BookController {
|
||||
|
||||
@Autowired
|
||||
public DataLoaderBookController(BatchLoaderRegistry registry) { // <1>
|
||||
registry.forTypePair(Integer.class, Book.class).registerBatchLoader((bookIds, environment) -> {
|
||||
// load entities...
|
||||
});
|
||||
}
|
||||
|
||||
@EntityMapping
|
||||
public Future<Book> book(@Argument int id, DataLoader<Integer, Book> dataLoader) { // <2>
|
||||
return dataLoader.load(id);
|
||||
}
|
||||
|
||||
@BatchMapping
|
||||
public Map<Book, Author> author(List<Book> books) { // <3>
|
||||
// ...
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
<1> Register a batch loader for the federated entity type.
|
||||
<2> Declare a `DataLoader` argument to the `@EntityMapping` method.
|
||||
<3> `@BatchMapping` methods can be used for the rest of the graph.
|
||||
|
||||
|
||||
[[federation.entity-mapping.signature]]
|
||||
@@ -153,6 +179,9 @@ Entity mapping methods support the following arguments:
|
||||
| `DataFetchingEnvironment`
|
||||
| For direct access to the underlying `DataFetchingEnvironment`.
|
||||
|
||||
| `DataLoader<I, E>`
|
||||
| To load federated entities with a `DataLoader` where `I` is the id type, and `E` is the entity type.
|
||||
|
||||
|===
|
||||
|
||||
`@EntityMapping` methods can return `Mono`, `CompletableFuture`, `Callable`, or the actual entity.
|
||||
|
||||
Reference in New Issue
Block a user