Gratos uses graphql-js to execute queries, so each resolver has access to the conventional four arguments. But your resolvers don't have to match graphql-js's signature: as long as Gratos can tell which value each of your arguments asks for, the generated schema maps graphql-js's arguments onto yours. You are free to omit or reorder them.

obj

The parent object, which for a field on the root Query type is often not used.

Note

When you define your resolvers as methods on a class, the initial obj argument is omitted in favor of this.

In the functional or static method style, a field on a type other than Query, Mutation or Subscription takes it as its first argument. Gratos reads the type annotation of that argument to know which type the field is defined on, so that type's definition must be annotated with /** @gqlType */. Gratos reports a helpful error if you forget.

args

The arguments passed to the field in the GraphQL query. A resolver reads them in one of two styles:

  • Positional arguments: each top-level parameter of the resolver typed with a GraphQL type is an argument. Gratos infers the argument's type and name from the parameter, and passes that argument's value in that position.
  • Object map: one parameter with an explicit inline literal type. Gratos reads that type to know the arguments and their GraphQL types.

See Arguments.

context

A value passed to every resolver that holds contextual information, generally:

  • information about the current request;
  • the user who is signed in;
  • per-request caches, such as DataLoaders.

Annotate your context type with /** @gqlContext */: any parameter of that type is passed the context object. See Context.

info

Field-specific information about the current query, and the schema (graphql-js's GraphQLResolveInfo). It is rarely used, but helps in advanced cases, such as a database query that joins only what the query selects.

Gratos passes the info object to any parameter typed with the GqlInfo type you import from gratos. See Info.

Class method example

User.ts
/** @gqlType */
class User {
  _bestFriendID: number;

  constructor(_bestFriendID: number) {
    this._bestFriendID = _bestFriendID;
  }
  /** @gqlField */
  bestFriend(order: string, context: GqlContext): User {
    return context.db.getSortedFriends(this._bestFriendID, order);
  }
}

/** @gqlContext */
type GqlContext = {
  db: { getSortedFriends(id: number, order: string): User };
};

Functional style example

Note

Gratos reads the type of the first argument to know which GraphQL type the field is added to.

User.ts
/** @gqlType */
type User = {
  _bestFriendID: number;
};

/** @gqlField */
export function friends(user: User, order: string, context: GqlContext): User {
  return context.db.getSortedFriends(user._bestFriendID, order);
}

/** @gqlContext */
type GqlContext = {
  db: { getSortedFriends(id: number, order: string): User };
};

Effect resolvers

Any of these resolvers may also return an Effect.Effect<A, E, R>, which Gratos reads as a field of type A, as it reads Promise<A>. Its arguments follow the same rules; the services it needs come from its Effect context, which the server's Effect layer provides per request, rather than from context. See Effect resolvers.