GraphQLDependency.load() method

Loads everything the request asked for beneath this type and attaches it to the result, in as few bulk calls as the graph allows.

Signature:

load(source: ResultType, context: any, fields: any): Promise<ResultType>;

Parameters

Parameter

Type

Description

source

ResultType

the objects already fetched by the resolver, one or many

context

any

the GraphQL resolver context, handed to every loader and initializer untouched

fields

any

the requested fields as a nested map, as produced by fieldsMap() from graphql-fields-list over the resolver's GraphQLResolveInfo

Returns:

Promise<ResultType>

source, with the requested dependencies attached

Remarks

This is the one runtime call. Everything else on this class is start-up declaration; here those declarations meet an actual query. Invoke it from a top-level resolver, after the initial service call, and hand it the fields the client asked for.

What it does, in order: scan the requested fields for types that have a dependency description; merge every request for the same type into one minimal field set; run the initializers and bulk loaders in dependency order, level by level and concurrently within a level; attach each loaded object to its parents; and return the result.

Two things to be aware of, both of which follow from matching by id. fields is mutated: id is added at every level of the map, since without it nothing can be attached. And source is mutated too — the dependency fields are written onto the very objects that were passed in, and the return value is that same object rather than a copy. Loaded children are shared by reference between the parents that match them, so a result graph stays cheap even when many parents point at the same child.

A falsy fields short-circuits: nothing is requested, so source comes back untouched. source may be a single object or an array of them.

Example

async function user(
    source: any,
    args: any,
    context: any,
    info: GraphQLResolveInfo,
) {
    const fields = fieldsMap(info);
    const data = await context.user.listUser(args);

    // fills in every dependent structure the query touched
    return Dependency(UserType).load(data, context, fields);
}

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.