Besides the arguments, each resolver method or function may also read a context value. Context is the standard way to inject dependencies into GraphQL resolvers. Typically the context value is an object that holds the current request, information about the user who sent it, a database connection, and per-request caches, such as DataLoaders.

To define the context object's type, put the @gqlContext tag directly above its type definition.

Consuming context values in resolvers

Gratos detects every resolver parameter typed with the @gqlContext type, and passes the context object to the resolver in that position:

me.ts
/** @gqlContext */
type GQLCtx = {
  req: Request;
  userID: string;
  db: Database;
};

/** @gqlQueryField */
export function me(ctx: GQLCtx): User {
  return ctx.db.users.getById(ctx.userID);
}

/** @gqlType */
type User = {
  /** @gqlField */
  name: string;
};

type Database = {
  users: { getById(id: string): User };
};

Tip

Unlike graphql-js, Gratos does not require the context object to be a specific positional argument of the resolver. Put it anywhere in the parameter list, as long as its type is the one annotated with @gqlContext.

Derived context values

Some context values are expensive to create, or used by a few resolvers only. For these, define a derived context: a function that produces a context value. It may read other context values as its arguments, derived ones included.

Once a derived context is defined, the type it returns is a new context type. Any resolver parameter of this type receives the value the derived context function returns.

me.ts
type DB = {
  selectUser(): { name: string };
  /*...*/
};

/** @gqlContext */
type Ctx = { db: DB };

/** @gqlContext */
export function getDb(ctx: Ctx): DB {
  return ctx.db;
}

/**
 * A field which reads a derived context. Gratos will invoke the above `getDb`
 * function and pass it to this resolver function.
 *
 * @gqlQueryField */
export function me(db: DB): string {
  return db.selectUser().name;
}

A derived context function is called separately for each resolver that reads its value. To reuse the result across resolvers, memoize it:

me.ts
class DB {
  selectUser(): { name: string } {
    return { name: "Alice" };
  }
}

/** @gqlContext */
type Ctx = {};

// A derived context resolver cached using a WeakMap to ensure the value is
// computed at most once per request.

const DB_CACHE = new WeakMap<Ctx, DB>();

/** @gqlContext */
export function getDb(ctx: Ctx): DB {
  if (!DB_CACHE.has(ctx)) {
    DB_CACHE.set(ctx, new DB());
  }
  return DB_CACHE.get(ctx)!;
}

/**
 * @gqlQueryField */
export function me(db: DB): string {
  return db.selectUser().name;
}

A derived context function cannot return an Effect.Effect or a Stream.Stream: Gratos reports an error. An Effect resolver reads its dependencies as Effect services instead, which the server's Effect layer provides for each request (see Effect resolvers).

Constructing your root context object

How you construct your context object depends on your GraphQL server. With gratos-server, the server's context option builds it for each request (see The server core).

Caution

Gratos ensures that every resolver expects the same annotated root context type, but it cannot ensure that the root context value you construct matches that type. It is up to you to construct your root context value correctly and pass it to the GraphQL executor.