Skip to content

Commit

Permalink
feat(data-loader): pass loader context to loadMany (#36)
Browse files Browse the repository at this point in the history
This allows loaders to reference other loaders
  • Loading branch information
CarsonF authored Sep 17, 2024
1 parent af90dbe commit d7b2851
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
7 changes: 4 additions & 3 deletions packages/data-loader/src/data-loader.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ export class DataLoaderContext {
});
}

private createContext(): LoaderContextType {
private createContext() {
const contextId = ContextIdFactory.create();
const loaders: LoaderContextType['loaders'] = new Map();
return {
const loaderContext: LoaderContextType = {
contextId,
loaders,
getLoader: cacheable(loaders, (strategyType) =>
this.factory.create(strategyType, contextId),
this.factory.create(strategyType, contextId, loaderContext),
),
};
return loaderContext;
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/data-loader/src/data-loader.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Inject, Injectable, Type } from '@nestjs/common';
import { ContextId, ModuleRef } from '@nestjs/core';
import DataLoaderLib from 'dataloader';
import type { DataLoaderOptions } from './data-loader-options.type';
import type { LoaderContextType } from './data-loader.context';
import { MODULE_OPTIONS_TOKEN } from './data-loader.module-builder';
import { DataLoaderStrategy } from './data-loader.strategy';
import { DataLoader } from './data-loader.type';
Expand All @@ -27,6 +28,7 @@ export class DataLoaderFactory {
async create<Item, Key, CachedKey = Key>(
strategyType: Type<DataLoaderStrategy<Item, Key, CachedKey>>,
contextId: ContextId,
loaderContext: LoaderContextType,
): Promise<DataLoader<Item, Key, CachedKey>> {
const strategy = await this.moduleRef.resolve<
DataLoaderStrategy<Item, Key, CachedKey>
Expand All @@ -35,7 +37,7 @@ export class DataLoaderFactory {
const options = this.resolveOptions(strategy, this.defaultOptions);

const loader = new DataLoaderLib<Key, Item, CachedKey>(
this.makeBatchFn(strategy, options),
this.makeBatchFn(strategy, options, loaderContext),
options,
) as DataLoader<Item, Key, CachedKey>;

Expand Down Expand Up @@ -72,11 +74,12 @@ export class DataLoaderFactory {
protected makeBatchFn<Item, Key, CachedKey = Key>(
strategy: DataLoaderStrategy<Item, Key, CachedKey>,
options: ResolvedDataLoaderOptions<Item, Key, CachedKey>,
loaderContext: LoaderContextType,
) {
const { propertyKey: getKey, cacheKeyFn: getCacheKey } = options;

const batchFn: DataLoaderLib.BatchLoadFn<Key, Item> = async (keys) => {
const docs = await strategy.loadMany(keys);
const docs = await strategy.loadMany(keys, loaderContext);

// Put documents (docs) into a map where key is a document's ID or some
// property (prop) of a document and value is a document.
Expand Down
2 changes: 2 additions & 0 deletions packages/data-loader/src/data-loader.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable @typescript-eslint/method-signature-style */
import { DataLoaderOptions } from './data-loader-options.type';
import { LoaderContextType } from './data-loader.context';

export interface DataLoaderStrategy<Item, Key, CachedKey = Key> {
loadMany(
keys: readonly Key[],
loaderContext: LoaderContextType,
): Promise<ReadonlyArray<Item | { key: Key; error: Error }>>;

getOptions?: () => DataLoaderOptions<Item, Key, CachedKey>;
Expand Down

0 comments on commit d7b2851

Please sign in to comment.