Skip to content

Commit

Permalink
feat(batch-delegate): added mapping function to createBatchDelegateFn (
Browse files Browse the repository at this point in the history
…#1825)

* Added mapping function to createBatchDelegateFn.

This is useful to reverse more complex argsFn processing, as well as more simple cases in which results may not be in identical order as arguments.

Authored-by: Blake Jackson <blakejackson@Blakes-MBP.lan>
  • Loading branch information
jakeblaxon committed Jul 29, 2020
1 parent 0008973 commit cb7385f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/batch-delegate/src/createBatchDelegateFn.ts
Expand Up @@ -9,17 +9,19 @@ import { BatchDelegateOptionsFn, BatchDelegateFn, BatchDelegateOptions } from '.
export function createBatchDelegateFn<K = any, V = any, C = K>(
argFn: (args: ReadonlyArray<K>) => Record<string, any>,
batchDelegateOptionsFn: BatchDelegateOptionsFn,
dataLoaderOptions?: DataLoader.Options<K, V, C>
dataLoaderOptions?: DataLoader.Options<K, V, C>,
resultsFn?: (results: any, keys: ReadonlyArray<K>) => V[]
): BatchDelegateFn<K> {
let cache: WeakMap<ReadonlyArray<FieldNode>, DataLoader<K, V, C>>;

function createBatchFn(options: BatchDelegateOptions) {
return async (keys: ReadonlyArray<K>) => {
const results = await delegateToSchema({
let results = await delegateToSchema({
returnType: new GraphQLList(getNamedType(options.info.returnType) as GraphQLOutputType),
args: argFn(keys),
...batchDelegateOptionsFn(options),
});
results = resultsFn ? resultsFn(results, keys) : results;
return Array.isArray(results) ? results : keys.map(() => results);
};
}
Expand Down

0 comments on commit cb7385f

Please sign in to comment.