Build the mental model
Every resolver function can accept four arguments: `parent` (the resolved value of the parent field), `args` (the field's arguments), `context` (a shared object for the whole request — the db connection, the current user), and `info` (execution metadata, rarely used directly). `context` is built once per request inside Apollo Server's `context` function and passed to every resolver, giving you one place to manage database connection pooling and the current authenticated user.
Connect it to a real scenario
The Tutorial Platform's `Context` interface holds `db` (a typed database client) and `userId` (a string decoded from a token, or null). The `Query.tutorial` resolver calls `context.db.tutorial.findUnique(...)`, so every resolver reuses the same database-access pattern consistently.
Try the working example
interface Context {
db: Database;
userId: string | null;
}
const resolvers = {
Query: {
tutorial: (
_parent: unknown,
args: { id: string },
context: Context,
) => context.db.tutorial.findUnique({ where: { id: args.id } }),
},
};
const server = new ApolloServer<Context>({ typeDefs, resolvers });
await startStandaloneServer(server, {
context: async ({ req }) => ({
db,
userId: getUserIdFromToken(req.headers.authorization),
}),
});You can distinguish the four resolver arguments and write a typed context object.5-minute try-it
Write how the `Tutorial.author` resolver would use the `parent` argument to read the tutorial's authorId.
One important caution
Opening a fresh database connection inside a resolver function on every call can open many connections per request — always reuse the shared connection or pool from context.
Apollo Server — Resolvers — GraphQL