0
0
GraphQLquery~10 mins

N+1 problem and solutions in GraphQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the GraphQL query to fetch all users with their posts.

GraphQL
query { users { id name posts { [1] } } }
Drag options to blanks, or click blank then click option'
Aaddress
Bemail
Cage
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing user fields like email inside posts.
Leaving the posts field empty.
2fill in blank
medium

Complete the resolver to batch load posts for users to avoid N+1 problem.

GraphQL
const resolvers = { User: { posts: (user, args, context) => context.[1].load(user.id) } };
Drag options to blanks, or click blank then click option'
ApostLoader
BuserLoader
CcommentLoader
DprofileLoader
Attempts:
3 left
💡 Hint
Common Mistakes
Using loaders unrelated to posts.
Not using any loader causing N+1 queries.
3fill in blank
hard

Fix the error in the DataLoader batch function to correctly group posts by userId.

GraphQL
const batchPosts = async (userIds) => { const posts = await db.posts.find({ userId: { [1]: userIds } }); return userIds.map(id => posts.filter(post => post.userId === id)); };
Drag options to blanks, or click blank then click option'
Aeq
Bcontains
Cin
Dlike
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality operator which only matches one value.
Using string matching operators incorrectly.
4fill in blank
hard

Fill both blanks to complete the GraphQL query and resolver to use DataLoader for comments.

GraphQL
query { posts { id comments { [1] } } } const resolvers = { Post: { comments: (post, args, context) => context.[2].load(post.id) } };
Drag options to blanks, or click blank then click option'
Atext
BcommentLoader
Cauthor
DuserLoader
Attempts:
3 left
💡 Hint
Common Mistakes
Fetching wrong fields inside comments.
Using wrong loader names causing errors.
5fill in blank
hard

Fill all three blanks to implement a DataLoader batch function that fetches users by IDs and returns them in order.

GraphQL
const batchUsers = async ([1]) => { const users = await db.users.find({ id: { [2]: [1] } }); return [1].map(id => users.find(user => user.id === id)); };
Drag options to blanks, or click blank then click option'
AuserIds
Bin
Deq
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names.
Using equality operator instead of in.
Not returning users in the order of IDs.