0
0
GraphQLquery~20 mins

DataLoader for batching in GraphQL - 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!
query_result
intermediate
2:00remaining
What is the output of this DataLoader batch function?

Consider a DataLoader batch function that receives an array of user IDs and returns user objects in the same order.

Given the following batch function:

async function batchUsers(userIds) {
  const users = await db.query('SELECT * FROM users WHERE id IN (?)', [userIds]);
  return userIds.map(id => users.find(user => user.id === id));
}

What will be the output if userIds is [3, 1, 2] and the database returns users in order [{id:1, name:'Alice'}, {id:2, name:'Bob'}, {id:3, name:'Carol'}]?

GraphQL
async function batchUsers(userIds) {
  const users = await db.query('SELECT * FROM users WHERE id IN (?)', [userIds]);
  return userIds.map(id => users.find(user => user.id === id));
}
A[{id:3, name:'Carol'}, undefined, {id:2, name:'Bob'}]
B[{id:1, name:'Alice'}, {id:2, name:'Bob'}, {id:3, name:'Carol'}]
C[{id:3, name:'Carol'}, {id:1, name:'Alice'}, {id:2, name:'Bob'}]
D[undefined, undefined, undefined]
Attempts:
2 left
💡 Hint

Remember DataLoader expects the batch function to return results in the same order as the keys.

🧠 Conceptual
intermediate
1:30remaining
Why use DataLoader in GraphQL APIs?

What is the main reason to use DataLoader in a GraphQL API?

ATo validate GraphQL schema types automatically
BTo batch and cache database requests to reduce redundant queries
CTo generate GraphQL queries dynamically
DTo encrypt data sent over the network
Attempts:
2 left
💡 Hint

Think about performance optimization when fetching data.

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

Which option contains a syntax error in the batch function for DataLoader?

const batchFunction = async (keys) => {
  const results = await db.query('SELECT * FROM items WHERE id IN (?)', [keys]);
  return keys.map(key => results.find(item => item.id === key));
}
A
}
;))yek === di.meti >= meti(dnif.stluser >= yek(pam.syek nruter  
;)]syek[ ,')?( NI di EREHW smeti MORF * TCELES'(yreuq.bd tiawa = stluser tsnoc  
{ >= )syek( cnysa = noitcnuFhctab tsnoc
B
const batchFunction = async (keys) => {
  const results = await db.query('SELECT * FROM items WHERE id IN (?)', keys);
  return keys.map(key => results.find(item => item.id === key));
}
C
const batchFunction = async keys => {
  const results = await db.query('SELECT * FROM items WHERE id IN (?)', keys);
  return keys.map(key => results.find(item => item.id === key));
}
D
const batchFunction = async (keys) => {
  const results = await db.query('SELECT * FROM items WHERE id IN (?)', [keys]);
  return keys.map(key => results.find(item => item.id === key));
}
Attempts:
2 left
💡 Hint

Look carefully at the comparison operator inside the find callback.

optimization
advanced
2:00remaining
How to optimize DataLoader batch function for large key arrays?

You have a DataLoader batch function that receives a large array of keys. Which approach optimizes performance best?

ASplit keys into smaller chunks and query the database multiple times in parallel, then combine results
BQuery the database once with all keys in a single IN clause
CCache only the first 10 keys and ignore the rest
DQuery the database separately for each key sequentially
Attempts:
2 left
💡 Hint

Consider database query limits and parallelism.

🔧 Debug
expert
2:30remaining
Why does this DataLoader batch function cause incorrect results?

Given this batch function:

async function batch(keys) {
  const rows = await db.query('SELECT * FROM products WHERE id IN (?)', [keys]);
  return rows;
}

Why might this cause incorrect results when used with DataLoader?

ABecause the function returns rows in arbitrary order, not matching the order of keys
BBecause the query uses IN (?) which is invalid syntax
CBecause the function does not cache results
DBecause the function does not handle errors
Attempts:
2 left
💡 Hint

Think about DataLoader's requirement for batch function output order.