Complete the code to import DataLoader in a NestJS service.
import [1] from 'dataloader';
You need to import DataLoader exactly as it is exported from the 'dataloader' package.
Complete the code to create a new DataLoader instance in a NestJS service.
const userLoader = new DataLoader([1]);The DataLoader constructor expects an object with a batchLoadFn property that is a function to batch load keys.
Fix the error in the batch loading function signature for DataLoader.
async function batchLoadUsers([1]) { return users; }
The batch loading function receives an array of keys, commonly named keys.
Fill both blanks to create a DataLoader that batches user IDs and returns users.
const userLoader = new DataLoader(async ([1]) => { const users = await this.userService.findByIds([2]); return [1].map(id => users.find(user => user.id === id)); });
The batch function receives keys and passes userIds to the service. Then it maps keys to the found users.
Fill all three blanks to use DataLoader in a NestJS resolver method to load a user by ID.
async getUser(id: string) {
const user = await this.[1].load([2]);
return user ? [3] : null;
}The method calls userLoader.load(id) and returns the loaded user or null.