To define fields on the root types Query, Mutation or Subscription, use these docblock tags:

  • @gqlQueryField
  • @gqlMutationField
  • @gqlSubscriptionField

Put them before a:

  • static method;
  • function declaration;
  • arrow function declaration.

Tip

To define fields on other types, see Fields.

Class-based root fields

To keep the root fields that return or change a type with that type's class, define them as static methods of the class:

User.ts
/** @gqlType */
export class User {
  /** @gqlField */
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  /** @gqlQueryField */
  static me(): User {
    return new User("Elizabeth");
  }
}

For more about field resolvers, see Resolver signature.

Functional style root fields

If you prefer to avoid classes, define root fields as named, exported functions. A function resolver:

  • has a return type that matches the field's type;
  • is an exported, named function, whose name is the field's name.

Extending Query

userById.ts
const DB: any = {};

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

/** @gqlQueryField */
export function userById(id: string): User {
  return DB.getUserById(id);
}

Extending Mutation

deleteUser.ts
const DB: any = {};

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

/** @gqlMutationField */
export function deleteUser(id: string): boolean {
  return DB.deleteUser(id);
}

Tip

A field on one of these root types adds the type to your schema. To define a root type explicitly, to add a description say, see Operation types.

A subscription field returns an AsyncIterable (see Subscriptions), or, in Gratos, a Stream.Stream (see Effect resolvers).

More field documentation