Bird
0
0

Consider this resolver snippet:

medium📝 query result Q5 of 15
GraphQL - Resolvers
Consider this resolver snippet:
const resolver = (parent, args, context) => {
  if (!context.user) return null;
  return context.db.getPostsByUser(context.user.id);
}

What happens if context.user is null?
AThe resolver throws an error
BThe resolver returns null immediately
CThe resolver returns all posts
DThe resolver returns undefined
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the if condition checking context.user

    If context.user is falsy (null), the resolver returns null immediately.
  2. Step 2: Confirm no further code runs when user is null

    The return statement stops execution, so no error or other return happens.
  3. Final Answer:

    The resolver returns null immediately -> Option B
  4. Quick Check:

    Null user check returns null [OK]
Quick Trick: Check context.user before accessing data [OK]
Common Mistakes:
  • Expecting an error instead of null
  • Assuming all posts are returned
  • Thinking undefined is returned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes