From 483f743683cdb793b33690e7b494a966a4759606 Mon Sep 17 00:00:00 2001 From: Peter Sieg Date: Mon, 3 Aug 2020 17:09:45 -0400 Subject: [PATCH] [BREAKING] Update types to allow array of promises in batchLoadFn return --- README.md | 3 ++- src/__tests__/dataloader.test.js | 17 +++++++++++++++++ src/index.js | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e192a18..c2f5b28 100644 --- a/README.md +++ b/README.md @@ -389,7 +389,8 @@ access permissions and consider creating a new instance per web request. Create a new `DataLoader` given a batch loading function and options. - *batchLoadFn*: A function which accepts an Array of keys, and returns a - Promise which resolves to an Array of values. + Promise which resolves to an Array of values or promises which resolve to + values. - *options*: An optional object of options: diff --git a/src/__tests__/dataloader.test.js b/src/__tests__/dataloader.test.js index 47b2059..63ec745 100644 --- a/src/__tests__/dataloader.test.js +++ b/src/__tests__/dataloader.test.js @@ -91,6 +91,23 @@ describe('Primary API', () => { expect(values).toEqual([ 'a', 'b', new Error('Bad Key') ]); }); + it('supports loading with resolved and rejected promises', async () => { + const identityLoader = new DataLoader(keys => + Promise.resolve( + keys.map(key => (key === 'bad' ? + Promise.reject(new Error('Bad Key')) : + Promise.resolve(key)) + ) + ) + ); + + const promiseAll = identityLoader.loadMany([ 'a', 'b', 'bad' ]); + expect(promiseAll).toBeInstanceOf(Promise); + + const values = await promiseAll; + expect(values).toEqual([ 'a', 'b', new Error('Bad Key') ]); + }); + it('batches multiple requests', async () => { const [ identityLoader, loadCalls ] = idLoader(); diff --git a/src/index.js b/src/index.js index 6250df9..f48e26c 100644 --- a/src/index.js +++ b/src/index.js @@ -10,7 +10,7 @@ // A Function, which when given an Array of keys, returns a Promise of an Array // of values or Errors. export type BatchLoadFn = - (keys: $ReadOnlyArray) => Promise<$ReadOnlyArray>; + (keys: $ReadOnlyArray) => Promise<$ReadOnlyArray | Error>>; // Optionally turn off batching or caching or provide a cache key function or a // custom cache instance.