Skip to content

Commit

Permalink
Fixes graphql#297.
Browse files Browse the repository at this point in the history
  • Loading branch information
thekevinbrown committed Dec 5, 2022
1 parent 015a94c commit 2d4cffc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 18 additions & 2 deletions src/__tests__/dataloader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>({
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 = () => {};
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ function getCurrentBatch<K, V>(loader: DataLoader<K, V, any>): Batch<K, V> {
if (
existingBatch !== null &&
!existingBatch.hasDispatched &&
existingBatch.keys.length < loader._maxBatchSize &&
(!existingBatch.cacheHits ||
existingBatch.cacheHits.length < loader._maxBatchSize)
existingBatch.keys.length < loader._maxBatchSize
) {
return existingBatch;
}
Expand Down

0 comments on commit 2d4cffc

Please sign in to comment.