Docblock tags
Root fields
On this page
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:
/** @gqlType */
export class User {
/** @gqlField */
name: string;
constructor(name: string) {
this.name = name;
}
/** @gqlQueryField */
static me(): User {
return new User("Elizabeth");
}
}type Query {
me: User
}
type User {
name: String
}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
const DB: any = {};
/** @gqlType */
type User = {
/** @gqlField */
name: string;
};
/** @gqlQueryField */
export function userById(id: string): User {
return DB.getUserById(id);
}type Query {
userById(id: String!): User
}
type User {
name: String
}Extending Mutation
const DB: any = {};
/** @gqlType */
type User = {
/** @gqlField */
name: string;
};
/** @gqlMutationField */
export function deleteUser(id: string): boolean {
return DB.deleteUser(id);
}type Mutation {
deleteUser(id: String!): Boolean
}
type User {
name: String
}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
- Renaming, to expose a field under a name other than its function, property or method's
- Arguments, to define arguments
- Descriptions, to add descriptions to your fields
- Field nullability, to control the error handling and nullability of a field
- Deprecated, to deprecate a field