0
0
NestJSframework~20 mins

DataLoader integration in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DataLoader Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this DataLoader batch function return?
Consider this NestJS DataLoader batch function that fetches users by IDs. What will be the output array order if the input keys are [3, 1, 2]?
NestJS
async function batchUsers(keys: readonly number[]) {
  const users = await userService.findByIds(keys as number[]);
  return keys.map(key => users.find(user => user.id === key));
}
A[User with id 3, User with id 1, User with id 2]
B[User with id 1, User with id 2, User with id 3]
C[User with id 2, User with id 3, User with id 1]
D[undefined, undefined, undefined]
Attempts:
2 left
💡 Hint
The batch function must return results in the same order as the keys array.
state_output
intermediate
2:00remaining
What is the value of cache after loading twice with the same key?
Given this NestJS DataLoader instance, what is the size of the internal cache after these calls? const loader = new DataLoader(async keys => keys.map(k => k * 2)); await loader.load(5); await loader.load(5);
NestJS
const loader = new DataLoader(async keys => keys.map(k => k * 2));
await loader.load(5);
await loader.load(5);
const cacheSize = loader._cacheMap.size;
AThrows error because _cacheMap is private
B1
C2
D0
Attempts:
2 left
💡 Hint
DataLoader caches results per key to avoid duplicate fetches.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a DataLoader with context access in NestJS?
You want to create a DataLoader inside a NestJS resolver that accesses the request context. Which code snippet is correct?
Aconst loader = new DataLoader(async keys => fetchUsers(keys), { cacheKeyFn: this.context });
Bconst loader = new DataLoader(async keys => fetchUsers(keys)); // context accessed via closure
Cconst loader = new DataLoader(async keys => fetchUsers(keys), { context: this.context });
Dconst loader = new DataLoader(async keys => fetchUsers(keys, this.context));
Attempts:
2 left
💡 Hint
DataLoader constructor accepts batch function; context must be passed explicitly if needed.
🔧 Debug
advanced
2:00remaining
Why does this DataLoader batch function lead to incorrect results?
Examine this batch function used in NestJS DataLoader. Why does it lead to incorrect results? async function batch(keys) { const results = await userService.findByIds(keys); return results; }
NestJS
async function batch(keys) {
  const results = await userService.findByIds(keys);
  return results;
}
ABatch function must return a Promise of Map, not array
BuserService.findByIds is not async, causing await to fail
CThe returned array order may not match keys order, causing undefined results
DKeys parameter must be an object, not array
Attempts:
2 left
💡 Hint
DataLoader requires the batch function to return results in the same order as keys.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of integrating DataLoader in a NestJS GraphQL resolver?
Why do developers integrate DataLoader in NestJS GraphQL resolvers?
ATo batch and cache database requests, reducing redundant queries and improving performance
BTo automatically validate GraphQL schema types at runtime
CTo replace the need for service layers in NestJS applications
DTo enable real-time subscriptions without additional setup
Attempts:
2 left
💡 Hint
Think about how DataLoader helps with multiple requests for the same data.