# GraphQLDependency.defineInitializer() method · @imqueue/graphql-dependency

Source: https://imqueue.org/api/graphql-dependency/latest/graphql-dependency.graphqldependency.defineinitializer/
Published: 2026-07-31
Author: @imqueue maintainers (https://github.com/imqueue)
Package: @imqueue/graphql-dependency 3.1.0 — generated reference, not hand-written

Declares an async routine that fills extra fields onto this type's own objects before its dependencies are loaded.

**Signature:**

```typescript
defineInitializer(initializer: DataInitializer<ResultType>, ...fields: DependencyFieldsGetter[]): GraphQLDependency<ResultType>;
```

## Parameters


| Parameter | Type | Description |
| --- | --- | --- |
| initializer | [DataInitializer](https://imqueue.org/api/graphql-dependency/latest/graphql-dependency.datainitializer/)<ResultType> | async routine returning a map of object id to the extra fields to merge onto that object |
| fields | [DependencyFieldsGetter](https://imqueue.org/api/graphql-dependency/latest/graphql-dependency.dependencyfieldsgetter/)\[\] | accessors for the fields the initializer fills; supply them so dependencies that do not read them need not wait |


**Returns:**

[GraphQLDependency](https://imqueue.org/api/graphql-dependency/latest/graphql-dependency.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


```typescript
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,
);
```

