From a5c6034273ed14e5c36754d2ef9ab3340bf0d7cd Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Mon, 5 Dec 2022 18:53:18 +1100 Subject: [PATCH 1/2] Fixes #294. --- src/__tests__/dataloader.test.js | 20 ++++++++++++++++++-- src/index.js | 4 +--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/__tests__/dataloader.test.js b/src/__tests__/dataloader.test.js index e53483f..a32a60b 100644 --- a/src/__tests__/dataloader.test.js +++ b/src/__tests__/dataloader.test.js @@ -120,6 +120,21 @@ describe('Primary API', () => { expect(loadCalls).toEqual([ [ 1, 2 ], [ 3 ] ]); }); + it('applies maxBatchSize correctly with duplicate keys', async () => { + const [ identityLoader, loadCalls ] = idLoader({ + maxBatchSize: 3, + batchScheduleFn: callback => { setTimeout(callback, 100); }, + }); + + const values = [ 'a', 'b', 'a', 'a', 'a', 'b', 'c' ]; + const results = await Promise.all(values.map( + value => identityLoader.load(value) + )); + + expect(results).toEqual(values); + expect(loadCalls).toEqual([ [ 'a', 'b', 'c' ] ]); + }); + it('batches cached requests', async () => { const loadCalls = []; let resolveBatch = () => {}; @@ -185,8 +200,9 @@ describe('Primary API', () => { // Move to next macro-task (tick) await new Promise(setImmediate); - // Promise 1 resolves first since max batch size is 1 - expect(promise1Resolved).toBe(true); + // Promise 1 resolves first since max batch size is 1, + // but it still hasn't resolved yet. + expect(promise1Resolved).toBe(false); expect(promise2Resolved).toBe(false); resolveBatch(); diff --git a/src/index.js b/src/index.js index 87dade9..a80bc2b 100644 --- a/src/index.js +++ b/src/index.js @@ -268,9 +268,7 @@ function getCurrentBatch(loader: DataLoader): Batch { if ( existingBatch !== null && !existingBatch.hasDispatched && - existingBatch.keys.length < loader._maxBatchSize && - (!existingBatch.cacheHits || - existingBatch.cacheHits.length < loader._maxBatchSize) + existingBatch.keys.length < loader._maxBatchSize ) { return existingBatch; } From 702efba6a3ec857ba8f3cf3e12f42059c65ff043 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Tue, 6 Dec 2022 10:04:29 +1100 Subject: [PATCH 2/2] Added changeset for fix. --- .changeset/swift-buckets-drop.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/swift-buckets-drop.md diff --git a/.changeset/swift-buckets-drop.md b/.changeset/swift-buckets-drop.md new file mode 100644 index 0000000..db803dd --- /dev/null +++ b/.changeset/swift-buckets-drop.md @@ -0,0 +1,5 @@ +--- +"dataloader": patch +--- + +Resolves an issue where the maxBatchSize parameter wouldn't be fully used on each batch sent to the backend loader.