Description
I am experiencing a weird edge case with data loader.
Suppose we have functions like below.
const firstQueryBatch = (ids: string[]) => do something batch with Ids...
const secondQueryNonBatch = (id: string) => ... do something with single id
const thirdQueryBatch = (ids: string[]) => ... do something batch with ids
const callThreeQueriesWithId = async (id: string) => {
const firstResult = await (() => {
new DataLoader(firstQueryBatch, {
cache: false,
});
return (id: string) => loader.load(id)
})();
const secondResult = await secondQueryNonBatch(id); // non-data loader applied function
const thirdResult = await (() => {
new DataLoader(thirdQueryBatch, {
cache: false,
});
return (id: string) => loader.load(id)
})();
return {
firstResult,
secondResult,
thirdResult,
};
};
const Ids = ['1', '2', '3'];
const finalResults = await Promise.all(Ids.map((id) => await callThreeQueriesWithId(id))));
Let's say there are total three async functions where the first and the last function use dataloader, but second function is just a plain non-dataloader function.
I simply expected that 'firstQueryBatch' and 'thirdQueryBatch' would be called once with batch Ids[](as dataloader should combine parameters) and only calling 'secondQueryNonBatch' three times as it is not a dataloader function.
However, when I debug the output, once it calls 'firstQueryBatch' once successfully, it ended up calling 'secondQueryNonBatch' AND 'thirdQueryBatch' three times. Dataloader fails to combine parameters for 'thirdQueryBatch' and calling it three times with parameters with single item in each array. ['1'], ['2'], then ['3'].
Could anyone explain why this is happening?