Bird
0
0

Find the bug in this resolver:

medium📝 Debug Q7 of 15
GraphQL - Resolvers
Find the bug in this resolver:
const resolver = (parent, args, context) => {
  const userId = context.userId;
  return context.db.getUser(userId);
}

Assuming context.userId is undefined.
AMissing <code>args</code> parameter
BUsing <code>context.userId</code> instead of <code>context.user.id</code>
CResolver should be async
DIncorrect return statement syntax
Step-by-Step Solution
Solution:
  1. Step 1: Identify the property used from context

    The code uses context.userId, but context likely stores user info under context.user.id.
  2. Step 2: Understand undefined userId causes wrong DB call

    Since context.userId is undefined, the DB call will fail or return wrong data.
  3. Final Answer:

    Using context.userId instead of context.user.id -> Option B
  4. Quick Check:

    Correct context property usage fixes bug [OK]
Quick Trick: Use correct context property names [OK]
Common Mistakes:
  • Confusing userId location in context
  • Assuming missing args causes error
  • Thinking async is required always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes