What is the context object in GraphQL?
Answer
The context is an object shared across all resolvers in a single request. It is the third argument to every resolver: resolver(parent, args, context, info). Typical contents: authenticated user (from JWT/session), database connection or ORM instance, DataLoader instances (created per-request for batching), service clients, request headers, logger. The context is created once per request in the server configuration: context: ({ req }) => ({ user: getUser(req.headers.authorization), db, loaders: createLoaders() }). Because it's shared, resolvers can access user info and data sources without passing them as arguments. Important: DataLoaders must be created per-request (not shared globally) to prevent cache leakage between requests from different users.
Previous
What is schema-first vs code-first GraphQL development?
Next
What is the root value in GraphQL?