Skip to main content


After the arguments object, each resolver method/function is passed a context value. Context is the standard way to implement depednecy injection for GraphQL resolvers. Typically the context value will be an object includiding the current request, information about the requesting user, as well as a database connection and per-request caches, such as DataLoaders.

Becuase GraphQL invokes resolver functions dynamically, there are generally no static calls to your resolver methods/functions that are visible to TypeScript. This means that there is no typecheck that can confirm that the context arugment is typed correctly.

Grats helps to mitigate this risk by alidating that every resolver that specifies a context argument references the same defintion for the context value's type. This at least ensures all your resolvers match.

type GQLCtx = {
req: Request;
userID: string;
db: Database;

/** @gqlField */
export function me(_: Query, args: unknown, ctx: GQLCtx): User {
return ctx.db.users.getById(ctx.userID);

The context argument is passed as the third argument of resovler functions and the second argument of resolver methods. If your resolver does not need access to the context object, you can omit the context argument.


If you need to access the context object in your resolver, but your field does not define any args, you can type your args parameter as unknown.


Due to limitations in the TypeScript compiler, Grats is not able to structurally typecheck the context value. Instead is simply checks that every resolver that specifies a context argument references the same type definition.

Constructing your context object

The mechanism by which you construct your context object will vary depending upon the GraphQL server library you are using. See your GraphQL server library's documentation for more information.


Grats can ensure that every resolver is expecting the same context type, but it cannot ensure that the context value you construct and pass in matches that type. It is up to you to ensure that your context value is constructed correctly and passed to the GraphQL execution engine.