SchemaBuilder
Reference for SchemaBuilder — its generic, constructor options, and every type-building method.
SchemaBuilder is the core class of Pothos. You use it to define every type in your schema, then call toSchema to produce a graphql-js GraphQLSchema. For a guided walk through configuring the builder, see SchemaBuilder in the fundamentals.
constructor<SchemaTypes>(options)
- typeParam
SchemaTypes: a type describing the backing models, context, and defaults for your schema. options:SchemaBuilderOptions
SchemaTypes
type SchemaTypes = {
// Shape of the `context` arg in your resolvers.
Context?: object;
// Shape of the `parent`/root value passed to root fields.
Root?: object;
// A map of Object type names to their backing models.
Objects?: object;
// A map of Input type names to their backing models.
Inputs?: object;
// A map of Interface type names to their backing models.
Interfaces?: object;
// Map of scalar names to Input and Output shapes. Use it to overwrite the
// default scalar types, or to add type information for custom scalars.
Scalars?: {
[s: string]: {
Input: unknown;
Output: unknown;
};
};
// When `false`, fields are non-nullable by default (requires the matching
// `defaultFieldNullability` builder option).
DefaultFieldNullability?: boolean;
// When `true`, input fields and arguments are required by default (requires
// the matching `defaultInputFieldRequiredness` builder option).
DefaultInputFieldRequiredness?: boolean;
};SchemaBuilderOptions
type SchemaBuilderOptions = {};The core builder takes no options. Plugins contribute their own, such as plugins: [...] and each plugin's config block. See the individual plugin pages for what each adds.
queryType(options, fields?)
Creates the Query type with a set of fields.
options:QueryTypeOptionsfields?: a function that receives aFieldBuilderand returns a map of field names to field refs.
QueryTypeOptions
type QueryTypeOptions = {
description?: string;
fields?: FieldsFunction;
};description: a description of theQuerytype.fields: a function that receives aFieldBuilderand returns a map of field names to field refs.
queryFields(fields)
Adds a set of fields to the Query type.
fields: a function that receives aFieldBuilderand returns a map of field names to field refs.
queryField(name, field)
Adds a single field to the Query type.
name: the name of the field.field: a function that receives aFieldBuilderand returns a field ref.
mutationType(options, fields?)
Creates the Mutation type with a set of fields.
options:MutationTypeOptionsfields?: a function that receives aFieldBuilderand returns a map of field names to field refs.
MutationTypeOptions
type MutationTypeOptions = {
description?: string;
fields?: FieldsFunction;
};description: a description of theMutationtype.fields: a function that receives aFieldBuilderand returns a map of field names to field refs.
mutationFields(fields)
Adds a set of fields to the Mutation type.
fields: a function that receives aFieldBuilderand returns a map of field names to field refs.
mutationField(name, field)
Adds a single field to the Mutation type.
name: the name of the field.field: a function that receives aFieldBuilderand returns a field ref.
subscriptionType(options, fields?)
Creates the Subscription type with a set of fields.
options:SubscriptionTypeOptionsfields?: a function that receives aFieldBuilderand returns a map of field names to field refs.
SubscriptionTypeOptions
type SubscriptionTypeOptions = {
description?: string;
fields?: FieldsFunction;
};description: a description of theSubscriptiontype.fields: a function that receives aFieldBuilderand returns a map of field names to field refs.
subscriptionFields(fields)
Adds a set of fields to the Subscription type.
fields: a function that receives aFieldBuilderand returns a map of field names to field refs.
subscriptionField(name, field)
Adds a single field to the Subscription type.
name: the name of the field.field: a function that receives aFieldBuilderand returns a field ref.
objectType(param, options, fields?)
Defines an object type. param can be a class, an ObjectRef, or a SchemaTypes-registered name; see Object types for how the three forms differ.
param: a key of theObjectsproperty inSchemaTypes, a class, or anObjectRefcreated bybuilder.objectRef.options:ObjectTypeOptionsfields?: a function that receives aFieldBuilderand returns a map of field names to field refs.
ObjectTypeOptions
type ObjectTypeOptions = {
description?: string;
fields?: FieldsFunction;
interfaces?: Interfaces;
isTypeOf?: (obj, context, info) => boolean;
name?: string;
extensions?: Record<string, unknown>;
};description: a description of the type.fields: a function that receives aFieldBuilderand returns a map of field names to field refs.isTypeOf: recommended when implementing interfaces. Called during execution to decide whether a value of an implemented interface is of this type.interfaces: an array of interfaces this type implements. Each item is an interface param (see theparamargument ofinterfaceType).name: name of the GraphQL type. Required whenparamis a class.extensions: arbitrary extension metadata, read by directives and server plugins.
objectFields(param, fields)
Adds a set of fields to an object type.
param: a key of theObjectsproperty inSchemaTypes, a class, or anObjectRefcreated bybuilder.objectRef.fields: a function that receives aFieldBuilderand returns a map of field names to field refs.
objectField(param, name, field)
Adds a single field to an object type.
param: a key of theObjectsproperty inSchemaTypes, a class, or anObjectRefcreated bybuilder.objectRef.name: the name of the field.field: a function that receives aFieldBuilderand returns a field ref.
objectRef<T>(name)
Creates a reference to an object type before it is implemented. Use it to break circular references, to build modular schemas without registering every type on SchemaTypes, or when writing plugins.
name: name of the type this ref represents. Can be overwritten when the ref is implemented.T: the backing model, the shape your resolvers return and Pothos hands back asparent.
The returned ref carries an implement method, so you can define fields directly: builder.objectRef<IRace>('Race').implement({ fields: ... }).
interfaceType(param, options, fields?)
Defines an interface type.
param: a key of theInterfacesproperty inSchemaTypes, a class, or anInterfaceRefcreated bybuilder.interfaceRef.options:InterfaceTypeOptionsfields?: a function that receives aFieldBuilderand returns a map of field names to field refs.
InterfaceTypeOptions
type InterfaceTypeOptions = {
description?: string;
fields?: FieldsFunction;
interfaces?: Interfaces;
resolveType?: (parent, context, info) => string;
name?: string;
extensions?: Record<string, unknown>;
};description: a description of the type.fields: a function that receives aFieldBuilderand returns a map of field names to field refs.interfaces: an array of interfaces this interface extends. Each item is an interface param (see theparamargument ofinterfaceType).resolveType: returns the name of the concrete type for a given value. An alternative to settingisTypeOfon each implementing object type.name: name of the GraphQL type. Required whenparamis a class.extensions: arbitrary extension metadata, read by directives and server plugins.
interfaceFields(param, fields)
Adds a set of fields to an interface type.
param: a key of theInterfacesproperty inSchemaTypes, a class, or anInterfaceRefcreated bybuilder.interfaceRef.fields: a function that receives aFieldBuilderand returns a map of field names to field refs.
interfaceField(param, name, field)
Adds a single field to an interface type.
param: a key of theInterfacesproperty inSchemaTypes, a class, or anInterfaceRefcreated bybuilder.interfaceRef.name: the name of the field.field: a function that receives aFieldBuilderand returns a field ref.
interfaceRef<T>(name)
Creates a reference to an interface type before it is implemented. Use it to break circular references, to build modular schemas, or when writing plugins.
name: name of the type this ref represents. Can be overwritten when the ref is implemented.T: the backing model, the shape shared by every implementing type.
unionType(name, options)
Defines a union type.
name: the name of the union.options:UnionTypeOptions
UnionTypeOptions
type UnionTypeOptions = {
description?: string;
types: Member[] | (() => Member[]);
resolveType?: (parent, context, info) => MaybePromise<GraphQLObjectType | TypeName>;
extensions?: Record<string, unknown>;
};description: a description of the type.types: the object types included in the union: an array, or a thunk returning one so members can be referenced before they are defined. Each item is an object param (see theparamargument ofobjectType).resolveType: called when resolving the type of a union value.parentis a union of the backing models of the member types. Return the name of the matching member type. Optional if each member type setsisTypeOf, but supplying it here is the usual approach.extensions: arbitrary extension metadata, read by directives and server plugins.
enumType(param, options)
Defines an enum type.
param: a string name for the enum, or a TypeScript enum.options:EnumTypeOptions
EnumTypeOptions
type EnumTypeOptions = {
description?: string;
values?: Values;
name?: string;
extensions?: Record<string, unknown>;
};description: a description of the type.values: either an array of strings (you may needas constto get precise value names) or aGraphQLEnumValueConfigMap. Required whenparamis not a TypeScript enum.name: required whenparamis a TypeScript enum.extensions: arbitrary extension metadata, read by directives and server plugins.
scalarType(name, options)
Defines a custom scalar.
name: a key of theScalarsproperty inSchemaTypes.options:ScalarTypeOptions
ScalarTypeOptions
type ScalarTypeOptions = {
description?: string;
// Serializes an internal value to include in a response.
serialize?: GraphQLScalarSerializer<OutputShape>;
// Parses an externally provided value to use as an input.
parseValue?: GraphQLScalarValueParser<InputShape>;
// Parses an externally provided literal value to use as an input.
parseLiteral?: GraphQLScalarLiteralParser<InputShape>;
extensions?: Readonly<Record<string, unknown>>;
};On graphql-js 17 the newer coercion hooks (coerceOutputValue, coerceInputValue, coerceInputLiteral, and valueToLiteral) are also accepted and forwarded to the underlying scalar config. This is why serialize is optional: supply either serialize or coerceOutputValue. The hooks are ignored on graphql-js 16, so serialize/parseValue/parseLiteral remain the portable choice.
addScalarType(name, scalar, options?)
Registers an existing GraphQLScalarType (for example, one from graphql-scalars) under a SchemaTypes name.
name: a key of theScalarsproperty inSchemaTypes.scalar: aGraphQLScalarType.options?: the same options asscalarType, withserializeoptional since the passed scalar already supplies one. Anything you set here overrides the scalar's own config.
inputType(param, options)
Defines an input object type.
param: a string name, or anInputObjectRefcreated bybuilder.inputRef.options:InputTypeOptions
InputTypeOptions
type InputTypeOptions = {
description?: string;
fields: InputFieldsFunction;
isOneOf?: boolean;
extensions?: Record<string, unknown>;
};description: a description of the type.fields: a function that receives anInputFieldBuilderand returns a map of field names to field definitions. Whenparamis a key of theInputsproperty inSchemaTypes, the shape is type-checked against the registered backing model.isOneOf: marks the type as a@oneOfinput, where exactly one field may be provided. All fields must be nullable.extensions: arbitrary extension metadata, read by directives and server plugins.
inputRef<T>(name)
Creates a reference to an input object type before it is implemented. Use it for recursive input types, for modular schemas, or when writing plugins.
name: name of the type this ref represents. Can be overwritten when the ref is implemented.T: the backing shape of the input.
args(fields)
Creates a standalone arguments object you can reuse as the args option on a field definition.
fields: a function that receives anArgBuilderand returns a map of arg names to arg definitions.
toSchema(options?)
Builds and returns a GraphQLSchema from every type registered on the builder.
options?:BuildSchemaOptions, carrying build-time options such asdirectivesandextensions. Plugins add their own keys here.
Earlier docs described toSchema as taking an array of types. It does not — types register themselves on the builder as you define them, and toSchema takes only an optional options object.
SchemaBuilder.allowPluginReRegistration
A static boolean on the SchemaBuilder class. When true, a plugin may call registerPlugin more than once — useful for hot-module reloading. It defaults to false so duplicate copies of a plugin surface as an error rather than a silent conflict.