GraphQLDependency.require() method
Declares that this type owns a child type, and how the child's objects are found and where they are attached.
Signature:
require(child: GraphQLObjectType, ...options: DependencyOptionsGetter[]): GraphQLDependency<ResultType>;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
child |
GraphQLObjectType |
the child type this type depends on |
|
options |
one getter per relation to |
Returns:
GraphQLDependency<ResultType>
this description, so calls can be chained
Remarks
Each requirement is a pair. as is the field on *this* type the loaded children are written to — whether one child or a list is attached is decided by that field's own GraphQL type. filter maps a key of the child's loader filter to the field on *this* type whose values fill it, so it reads child-side key first, parent-side source second.
Passing several requirements for one child type is the normal case rather than a special one: a company relating to users as both owner and employees is two requirements naming UserType, differing in as and in which field feeds the filter.
Requirements are given as thunks because the types they reference are usually still being defined when this runs — a GraphQLObjectType with circular references only has its fields once the schema settles, so the getters are called at request time, not now.
A repeat call for the same child type replaces that type's requirements rather than adding to them; list every relation to a type in one call.
Example
Dependency(CompanyType).require(
UserType,
() => ({
as: CompanyType.getFields().employees,
filter: {
[UserType.getFields().companyId.name]:
CompanyType.getFields().id,
},
}),
() => ({
as: CompanyType.getFields().owner,
filter: {
[UserType.getFields().id.name]:
CompanyType.getFields().ownerId,
},
}),
);
Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.