Bird
Raised Fist0
NestJSframework~20 mins

DataLoader integration in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using DataLoader in a NestJS application?
easy
A. To handle HTTP routing and middleware
B. To serve static files efficiently
C. To manage database schema migrations
D. To batch and cache multiple data requests to improve performance

Solution

  1. Step 1: Understand DataLoader's role

    DataLoader is designed to batch and cache data fetching operations to reduce redundant calls.
  2. Step 2: Compare with other options

    Options A, C, and D relate to other NestJS features, not DataLoader's purpose.
  3. Final Answer:

    To batch and cache multiple data requests to improve performance -> Option D
  4. Quick Check:

    DataLoader = batch + cache [OK]
Hint: DataLoader batches requests to avoid repeated fetching [OK]
Common Mistakes:
  • Confusing DataLoader with routing or middleware
  • Thinking DataLoader manages database migrations
  • Assuming DataLoader serves static files
2. Which of the following is the correct way to create a DataLoader instance inside a NestJS service?
easy
A. const loader = new DataLoader(batchFunction());
B. const loader = DataLoader.create(batchFunction);
C. const loader = new DataLoader(keys => batchFunction(keys));
D. const loader = DataLoader(batchFunction);

Solution

  1. Step 1: Recall DataLoader instantiation syntax

    DataLoader is instantiated with new DataLoader(batchFunction), where batchFunction is a function receiving keys.
  2. Step 2: Analyze options

    const loader = new DataLoader(keys => batchFunction(keys)); correctly uses new DataLoader(keys => batchFunction(keys)). Options B, C, and D use incorrect syntax or call batchFunction immediately.
  3. Final Answer:

    const loader = new DataLoader(keys => batchFunction(keys)); -> Option C
  4. Quick Check:

    Use new DataLoader(batchFunction) [OK]
Hint: Use 'new DataLoader' with a batch function callback [OK]
Common Mistakes:
  • Calling batchFunction immediately instead of passing it
  • Using DataLoader as a function without 'new'
  • Using a non-function argument
3. Given the following batch function in a NestJS DataLoader service:
async function batchUsers(ids: readonly number[]) {
  return ids.map(id => ({ id, name: `User${id}` }));
}

What will be the output of await loader.load(2) if loader is created with this batch function?
medium
A. { id: 2, name: 'User2' }
B. [{ id: 2, name: 'User2' }]
C. undefined
D. Error: batch function must return array of same length

Solution

  1. Step 1: Understand batch function output

    The batch function returns an array of user objects matching the input ids in order.
  2. Step 2: Understand loader.load behavior

    loader.load(2) returns a single user object corresponding to id 2, not an array.
  3. Final Answer:

    { id: 2, name: 'User2' } -> Option A
  4. Quick Check:

    load(id) returns single item from batch array [OK]
Hint: load() returns one item, batch returns array [OK]
Common Mistakes:
  • Expecting load() to return an array
  • Confusing batch function return with load return
  • Assuming batch function returns undefined
4. You wrote this batch function for DataLoader:
async function batchPosts(ids: readonly number[]) {
  const posts = await getPostsByIds(ids);
  return posts;
}

But you get wrong results when loading posts. What is the likely problem?
medium
A. The returned array posts is not in the same order as ids
B. The batch function should not be async
C. You must return a Map instead of an array
D. The function should return a Promise of a single post, not an array

Solution

  1. Step 1: Check batch function contract

    DataLoader requires the batch function to return results in the same order as the keys array.
  2. Step 2: Analyze getPostsByIds behavior

    If getPostsByIds returns posts unordered, the batch function breaks this contract causing wrong results.
  3. Final Answer:

    The returned array posts is not in the same order as ids -> Option A
  4. Quick Check:

    Batch function must preserve order [OK]
Hint: Batch function output order must match input keys order [OK]
Common Mistakes:
  • Ignoring order of returned array
  • Thinking batch function can't be async
  • Returning Map instead of array
5. In a NestJS GraphQL resolver, you want to integrate DataLoader to batch user requests per HTTP request. Which approach correctly ensures a new DataLoader instance is created for each request to avoid caching issues across users?
hard
A. Create a DataLoader instance once in the module constructor
B. Create a DataLoader instance inside a request-scoped provider injected into the resolver
C. Create a single global DataLoader instance as a singleton service
D. Use a static DataLoader instance shared across all requests

Solution

  1. Step 1: Understand DataLoader caching scope

    DataLoader caches results per instance, so sharing one instance globally causes cross-request data leaks.
  2. Step 2: Use request-scoped provider

    Creating a DataLoader instance per request via a request-scoped provider ensures isolated caches per user request.
  3. Final Answer:

    Create a DataLoader instance inside a request-scoped provider injected into the resolver -> Option B
  4. Quick Check:

    Request-scoped DataLoader avoids cross-request cache [OK]
Hint: Use request-scoped provider for per-request DataLoader [OK]
Common Mistakes:
  • Using singleton DataLoader causing shared cache
  • Creating DataLoader once in module constructor
  • Using static instance shared globally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard