GraphQLDependency.defineInitializer() method

Declares an async routine that fills extra fields onto this type's own objects before its dependencies are loaded.

Signature:

defineInitializer(initializer: DataInitializer<ResultType>, ...fields: DependencyFieldsGetter[]): GraphQLDependency<ResultType>;

Parameters

Parameter

Type

Description

initializer

DataInitializer<ResultType>

async routine returning a map of object id to the extra fields to merge onto that object

fields

DependencyFieldsGetter[]

accessors for the fields the initializer fills; supply them so dependencies that do not read them need not wait

Returns:

GraphQLDependency<ResultType>

this description, so calls can be chained

Remarks

Reach for it when a dependency's filter needs a value the initial result does not carry — a list of foreign ids that has to be fetched or derived first, typically. The initializer returns a map keyed by object id, and each entry is merged onto the matching object with Object.assign; source objects without an id are skipped.

Naming the fields it fills is optional but worth doing. Given them, loading only waits for the initializer when some dependency's filter actually reads one of those fields, and everything else starts alongside it. Leave them out and there is no way to tell, so every dependency at this level waits — correct, but serialised.

Example

Dependency(UserType).defineInitializer(
    async (context: any, result: User[]) => {
        const ids = result.map(user => user.id);
        const orders = await context.order.listByUser(ids);

        // keyed by user id; merged onto the matching user, so the
        // orderId filter below has a value to work from
        return orders;
    },
    () => UserType.getFields().orderId,
    () => UserType.getFields().shipmentIds,
);

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