Bird
0
0

How would you modify this resolver to access a database connection from context and use an argument to fetch a user by ID?

hard📝 Application Q9 of 15
GraphQL - Resolvers
How would you modify this resolver to access a database connection from context and use an argument to fetch a user by ID?
function resolver(parent, args, context) {
  return context.db.getUser(args.id);
}
Aargs.id should be replaced with parent.id
BYou must pass parent before args and context
CThis is correct; context.db provides the database and args.id is used properly
DYou should not use context for database access
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the use of context and args

    The resolver uses context.db to access the database and args.id to specify the user ID, which is correct.
  2. Step 2: Validate other options

    Argument order is correct, context is the right place for shared resources, and parent.id is unrelated here.
  3. Final Answer:

    This is correct; context.db provides the database and args.id is used properly -> Option C
  4. Quick Check:

    Use context for shared resources like DB [OK]
Quick Trick: Use context for shared resources, args for input data [OK]
Common Mistakes:
  • Misplacing argument order
  • Avoiding context for DB access
  • Using parent instead of args for input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes