GraphQLDependency.defineLoader() method
Declares how to fetch many objects of this type in one call, which is what makes the type usable as another type's dependency.
Signature:
defineLoader<T>(loader: DataLoader<T>): GraphQLDependency<ResultType>;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
loader |
DataLoader<T> |
bulk fetch for this type |
Returns:
GraphQLDependency<ResultType>
this description, so calls can be chained
Remarks
The loader receives a filter assembled from the parent objects — its shape is whatever GraphQLDependency.require() maps into it — together with the merged set of fields the query asked for, so both can be pushed down to the service or store behind it.
Two things are required of it. Every object it returns must carry an id, because results are keyed by id and attached to parents by id. And it must accept a *set* of values per filter key rather than one, since fetching a whole level of parents in a single call is the entire point.
Declaring a loader is also what makes a type eligible to be require()d elsewhere. A type with requirements but no loader is not an error: no call is made for it, and its own children are still resolved against whatever data the parent result already holds.
Example
Dependency(UserType).defineLoader(async (
context: any,
filter: FiltersInput,
fields?: FieldsMapInput,
) => (await context.user.listUser(filter, fields)).data);
Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.