0
0
GraphQLquery~20 mins

N+1 problem and solutions in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
N+1 Problem Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding the N+1 Problem in GraphQL

What best describes the N+1 problem in GraphQL?

AIt is an issue where GraphQL schema definitions are missing required fields.
BIt is a problem where GraphQL queries return no data due to syntax errors.
CIt happens when GraphQL queries are too large and exceed server memory limits.
DIt occurs when a query fetches a list of items and then makes an additional query for each item to fetch related data, causing many database calls.
Attempts:
2 left
💡 Hint

Think about how many database calls happen when fetching related data for multiple items.

query_result
intermediate
1:30remaining
Result of a Query Causing N+1 Problem

Given a GraphQL query fetching 3 authors and their books, if each author triggers a separate database call for books, how many total database calls are made?

GraphQL
query {
  authors {
    id
    name
    books {
      title
    }
  }
}
A4 database calls
B1 database call
C3 database calls
D6 database calls
Attempts:
2 left
💡 Hint

Count one call for authors plus one call per author for books.

📝 Syntax
advanced
2:00remaining
Fixing N+1 Problem Using DataLoader

Which code snippet correctly uses DataLoader to batch load books for authors and avoid the N+1 problem?

GraphQL
const booksLoader = new DataLoader(async (authorIds) => {
  // Fetch books for all authorIds in one query
  const books = await db.books.where('authorId').in(authorIds).toArray();
  return authorIds.map(id => books.filter(book => book.authorId === id));
});
A
const booksLoader = new DataLoader(async (authorIds) => {
  const books = await db.books.where('authorId').in(authorIds).toArray();
  return books;
});
B
const booksLoader = new DataLoader(async (authorIds) => {
  const books = await db.books.where('authorId').in(authorIds).toArray();
  return authorIds.map(id => books.filter(book => book.authorId === id));
});
C
const booksLoader = new DataLoader(async (authorIds) => {
  const books = await db.books.where('authorId').anyOf(authorIds).toArray();
  return books.map(book => authorIds.includes(book.authorId));
});
D
const booksLoader = new DataLoader(async (authorIds) => {
  const books = await db.books.where('authorId').equals(authorIds).toArray();
  return authorIds.map(id => books.filter(book => book.authorId === id));
});
Attempts:
2 left
💡 Hint

Look for the method that fetches books for multiple author IDs at once and returns them grouped by author.

optimization
advanced
1:30remaining
Best Approach to Avoid N+1 Problem in GraphQL Resolvers

Which approach best avoids the N+1 problem when resolving nested fields in GraphQL?

AFetch all data in the root resolver and ignore nested resolvers.
BUse separate database queries inside each resolver for nested fields.
CUse DataLoader to batch and cache database requests for nested fields.
DUse multiple GraphQL queries from the client to fetch nested data separately.
Attempts:
2 left
💡 Hint

Think about batching and caching to reduce repeated database calls.

🔧 Debug
expert
2:00remaining
Identifying N+1 Problem in GraphQL Resolver Code

Given the following resolver code, which line causes the N+1 problem?

const resolvers = {
  Query: {
    authors: () => db.authors.findAll(),
  },
  Author: {
    books: (author) => db.books.findAll({ where: { authorId: author.id } }),
  },
};
ALine: books: (author) => db.books.findAll({ where: { authorId: author.id } })
BLine: const resolvers = { ... }
CLine: authors: () => db.authors.findAll()
DLine: Query: { ... }
Attempts:
2 left
💡 Hint

Look for the resolver that queries the database for each author individually.