Skip to content

Commit 3cd3a43

Browse files
fix: maxBatchSize parameter was not being sent to loader (#321)
* Fixes #294. * Added changeset for fix.
1 parent 26136b0 commit 3cd3a43

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

.changeset/swift-buckets-drop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"dataloader": patch
3+
---
4+
5+
Resolves an issue where the maxBatchSize parameter wouldn't be fully used on each batch sent to the backend loader.

src/__tests__/dataloader.test.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ describe('Primary API', () => {
120120
expect(loadCalls).toEqual([ [ 1, 2 ], [ 3 ] ]);
121121
});
122122

123+
it('applies maxBatchSize correctly with duplicate keys', async () => {
124+
const [ identityLoader, loadCalls ] = idLoader<string>({
125+
maxBatchSize: 3,
126+
batchScheduleFn: callback => { setTimeout(callback, 100); },
127+
});
128+
129+
const values = [ 'a', 'b', 'a', 'a', 'a', 'b', 'c' ];
130+
const results = await Promise.all(values.map(
131+
value => identityLoader.load(value)
132+
));
133+
134+
expect(results).toEqual(values);
135+
expect(loadCalls).toEqual([ [ 'a', 'b', 'c' ] ]);
136+
});
137+
123138
it('batches cached requests', async () => {
124139
const loadCalls = [];
125140
let resolveBatch = () => {};
@@ -185,8 +200,9 @@ describe('Primary API', () => {
185200
// Move to next macro-task (tick)
186201
await new Promise(setImmediate);
187202

188-
// Promise 1 resolves first since max batch size is 1
189-
expect(promise1Resolved).toBe(true);
203+
// Promise 1 resolves first since max batch size is 1,
204+
// but it still hasn't resolved yet.
205+
expect(promise1Resolved).toBe(false);
190206
expect(promise2Resolved).toBe(false);
191207

192208
resolveBatch();

src/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ function getCurrentBatch<K, V>(loader: DataLoader<K, V, any>): Batch<K, V> {
268268
if (
269269
existingBatch !== null &&
270270
!existingBatch.hasDispatched &&
271-
existingBatch.keys.length < loader._maxBatchSize &&
272-
(!existingBatch.cacheHits ||
273-
existingBatch.cacheHits.length < loader._maxBatchSize)
271+
existingBatch.keys.length < loader._maxBatchSize
274272
) {
275273
return existingBatch;
276274
}

0 commit comments

Comments
 (0)