Context Overview
The Context object is the primary API entry point for all of the run-time functionality of your Keystone system. In GraphQL, a context is a value which is provided to every resolver and holds important contextual information such as the currently logged in user, or access to a database. Keystone makes a Context object available as the context of all resolvers in the system. The APIs provided by the Context object can be used to write the business logic for things like access control, hooks, testing and GraphQL schema extensions.
The Context object has the following properties, which are documented below.
import type { Context } from './generated/keystone/types'const context = {// Database APIdb,// HTTP headersreq,res,// Session APIsession,// GraphQL helpersgraphql: {schema,run,raw,},// New context creatorssudo,internal,withHeaders,withSession,// Transactionstransaction,// Raw Prisma accessprisma,}
HTTP headers
req: A WHATWG Headers object containing the incoming request headers.
res: A WHATWG Headers object for outgoing response headers. It can be undefined for read-only contexts.
Database API
db: The Database API, which can be used to perform CRUD operations against your GraphQL API and return database objects suitable for return from mutations in schema extensions.
GraphQL helpers
graphql.schema: The GraphQL Schema object, which can be used directly, or via graphql.raw and graphql.run.
graphql.run executes a GraphQL operation and returns data. If any errors occur, the first error is thrown.
graphql.raw returns the GraphQL ExecutionResult, including data and errors.
Both helpers take { query, variables } and execute the operation with the current context.
Typed GraphQL documents
You can use tools like gql.tada to write typed operations against Keystone's generated schema.graphql. TypeScript infers the result type and checks the variables you supply.
import { graphql } from './tada'const AuthorById = graphql(`query AuthorById($id: ID!) {author(where: { id: $id }) {idname}}`)const { author } = await context.graphql.run({query: AuthorById,variables: { id: 'author-id' },})
The Keystone gql.tada example shows how to configure graphql(). The helpers also accept TypedDocumentNode values from other libraries. See the GraphQL Code Generator example.
GraphQL strings and plain DocumentNode objects also work though they will not have typed results or variables.
Session API
If you configure your Keystone system with session management then you will have access to the following properties. See the getSession API for more details.
session: The current session data object.
New context creators
Keystone applies access control using the current context and its session. The following functions create a new Context with different settings.
sudo(): Returns a new Context that bypasses Keystone access control.
internal(): A function which returns a new Context object that bypasses graphql.omit on lists and fields, allowing you to query and mutate data that is otherwise omitted from the GraphQL API.
withHeaders(req, res?): Creates a context from WHATWG Headers and loads its session using getSession. Request headers are cloned; response headers are shared with the returned context and its derived contexts. Omitting res creates a read-only context for response headers, so session cookies cannot be set.
Keystone's built-in server copies outgoing headers to the HTTP response automatically. In custom integrations, supply a mutable Headers object as res and copy its contents to response headers before sending the response. Preserve separate Set-Cookie values with res.getSetCookie() rather than combining them into one header.
withSession(newSession): A function which returns a new Context object with the .session object replaced by the newSession argument.
Database access
The Context object exposes the underlying database driver directly via context.prisma, which is a Prisma Client object.
Access through context.prisma bypasses the GraphQL schema and Keystone access control.
Transactions
transaction(fn, options): Runs fn inside a Prisma transaction and passes it a new Context object backed by the transaction client. The optional options argument is forwarded to Prisma's $transaction call.