-
-
Notifications
You must be signed in to change notification settings - Fork 827
Expand file tree
/
Copy pathcreateBatchDelegateFn.ts
More file actions
31 lines (27 loc) · 1.08 KB
/
createBatchDelegateFn.ts
File metadata and controls
31 lines (27 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import DataLoader from 'dataloader';
import { CreateBatchDelegateFnOptions, BatchDelegateOptionsFn, BatchDelegateFn } from './types';
import { getLoader } from './getLoader';
export function createBatchDelegateFn<K = any, V = any, C = K>(
optionsOrArgsFromKeys: CreateBatchDelegateFnOptions | ((keys: ReadonlyArray<K>) => Record<string, any>),
lazyOptionsFn?: BatchDelegateOptionsFn,
dataLoaderOptions?: DataLoader.Options<K, V, C>,
valuesFromResults?: (results: any, keys: ReadonlyArray<K>) => Array<V>
): BatchDelegateFn<K> {
return typeof optionsOrArgsFromKeys === 'function'
? createBatchDelegateFnImpl({
argsFromKeys: optionsOrArgsFromKeys,
lazyOptionsFn,
dataLoaderOptions,
valuesFromResults,
})
: createBatchDelegateFnImpl(optionsOrArgsFromKeys);
}
function createBatchDelegateFnImpl<K = any>(options: CreateBatchDelegateFnOptions): BatchDelegateFn<K> {
return batchDelegateOptions => {
const loader = getLoader({
...options,
...batchDelegateOptions,
});
return loader.load(batchDelegateOptions.key);
};
}