Complete the code to define a GraphQL context parameter in the resolver function.
const resolver = (parent, args, [1]) => { return args.id; }
The third parameter in a GraphQL resolver is usually called context. It holds shared data like authentication info.
Complete the code to access the user ID from the context inside a resolver.
const userId = [1].user.id;The context object contains shared info like the authenticated user. Access user ID via context.user.id.
Fix the error in the resolver to correctly destructure context and get the database client.
const resolver = (parent, args, [1]) => { const { db } = context; return db.getUser(args.id); }
The parameter name must match the variable used inside the function. Here, context is used, so it must be the parameter name.
Fill both blanks to correctly pass context to the Apollo Server constructor.
const server = new ApolloServer({ typeDefs, resolvers, [1]: () => ([2]) });The context option is a function returning an object with shared data like the current user.
Fill all three blanks to create a resolver that uses context to check user role and fetch data.
const resolver = (parent, args, [1]) => { if (![2].user || [3].user.role !== 'admin') { throw new Error('Unauthorized'); } return [2].db.getData(args.id); }
The resolver uses the context parameter (also called ctx sometimes) to check user info and access the database. Here, the same context object is used multiple times.