Challenge - 5 Problems
DataLoader Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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)); }
Attempts:
2 left
💡 Hint
The batch function must return results in the same order as the keys array.
✗ Incorrect
DataLoader expects the batch function to return an array of results matching the order of the input keys. The code maps keys to users by matching ids, preserving order.
❓ state_output
intermediate2: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;
Attempts:
2 left
💡 Hint
DataLoader caches results per key to avoid duplicate fetches.
✗ Incorrect
DataLoader caches the result for key 5 after the first load. The second load uses the cache, so only one cache entry exists.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
DataLoader constructor accepts batch function; context must be passed explicitly if needed.
✗ Incorrect
Option D passes context as an argument to the batch function. Options C and D misuse DataLoader options. Option D does not show context usage.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
DataLoader requires the batch function to return results in the same order as keys.
✗ Incorrect
If the results array order differs from keys, DataLoader will assign wrong results or undefined to keys.
🧠 Conceptual
expert2:00remaining
What is the main benefit of integrating DataLoader in a NestJS GraphQL resolver?
Why do developers integrate DataLoader in NestJS GraphQL resolvers?
Attempts:
2 left
💡 Hint
Think about how DataLoader helps with multiple requests for the same data.
✗ Incorrect
DataLoader batches multiple requests for the same data into one and caches results, reducing database load and improving response times.