Skip to content

Commit bb8a7aa

Browse files
jorisrolingleebyron
authored andcommitted
use batchLoadFn.apply instead of directly invoking batchLoadFn (#182)
* use batchLoadFn.apply instead of directly invoking batchLoadFn use batchLoadFn.apply(loader,[keys]) instead of batchLoadFn(keys) to allow acces to the actual dataloader. Needed for example to use dataloader.prime() in the batch loader function. * Add tests
1 parent cffec0e commit bb8a7aa

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,19 @@ minimal outgoing data requests.
9090
#### Batch Function
9191

9292
A batch loading function accepts an Array of keys, and returns a Promise which
93-
resolves to an Array of values. There are a few constraints that must be upheld:
93+
resolves to an Array of values or Error instances. The loader itself is provided
94+
as the `this` context.
95+
96+
```js
97+
async function batchFunction(keys) {
98+
const results = await db.fetchAllKeys(keys)
99+
return keys.map(key => results[key] || new Error(`No result for ${key}`))
100+
}
101+
102+
const loader = new DataLoader(batchFunction)
103+
```
104+
105+
There are a few constraints this function must uphold:
94106

95107
* The Array of values must be the same length as the Array of keys.
96108
* Each index in the Array of values must correspond to the same index in the Array of keys.
@@ -116,7 +128,7 @@ with the original keys `[ 2, 9, 6, 1 ]`:
116128
[
117129
{ id: 2, name: 'San Francisco' },
118130
{ id: 9, name: 'Chicago' },
119-
null,
131+
null, // or perhaps `new Error()`
120132
{ id: 1, name: 'New York' }
121133
]
122134
```

src/__tests__/dataloader.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ describe('Primary API', () => {
3030
expect(value1).toBe(1);
3131
});
3232

33+
it('references the loader as "this" in the batch function', async () => {
34+
let that;
35+
const loader = new DataLoader(async function (keys) {
36+
that = this;
37+
return keys;
38+
});
39+
40+
// Trigger the batch function
41+
await loader.load(1);
42+
43+
expect(that).toBe(loader);
44+
});
45+
3346
it('supports loading multiple keys in one call', async () => {
3447
const identityLoader = new DataLoader(keys => Promise.resolve(keys));
3548

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ function dispatchQueueBatch<K, V>(
253253

254254
// Call the provided batchLoadFn for this loader with the loader queue's keys.
255255
var batchLoadFn = loader._batchLoadFn;
256-
var batchPromise = batchLoadFn(keys);
256+
// Call with the loader as the `this` context.
257+
var batchPromise = batchLoadFn.call(loader, keys);
257258

258259
// Assert the expected response from batchLoadFn
259260
if (!batchPromise || typeof batchPromise.then !== 'function') {

0 commit comments

Comments
 (0)