Bird
0
0

You want to ensure all GraphQL resolvers have access to both a database client and the current user's role via the context argument. Which setup is most appropriate?

hard📝 Application Q8 of 15
GraphQL - Resolvers
You want to ensure all GraphQL resolvers have access to both a database client and the current user's role via the context argument. Which setup is most appropriate?
ACreate a context object per request containing the DB client and user role, then pass it to resolvers
BUse global variables for DB client and user role accessible inside resolvers
CPass the DB client as an argument and user role as a global variable
DInclude DB client and user role inside the <code>args</code> argument of each resolver
Step-by-Step Solution
Solution:
  1. Step 1: Understand context usage

    The context argument is designed to share per-request data like DB clients and user info.
  2. Step 2: Avoid global variables

    Global variables are unsafe for concurrent requests and can cause data leaks.
  3. Step 3: Avoid passing via args

    Passing DB client or user role via args is not standard and mixes concerns.
  4. Final Answer:

    Create a context object per request containing the DB client and user role, then pass it to resolvers -> Option A
  5. Quick Check:

    Context per request is best practice for shared data [OK]
Quick Trick: Use per-request context for shared data, not globals [OK]
Common Mistakes:
  • Using global variables for user info or DB client
  • Passing shared data through resolver args
  • Not creating context per request

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes