0
0
GraphQLquery~20 mins

DataLoader batching and caching in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DataLoader Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does DataLoader batch requests?

Imagine you have multiple requests for user data by ID coming in quickly. How does DataLoader handle these requests?

AIt collects all requests within a tick and sends one batch request to the database.
BIt sends each request immediately to the database without waiting.
CIt caches the first request and ignores the rest.
DIt queues requests and sends them one by one with delays.
Attempts:
2 left
💡 Hint

Think about how batching reduces the number of database calls.

query_result
intermediate
2:00remaining
What is the output of this DataLoader cache test?

Given this DataLoader usage, what will be the value of count after all calls?

GraphQL
let count = 0;
const loader = new DataLoader(async keys => {
  count += 1;
  return keys.map(key => ({ id: key, name: `User${key}` }));
});

await Promise.all([
  loader.load(1),
  loader.load(1),
  loader.load(2)
]);

// What is count?
A1
B2
C0
D3
Attempts:
2 left
💡 Hint

Consider how caching affects repeated keys.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this DataLoader batch function

Which option contains a syntax error in the batch loading function?

GraphQL
const loader = new DataLoader(async keys => {
  return keys.map(key => {
    id: key,
    name: `User${key}`
  });
});
AIncorrect async keyword usage in batch function.
BMissing return statement inside map callback, causing undefined results.
CKeys parameter should be an object, not an array.
DUsing parentheses instead of braces for object return in map callback.
Attempts:
2 left
💡 Hint

Check how arrow functions return objects.

optimization
advanced
2:00remaining
How to optimize DataLoader cache for large datasets?

You have a DataLoader caching millions of keys. What is the best way to prevent memory issues?

AStore all keys permanently in the default cache.
BDisable caching and rely only on batching.
CIncrease Node.js memory limit to handle large cache.
DUse a custom cache with limited size and eviction policy.
Attempts:
2 left
💡 Hint

Think about controlling cache size to avoid memory overflow.

🔧 Debug
expert
2:00remaining
Why does this DataLoader batch function return results in wrong order?

Given this batch function, why might the results not match the order of keys?

GraphQL
const loader = new DataLoader(async keys => {
  const users = await database.getUsersByIds(keys);
  return users;
});
ABatch function must return a Map instead of an array.
BDataLoader requires keys to be sorted before batch function.
CThe database returns users in arbitrary order, not matching keys order.
DKeys array is mutated inside batch function causing mismatch.
Attempts:
2 left
💡 Hint

Consider how the database returns results compared to requested keys.