FieldBuilder
Reference for the field builder `t` — field, the scalar helpers, and the expose helpers.
The field builder is the t argument passed to every fields function. Its methods return field refs. For a guided introduction to defining fields and writing resolvers, see Fields and Resolvers.
field(options)
options:FieldOptions
FieldOptions
type FieldOptions = {
type: ReturnType;
args?: Args;
nullable?: boolean;
description?: string;
deprecationReason?: string;
extensions?: Record<string, unknown>;
resolve: (parent, args, context, info) => ResolveValue;
};type: Type Parameterargs: a map of arg name to arg definition. Create arg definitions with theInputFieldBuilder(t.arg) or withbuilder.args.nullable: whether this field can returnnull. Defaults totrueunless you flip it in the builder — see Default nullability.description: text description of the field, surfaced in tools like GraphiQL.deprecationReason: marks the field deprecated with this reason.extensions: arbitrary extension metadata, read by directives and server plugins.resolve: Resolver
Type parameter
A field's type can be any TypeRef returned by a SchemaBuilder type-definition method, a class used to create an object or interface type, a TypeScript enum used to define a GraphQL enum, or a string matching a key of the Objects, Interfaces, or Scalars maps in SchemaTypes.
For list fields, wrap any of the above in an array — for example ['Character'].
Resolver
A function that resolves the value of the field. It should return a value (or promise) matching the field's type. For Scalars, Objects, and Interfaces that is the corresponding shape from SchemaTypes. For unions, it may be any member's shape. For enums, the value depends on the enum's definition; see Enums.
The resolver receives four arguments:
parent: the backing model of the current type, as declared inSchemaTypes.args: an object matching the field'sargsoption.context: theContexttype fromSchemaTypes.info: aGraphQLResolveInfoobject describing how the field was queried.
Scalar helpers
Shortcuts for defining scalar fields. Each works like field but omits the type option:
string(options)id(options)boolean(options)int(options)float(options)stringList(options)idList(options)booleanList(options)intList(options)floatList(options)listRef(type, options)
Expose helpers
Shortcuts for exposing a property of the backing model directly, without a resolver. The name argument can be any property of the backing model whose type matches the field. Options are the same as field, with type and resolve omitted:
exposeString(name, options)exposeID(name, options)exposeBoolean(name, options)exposeInt(name, options)exposeFloat(name, options)exposeStringList(name, options)exposeIDList(name, options)exposeBooleanList(name, options)exposeIntList(name, options)exposeFloatList(name, options)
The expose helpers exist only on object and interface field builders, where a backing model is in scope. They are not available on Query, Mutation, or Subscription, whose root value carries no properties to expose.