Create and inspect GraphQL type definitions and schemas.
These exports are also available from the root graphql package.
For documentation purposes, these exports are grouped into the following categories:
Category: Paths
Types:
ResponsePath
Types
ResponsePath
Interface. Represents a linked response path from a field back to the root response.
| Name | Type | Description |
|---|---|---|
| prev | Path | undefined | The previous segment in the linked response path, or undefined at the root. |
| key | string | number | The field name or list index for this response path segment. |
| typename | string | undefined | The runtime object type name associated with this path segment, if known. |
Category: Names
Functions:
assertName()assertEnumValueName()
Functions
assertName()
Upholds the spec rules about naming.
Signature:
assertName(name: string): string;
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name to validate. |
| Type | Description |
|---|---|
string | The validated GraphQL name. |
import { assertName } from 'graphql/type';
assertName('User'); // => 'User'
assertName('123User'); // throws an errorassertEnumValueName()
Upholds the spec rules about naming enum values.
Signature:
assertEnumValueName(name: string): string;
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name to validate. |
| Type | Description |
|---|---|
string | The validated GraphQL name. |
import { assertEnumValueName } from 'graphql/type';
assertEnumValueName('ACTIVE'); // => 'ACTIVE'
assertEnumValueName('true'); // throws an errorCategory: Types
Classes:
GraphQLListGraphQLNonNullGraphQLScalarTypeGraphQLObjectTypeGraphQLInterfaceTypeGraphQLUnionTypeGraphQLEnumTypeGraphQLInputObjectType
Functions:
isType()assertType()isScalarType()assertScalarType()isObjectType()assertObjectType()isField()assertField()isArgument()assertArgument()isInterfaceType()assertInterfaceType()isUnionType()assertUnionType()isEnumType()assertEnumType()isEnumValue()assertEnumValue()isInputObjectType()assertInputObjectType()isInputField()assertInputField()assertListType()assertNonNullType()isInputType()assertInputType()isOutputType()assertOutputType()isLeafType()assertLeafType()isCompositeType()assertCompositeType()isAbstractType()assertAbstractType()isWrappingType()assertWrappingType()isNullableType()assertNullableType()isNamedType()assertNamedType()resolveReadonlyArrayThunk()resolveObjMapThunk()isRequiredArgument()isRequiredInputField()
Types:
GraphQLTypeGraphQLNullableInputTypeGraphQLInputTypeGraphQLNullableOutputTypeGraphQLOutputTypeGraphQLLeafTypeGraphQLCompositeTypeGraphQLAbstractTypeGraphQLWrappingTypeGraphQLNullableTypeGraphQLNamedTypeGraphQLNamedInputTypeGraphQLNamedOutputTypeThunkReadonlyArrayThunkObjMapGraphQLScalarTypeExtensionsGraphQLScalarSerializerGraphQLScalarOutputValueCoercerGraphQLScalarValueParserGraphQLScalarInputValueCoercerGraphQLScalarLiteralParserGraphQLScalarInputLiteralCoercerGraphQLScalarTypeConfigGraphQLObjectTypeExtensionsGraphQLObjectTypeConfigGraphQLTypeResolverGraphQLIsTypeOfFnGraphQLFieldResolverGraphQLResolveInfoHelpersGraphQLResolveInfoGraphQLFieldExtensionsGraphQLFieldConfigGraphQLFieldConfigArgumentMapGraphQLArgumentExtensionsGraphQLArgumentConfigGraphQLFieldConfigMapGraphQLFieldGraphQLArgumentGraphQLFieldMapGraphQLDefaultInputGraphQLInterfaceTypeExtensionsGraphQLInterfaceTypeConfigGraphQLUnionTypeExtensionsGraphQLUnionTypeConfigGraphQLEnumTypeExtensionsGraphQLEnumTypeConfigGraphQLEnumValueConfigMapGraphQLEnumValueExtensionsGraphQLEnumValueConfigGraphQLEnumValueGraphQLInputObjectTypeExtensionsGraphQLInputObjectTypeConfigGraphQLInputFieldExtensionsGraphQLInputFieldConfigGraphQLInputFieldConfigMapGraphQLInputFieldGraphQLInputFieldMap
Classes
GraphQLList
List Type Wrapper
A list is a wrapping type which points to another type. Lists are often created within the context of defining the fields of an object type.
| Name | Constraint | Default | Description |
|---|---|---|---|
| T | GraphQLType | The GraphQL type wrapped by this list type. |
const PersonType = new GraphQLObjectType({
name: 'Person',
fields: () => ({
parents: { type: new GraphQLList(PersonType) },
children: { type: new GraphQLList(PersonType) },
})
})Constructor
Creates a GraphQLList instance.
Signature:
new GraphQLList(ofType: T);
| Name | Type | Description |
|---|---|---|
| ofType | T | The type to wrap. |
Members
| Name | Type | Description |
|---|---|---|
| ofType | T | The type wrapped by this list or non-null type. |
toString()
Returns this wrapping type as a GraphQL type-reference string.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The GraphQL type-reference string. |
import { GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql/type';
const stringList = new GraphQLList(GraphQLString);
const requiredStringList = new GraphQLList(new GraphQLNonNull(GraphQLString));
stringList.toString(); // => '[String]'
requiredStringList.toString(); // => '[String!]'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The JSON-serializable representation. |
import { GraphQLList, GraphQLString } from 'graphql/type';
const stringList = new GraphQLList(GraphQLString);
stringList.toJSON(); // => '[String]'
JSON.stringify({ type: stringList }); // => '{"type":"[String]"}'GraphQLNonNull
Non-Null Type Wrapper
A non-null is a wrapping type which points to another type. Non-null types enforce that their values are never null and can ensure an error is raised if this ever occurs during a request. It is useful for fields which you can make a strong guarantee on non-nullability, for example usually the id field of a database row will never be null.
| Name | Constraint | Default | Description |
|---|---|---|---|
| T | GraphQLNullableType | The nullable GraphQL type wrapped by this non-null type. |
const RowType = new GraphQLObjectType({
name: 'Row',
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLString) },
})
})Note: the enforcement of non-nullability occurs within the executor.
Constructor
Creates a GraphQLNonNull instance.
Signature:
new GraphQLNonNull(ofType: T);
| Name | Type | Description |
|---|---|---|
| ofType | T | The type to wrap. |
Members
| Name | Type | Description |
|---|---|---|
| ofType | T | The type wrapped by this list or non-null type. |
toString()
Returns this wrapping type as a GraphQL type-reference string.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The GraphQL type-reference string. |
import { GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql/type';
const requiredString = new GraphQLNonNull(GraphQLString);
const requiredStringList = new GraphQLNonNull(
new GraphQLList(GraphQLString),
);
requiredString.toString(); // => 'String!'
requiredStringList.toString(); // => '[String]!'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The JSON-serializable representation. |
import { GraphQLNonNull, GraphQLString } from 'graphql/type';
const requiredString = new GraphQLNonNull(GraphQLString);
requiredString.toJSON(); // => 'String!'
JSON.stringify({ type: requiredString }); // => '{"type":"String!"}'GraphQLScalarType
Scalar Type Definition
Scalar types define the leaf values of a GraphQL response and the input values accepted by arguments and input object fields. A scalar type has a name and coercion functions that validate and convert runtime values and GraphQL literals.
If a type’s coerceOutputValue function returns null or does not return a
value (i.e. it returns undefined) then an error will be raised and a
null value will be returned in the response. Prefer validating inputs
before execution so clients receive input diagnostics before result coercion
fails.
Custom scalar behavior is defined via the following functions:
-
coerceOutputValue(value): Implements “Result Coercion”. Given an internal value, produces an external value valid for this type. Returns undefined or throws an error to indicate invalid values.
-
coerceInputValue(value): Implements “Input Coercion” for values. Given an external value (for example, variable values), produces an internal value valid for this type. Returns undefined or throws an error to indicate invalid values.
-
coerceInputLiteral(ast): Implements “Input Coercion” for constant literals. Given a GraphQL literal (AST) (for example, an argument value), produces an internal value valid for this type. Returns undefined or throws an error to indicate invalid values.
-
valueToLiteral(value): Converts an external value to a GraphQL literal (AST). Returns undefined or throws an error to indicate invalid values.
Deprecated, to be removed in v18:
-
serialize(value): Implements “Result Coercion”. Renamed to
coerceOutputValue(). -
parseValue(value): Implements “Input Coercion” for values. Renamed to
coerceInputValue(). -
parseLiteral(ast): Implements “Input Coercion” for literals including non-specified replacement of variables embedded within complex scalars. Replaced by the combination of the
replaceVariables()utility and thecoerceInputLiteral()method.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | unknown | Internal runtime representation for this scalar. | |
| TExternal | TInternal | External representation accepted from or returned to callers. |
import { GraphQLScalarType, Kind } from 'graphql';
const ensureOdd = (value) => {
if (!Number.isFinite(value)) {
throw new Error(
`Scalar "Odd" cannot represent "${value}" since it is not a finite number.`,
);
}
if (value % 2 === 0) {
throw new Error(`Scalar "Odd" cannot represent "${value}" since it is even.`);
}
return value;
};
const OddType = new GraphQLScalarType({
name: 'Odd',
coerceOutputValue: (value) => {
return ensureOdd(value);
},
coerceInputValue: (value) => {
return ensureOdd(value);
},
valueToLiteral: (value) => {
return { kind: Kind.INT, value: String(ensureOdd(value)) };
}
});Constructor
Creates a GraphQLScalarType instance.
Signature:
new GraphQLScalarType(
config: Readonly<
GraphQLScalarTypeConfig<TInternal, TExternal>
>,
);
| Name | Type | Description |
|---|---|---|
| config | Readonly<
GraphQLScalarTypeConfig<TInternal, TExternal>
> | Configuration describing this object. |
Members
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| specifiedByURL | Maybe<string> | URL identifying the behavior specified for this custom scalar. |
| serialize | GraphQLScalarSerializer<TExternal> | Deprecated legacy serializer used to convert internal values for response output. Use coerceOutputValue() instead. |
| parseValue | GraphQLScalarValueParser<TInternal> | Deprecated legacy parser used to convert externally provided input values. Use coerceInputValue() instead. |
| parseLiteral | GraphQLScalarLiteralParser<TInternal> | Deprecated legacy parser used to convert externally provided input literals. Use replaceVariables() and coerceInputLiteral() instead. |
| coerceOutputValue | GraphQLScalarOutputValueCoercer<TExternal> | Coercer used to convert internal scalar values for response output. |
| coerceInputValue | GraphQLScalarInputValueCoercer<TInternal> | Coercer used to convert externally provided scalar input values. |
| coerceInputLiteral | GraphQLScalarInputLiteralCoercer<TInternal> | undefined | Coercer used to convert GraphQL scalar input literals. |
| valueToLiteral | GraphQLScalarValueToLiteral | undefined | Converter used to produce GraphQL literals from runtime input values. |
| extensions | Readonly<GraphQLScalarTypeExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<ScalarTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes | readonly ScalarTypeExtensionNode[] | AST extension nodes applied to this schema element. |
toConfig()
Returns a normalized configuration object for this object.
Signature:
toConfig(): GraphQLScalarTypeNormalizedConfig<
TInternal,
TExternal
>;
| Type | Description |
|---|---|
GraphQLScalarTypeNormalizedConfig<TInternal, TExternal> | A configuration object that can be used to recreate this object. |
import { GraphQLScalarType } from 'graphql/type';
const Url = new GraphQLScalarType({
name: 'Url',
description: 'An absolute URL string.',
specifiedByURL: 'https://url.spec.whatwg.org/',
});
const config = Url.toConfig();
const UrlCopy = new GraphQLScalarType(config);
config.name; // => 'Url'
config.specifiedByURL; // => 'https://url.spec.whatwg.org/'
UrlCopy.name; // => Url.nametoString()
Returns the schema coordinate identifying this scalar type.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The schema coordinate for this scalar type. |
import { GraphQLScalarType } from 'graphql/type';
const DateTime = new GraphQLScalarType({ name: 'DateTime' });
DateTime.toString(); // => 'DateTime'
String(DateTime); // => 'DateTime'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The JSON-serializable representation. |
import { GraphQLScalarType } from 'graphql/type';
const DateTime = new GraphQLScalarType({ name: 'DateTime' });
DateTime.toJSON(); // => 'DateTime'
JSON.stringify({ type: DateTime }); // => '{"type":"DateTime"}'GraphQLObjectType
Object Type Definition
Almost all of the GraphQL types you define will be object types. Object types have a name, but most importantly describe their fields.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | any | Source object type passed to resolvers. | |
| TContext | any | Context object type passed to resolvers. | |
| TAbstract | any | Runtime value type used for abstract type resolution. |
const AddressType = new GraphQLObjectType({
name: 'Address',
fields: {
street: { type: GraphQLString },
number: { type: GraphQLInt },
formatted: {
type: GraphQLString,
resolve: (obj) => {
return obj.number + ' ' + obj.street
}
}
}
});When two types need to refer to each other, or a type needs to refer to itself in a field, you can use a function expression (aka a closure or a thunk) to supply the fields lazily.
const PersonType = new GraphQLObjectType({
name: 'Person',
fields: () => ({
name: { type: GraphQLString },
bestFriend: { type: PersonType },
})
});Constructor
Creates a GraphQLObjectType instance.
Signature:
new GraphQLObjectType(
config: Readonly<
GraphQLObjectTypeConfig<
TSource,
TContext,
TAbstract
>
>,
);
| Name | Type | Description |
|---|---|---|
| config | Readonly<
GraphQLObjectTypeConfig<
TSource,
TContext,
TAbstract
>
> | Configuration describing this object. |
Members
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| isTypeOf | Maybe<
GraphQLIsTypeOfFn<TAbstract, TContext>
> | Predicate used to determine whether a runtime value belongs to this object type. |
| extensions | Readonly<
GraphQLObjectTypeExtensions<TSource, TContext>
> | Extension fields to include in the formatted result. |
| astNode | Maybe<ObjectTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes | readonly ObjectTypeExtensionNode[] | AST extension nodes applied to this schema element. |
getFields()
Returns the fields defined by this type.
Signature:
getFields(): GraphQLFieldMap<
TSource,
TContext
>;
| Type | Description |
|---|---|
GraphQLFieldMap<TSource, TContext> | The fields keyed by field name. |
import { buildSchema } from 'graphql/utilities';
import { assertObjectType } from 'graphql/type';
const schema = buildSchema(`
type User {
id: ID!
name: String
}
type Query {
viewer: User
}
`);
const User = assertObjectType(schema.getType('User'));
const fields = User.getFields();
Object.keys(fields); // => ['id', 'name']
String(fields.id.type); // => 'ID!'getInterfaces()
Returns the interfaces implemented by this type.
Signature:
getInterfaces(): readonly GraphQLInterfaceType[];
| Type | Description |
|---|---|
readonly GraphQLInterfaceType[] | The implemented interfaces. |
import { buildSchema } from 'graphql/utilities';
import { assertObjectType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
type Query {
viewer: User
}
`);
const User = assertObjectType(schema.getType('User'));
User.getInterfaces().map((type) => type.name); // => ['Node']toConfig()
Returns a normalized configuration object for this object.
Signature:
toConfig(): GraphQLObjectTypeNormalizedConfig<
TSource,
TContext,
TAbstract
>;
| Type | Description |
|---|---|
GraphQLObjectTypeNormalizedConfig<
TSource,
TContext,
TAbstract
> | A configuration object that can be used to recreate this object. |
import { GraphQLObjectType, GraphQLString } from 'graphql/type';
const User = new GraphQLObjectType({
name: 'User',
fields: {
name: { type: GraphQLString },
},
});
const config = User.toConfig();
const UserCopy = new GraphQLObjectType(config);
config.fields.name.type; // => GraphQLString
UserCopy.getFields().name.type; // => GraphQLStringtoString()
Returns the schema coordinate identifying this object type.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The schema coordinate for this object type. |
import { buildSchema } from 'graphql/utilities';
import { assertObjectType } from 'graphql/type';
const schema = buildSchema(`
type User {
name: String
}
type Query {
viewer: User
}
`);
const User = assertObjectType(schema.getType('User'));
User.toString(); // => 'User'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The JSON-serializable representation. |
import { GraphQLObjectType, GraphQLString } from 'graphql/type';
const User = new GraphQLObjectType({
name: 'User',
fields: { name: { type: GraphQLString } },
});
User.toJSON(); // => 'User'
JSON.stringify({ type: User }); // => '{"type":"User"}'GraphQLInterfaceType
Interface Type Definition
When a field can return one of a heterogeneous set of types, a Interface type is used to describe what types are possible, what fields are in common across all types, as well as a function to determine which type is actually used when the field is resolved.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | any | Source object type passed to resolvers. | |
| TContext | any | Context object type passed to resolvers. |
const EntityType = new GraphQLInterfaceType({
name: 'Entity',
fields: {
name: { type: GraphQLString }
}
});Constructor
Creates a GraphQLInterfaceType instance.
Signature:
new GraphQLInterfaceType(
config: Readonly<
GraphQLInterfaceTypeConfig<TSource, TContext>
>,
);
| Name | Type | Description |
|---|---|---|
| config | Readonly<
GraphQLInterfaceTypeConfig<TSource, TContext>
> | Configuration describing this object. |
Members
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| resolveType | Maybe<
GraphQLTypeResolver<TSource, TContext>
> | Function that resolves the concrete object type for this abstract type. |
| extensions | Readonly<GraphQLInterfaceTypeExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<InterfaceTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes | readonly InterfaceTypeExtensionNode[] | AST extension nodes applied to this schema element. |
getFields()
Returns the fields defined by this type.
Signature:
getFields(): GraphQLFieldMap<
TSource,
TContext
>;
| Type | Description |
|---|---|
GraphQLFieldMap<TSource, TContext> | The fields keyed by field name. |
import { buildSchema } from 'graphql/utilities';
import { assertInterfaceType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
type Query {
node: Node
}
`);
const Node = assertInterfaceType(schema.getType('Node'));
const fields = Node.getFields();
Object.keys(fields); // => ['id']
String(fields.id.type); // => 'ID!'getInterfaces()
Returns the interfaces implemented by this type.
Signature:
getInterfaces(): readonly GraphQLInterfaceType[];
| Type | Description |
|---|---|
readonly GraphQLInterfaceType[] | The implemented interfaces. |
import { buildSchema } from 'graphql/utilities';
import { assertInterfaceType } from 'graphql/type';
const schema = buildSchema(`
interface Resource {
url: String!
}
interface Image implements Resource {
url: String!
width: Int
}
type Photo implements Resource & Image {
url: String!
width: Int
}
type Query {
image: Image
}
`);
const Image = assertInterfaceType(schema.getType('Image'));
Image.getInterfaces().map((type) => type.name); // => ['Resource']toConfig()
Returns a normalized configuration object for this object.
Signature:
toConfig(): GraphQLInterfaceTypeNormalizedConfig<
TSource,
TContext
>;
| Type | Description |
|---|---|
GraphQLInterfaceTypeNormalizedConfig<TSource, TContext> | A configuration object that can be used to recreate this object. |
import { GraphQLID, GraphQLInterfaceType, GraphQLNonNull } from 'graphql/type';
const Node = new GraphQLInterfaceType({
name: 'Node',
fields: {
id: { type: new GraphQLNonNull(GraphQLID) },
},
});
const config = Node.toConfig();
const NodeCopy = new GraphQLInterfaceType(config);
String(config.fields.id.type); // => 'ID!'
String(NodeCopy.getFields().id.type); // => 'ID!'toString()
Returns the schema coordinate identifying this interface type.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The schema coordinate for this interface type. |
import { buildSchema } from 'graphql/utilities';
import { assertInterfaceType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
type Query {
node: Node
}
`);
const Node = assertInterfaceType(schema.getType('Node'));
Node.toString(); // => 'Node'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The JSON-serializable representation. |
import { GraphQLInterfaceType, GraphQLString } from 'graphql/type';
const Named = new GraphQLInterfaceType({
name: 'Named',
fields: { name: { type: GraphQLString } },
});
Named.toJSON(); // => 'Named'
JSON.stringify({ type: Named }); // => '{"type":"Named"}'GraphQLUnionType
Union Type Definition
When a field can return one of a heterogeneous set of types, a Union type is used to describe what types are possible as well as providing a function to determine which type is actually used when the field is resolved.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | any | Source object type passed to resolvers. | |
| TContext | any | Context object type passed to resolvers. |
const PetType = new GraphQLUnionType({
name: 'Pet',
types: [DogType, CatType],
resolveType: (value) => {
if (value instanceof Dog) {
return DogType;
}
if (value instanceof Cat) {
return CatType;
}
}
});Constructor
Creates a GraphQLUnionType instance.
Signature:
new GraphQLUnionType(
config: Readonly<
GraphQLUnionTypeConfig<TSource, TContext>
>,
);
| Name | Type | Description |
|---|---|---|
| config | Readonly<
GraphQLUnionTypeConfig<TSource, TContext>
> | Configuration describing this object. |
Members
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| resolveType | Maybe<
GraphQLTypeResolver<TSource, TContext>
> | Function that resolves the concrete object type for this abstract type. |
| extensions | Readonly<GraphQLUnionTypeExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<UnionTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes | readonly UnionTypeExtensionNode[] | AST extension nodes applied to this schema element. |
getTypes()
Returns the object types included in this union.
Signature:
getTypes(): readonly GraphQLObjectType[];
| Type | Description |
|---|---|
readonly GraphQLObjectType[] | The union member object types. |
import { buildSchema } from 'graphql/utilities';
import { assertUnionType } from 'graphql/type';
const schema = buildSchema(`
type Photo {
url: String!
}
type Video {
url: String!
}
union Media = Photo | Video
type Query {
media: [Media]
}
`);
const Media = assertUnionType(schema.getType('Media'));
Media.getTypes().map((type) => type.name); // => ['Photo', 'Video']toConfig()
Returns a normalized configuration object for this object.
Signature:
toConfig(): GraphQLUnionTypeNormalizedConfig<
TSource,
TContext
>;
| Type | Description |
|---|---|
GraphQLUnionTypeNormalizedConfig<TSource, TContext> | A configuration object that can be used to recreate this object. |
import { GraphQLObjectType, GraphQLString, GraphQLUnionType } from 'graphql/type';
const Photo = new GraphQLObjectType({
name: 'Photo',
fields: { url: { type: GraphQLString } },
});
const Video = new GraphQLObjectType({
name: 'Video',
fields: { url: { type: GraphQLString } },
});
const Media = new GraphQLUnionType({
name: 'Media',
types: [Photo, Video],
});
const config = Media.toConfig();
const MediaCopy = new GraphQLUnionType(config);
MediaCopy.getTypes().map((type) => type.name); // => ['Photo', 'Video']toString()
Returns the schema coordinate identifying this union type.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The schema coordinate for this union type. |
import { buildSchema } from 'graphql/utilities';
import { assertUnionType } from 'graphql/type';
const schema = buildSchema(`
type Photo {
url: String!
}
union SearchResult = Photo
type Query {
search: [SearchResult]
}
`);
const SearchResult = assertUnionType(schema.getType('SearchResult'));
SearchResult.toString(); // => 'SearchResult'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The JSON-serializable representation. |
import { GraphQLObjectType, GraphQLString, GraphQLUnionType } from 'graphql/type';
const Photo = new GraphQLObjectType({
name: 'Photo',
fields: { url: { type: GraphQLString } },
});
const SearchResult = new GraphQLUnionType({
name: 'SearchResult',
types: [Photo],
});
SearchResult.toJSON(); // => 'SearchResult'
JSON.stringify({ type: SearchResult }); // => '{"type":"SearchResult"}'GraphQLEnumType
Enum Type Definition
Enum types define leaf values whose serialized form is one of a fixed set of GraphQL enum names. Internally, enum values can map to any runtime value, often integers.
import { GraphQLEnumType } from 'graphql/type';
const RGBType = new GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 },
},
});
RGBType.getValue('GREEN')?.value; // => 1Note: If a value is not provided in a definition, the name of the enum value will be used as its internal value.
Constructor
Creates a GraphQLEnumType instance.
Signature:
new GraphQLEnumType(config: Readonly<GraphQLEnumTypeConfig>);
| Name | Type | Description |
|---|---|---|
| config | Readonly<GraphQLEnumTypeConfig> | Configuration describing this object. |
Members
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| extensions | Readonly<GraphQLEnumTypeExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<EnumTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes | readonly EnumTypeExtensionNode[] | AST extension nodes applied to this schema element. |
getValues()
Returns the values defined by this enum type.
Signature:
getValues(): readonly GraphQLEnumValue[];
| Type | Description |
|---|---|
readonly GraphQLEnumValue[] | Enum value definitions in schema order. |
import { buildSchema } from 'graphql/utilities';
import { assertEnumType } from 'graphql/type';
const schema = buildSchema(`
enum Episode {
NEW_HOPE
EMPIRE
JEDI
}
type Query {
episode: Episode
}
`);
const Episode = assertEnumType(schema.getType('Episode'));
Episode.getValues().map((value) => value.name); // => ['NEW_HOPE', 'EMPIRE', 'JEDI']getValue()
Returns the enum value definition for a value name.
Signature:
getValue(
name: string,
): Maybe<GraphQLEnumValue>;
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name to look up. |
| Type | Description |
|---|---|
Maybe<GraphQLEnumValue> | The matching enum value definition, if it exists. |
import { buildSchema } from 'graphql/utilities';
import { assertEnumType } from 'graphql/type';
const schema = buildSchema(`
enum Episode {
NEW_HOPE
EMPIRE
}
type Query {
episode: Episode
}
`);
const Episode = assertEnumType(schema.getType('Episode'));
Episode.getValue('EMPIRE')?.name; // => 'EMPIRE'
Episode.getValue('JEDI'); // => undefinedserialize() Deprecated
Serializes a runtime enum value as a GraphQL enum name.
Signature:
serialize(
outputValue: unknown,
): Maybe<string>;
| Name | Type | Description |
|---|---|---|
| outputValue | unknown | Runtime enum value to serialize. |
| Type | Description |
|---|---|
Maybe<string> | The GraphQL enum name for the runtime value. This deprecated method delegates to coerceOutputValue(); callcoerceOutputValue() directly instead. |
import { GraphQLEnumType } from 'graphql/type';
const RGB = new GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 },
},
});
RGB.serialize(1); // => 'GREEN'
RGB.serialize(3); // throws an errorcoerceOutputValue()
Coerces a runtime enum value to a GraphQL enum name.
Signature:
coerceOutputValue(
outputValue: unknown,
): Maybe<string>;
| Name | Type | Description |
|---|---|---|
| outputValue | unknown | Runtime enum value to coerce. |
| Type | Description |
|---|---|
Maybe<string> | The GraphQL enum name for the runtime value. |
import { GraphQLEnumType } from 'graphql/type';
const RGB = new GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 },
},
});
RGB.coerceOutputValue(1); // => 'GREEN'
RGB.coerceOutputValue(3); // throws an errorparseValue() Deprecated
Deprecated legacy enum parser for externally provided input values. Use
coerceInputValue() instead.
Signature:
parseValue(
inputValue: unknown,
hideSuggestions?: Maybe<boolean>,
): any;
| Name | Type | Description |
|---|---|---|
| inputValue | unknown | External enum name to parse. |
| hideSuggestions? | Maybe<boolean> | Whether suggestion text should be omitted from errors. |
| Type | Description |
|---|---|
any | The internal runtime value for the enum name. |
import { GraphQLEnumType } from 'graphql/type';
const RGB = new GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 },
},
});
RGB.parseValue('BLUE'); // => 2
RGB.parseValue('PURPLE', true); // throws an errorcoerceInputValue()
Coerces an external enum name to its internal runtime value.
Signature:
coerceInputValue(
inputValue: unknown,
hideSuggestions?: Maybe<boolean>,
): any;
| Name | Type | Description |
|---|---|---|
| inputValue | unknown | External enum name to coerce. |
| hideSuggestions? | Maybe<boolean> | Whether suggestion text should be omitted from errors. |
| Type | Description |
|---|---|
any | The internal runtime value for the enum name. |
import { GraphQLEnumType } from 'graphql/type';
const RGB = new GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 },
},
});
RGB.coerceInputValue('BLUE'); // => 2
RGB.coerceInputValue('PURPLE'); // throws an error
RGB.coerceInputValue(2); // throws an errorparseLiteral() Deprecated
Deprecated legacy enum parser for externally provided input literals. Use
coerceInputLiteral() instead.
Signature:
parseLiteral(
valueNode: ValueNode,
_variables: Maybe<ObjMap<unknown>>,
hideSuggestions?: Maybe<boolean>,
): any;
| Name | Type | Description |
|---|---|---|
| valueNode | ValueNode | Enum value AST node to parse. |
| _variables | Maybe<ObjMap<unknown>> | Deprecated variable values parameter that is no longer used. |
| hideSuggestions? | Maybe<boolean> | Whether suggestion text should be omitted from errors. |
| Type | Description |
|---|---|
any | The internal runtime value for the enum literal. |
import { parseValue } from 'graphql/language';
import { GraphQLEnumType } from 'graphql/type';
const RGB = new GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 },
},
});
RGB.parseLiteral(parseValue('RED')); // => 0
RGB.parseLiteral(parseValue('"RED"')); // throws an errorcoerceInputLiteral()
Coerces an enum value AST node to its internal runtime value.
Signature:
coerceInputLiteral(
valueNode: ConstValueNode,
hideSuggestions?: Maybe<boolean>,
): any;
| Name | Type | Description |
|---|---|---|
| valueNode | ConstValueNode | Enum value AST node to coerce. |
| hideSuggestions? | Maybe<boolean> | Whether suggestion text should be omitted from errors. |
| Type | Description |
|---|---|
any | The internal runtime value for the enum literal. |
import { parseConstValue } from 'graphql/language';
import { GraphQLEnumType } from 'graphql/type';
const RGB = new GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 },
},
});
RGB.coerceInputLiteral(parseConstValue('RED')); // => 0
RGB.coerceInputLiteral(parseConstValue('"RED"'), true); // throws an errorvalueToLiteral()
Converts a runtime enum value to a GraphQL enum value AST node.
Signature:
valueToLiteral(
value: unknown,
): ConstValueNode | undefined;
| Name | Type | Description |
|---|---|---|
| value | unknown | Runtime enum value to convert. |
| Type | Description |
|---|---|
ConstValueNode | undefined | Enum value AST node, or undefined if the value is invalid. |
import { print } from 'graphql/language';
import { GraphQLEnumType } from 'graphql/type';
const RGB = new GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 },
},
});
print(RGB.valueToLiteral(2)); // => 'BLUE'
RGB.valueToLiteral(3); // => undefinedtoConfig()
Returns a normalized configuration object for this object.
Signature:
toConfig(): GraphQLEnumTypeNormalizedConfig;
| Type | Description |
|---|---|
GraphQLEnumTypeNormalizedConfig | A configuration object that can be used to recreate this object. |
import { GraphQLEnumType } from 'graphql/type';
const RGB = new GraphQLEnumType({
name: 'RGB',
values: {
RED: { value: 0 },
GREEN: { value: 1 },
BLUE: { value: 2 },
},
});
const config = RGB.toConfig();
const RGBCopy = new GraphQLEnumType(config);
config.values.GREEN.value; // => 1
RGBCopy.coerceOutputValue(2); // => 'BLUE'toString()
Returns the schema coordinate identifying this enum type.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The schema coordinate for this enum type. |
import { buildSchema } from 'graphql/utilities';
import { assertEnumType } from 'graphql/type';
const schema = buildSchema(`
enum Episode {
NEW_HOPE
}
type Query {
episode: Episode
}
`);
const Episode = assertEnumType(schema.getType('Episode'));
Episode.toString(); // => 'Episode'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The JSON-serializable representation. |
import { GraphQLEnumType } from 'graphql/type';
const Episode = new GraphQLEnumType({
name: 'Episode',
values: {
NEW_HOPE: {},
},
});
Episode.toJSON(); // => 'Episode'
JSON.stringify({ type: Episode }); // => '{"type":"Episode"}'GraphQLInputObjectType
Input Object Type Definition
An input object defines a structured collection of fields which may be supplied to a field argument.
Using NonNull will ensure that a value must be provided by the query
const GeoPoint = new GraphQLInputObjectType({
name: 'GeoPoint',
fields: {
lat: { type: new GraphQLNonNull(GraphQLFloat) },
lon: { type: new GraphQLNonNull(GraphQLFloat) },
alt: { type: GraphQLFloat, default: { value: 0 } },
}
});Constructor
Creates a GraphQLInputObjectType instance.
Signature:
new GraphQLInputObjectType(config: Readonly<GraphQLInputObjectTypeConfig>);
| Name | Type | Description |
|---|---|---|
| config | Readonly<GraphQLInputObjectTypeConfig> | Configuration describing this object. |
Members
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| extensions | Readonly<GraphQLInputObjectTypeExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<InputObjectTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes | readonly InputObjectTypeExtensionNode[] | AST extension nodes applied to this schema element. |
| isOneOf | boolean | Whether this input object uses the experimental OneOf input object semantics. |
getFields()
Returns the fields defined by this type.
Signature:
getFields(): GraphQLInputFieldMap;
| Type | Description |
|---|---|
GraphQLInputFieldMap | The fields keyed by field name. |
import { buildSchema } from 'graphql/utilities';
import { assertInputObjectType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
commentary: String = ""
}
type Query {
reviews(filter: ReviewInput): [String]
}
`);
const ReviewInput = assertInputObjectType(schema.getType('ReviewInput'));
const fields = ReviewInput.getFields();
Object.keys(fields); // => ['stars', 'commentary']
fields.commentary.default; // => { literal: { kind: 'StringValue', value: '' } }toConfig()
Returns a normalized configuration object for this object.
Signature:
toConfig(): GraphQLInputObjectTypeNormalizedConfig;
| Type | Description |
|---|---|
GraphQLInputObjectTypeNormalizedConfig | A configuration object that can be used to recreate this object. |
import {
GraphQLInputObjectType,
GraphQLInt,
GraphQLNonNull,
} from 'graphql/type';
const ReviewInput = new GraphQLInputObjectType({
name: 'ReviewInput',
fields: {
stars: { type: new GraphQLNonNull(GraphQLInt) },
},
});
const config = ReviewInput.toConfig();
const ReviewInputCopy = new GraphQLInputObjectType(config);
String(config.fields.stars.type); // => 'Int!'
String(ReviewInputCopy.getFields().stars.type); // => 'Int!'toString()
Returns the schema coordinate identifying this input object type.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The schema coordinate for this input object type. |
import { buildSchema } from 'graphql/utilities';
import { assertInputObjectType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type Query {
reviews(filter: ReviewInput): [String]
}
`);
const ReviewInput = assertInputObjectType(schema.getType('ReviewInput'));
ReviewInput.toString(); // => 'ReviewInput'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The JSON-serializable representation. |
import { GraphQLInputObjectType, GraphQLString } from 'graphql/type';
const ReviewInput = new GraphQLInputObjectType({
name: 'ReviewInput',
fields: {
commentary: { type: GraphQLString },
},
});
ReviewInput.toJSON(); // => 'ReviewInput'
JSON.stringify({ type: ReviewInput }); // => '{"type":"ReviewInput"}'Functions
isType()
Returns true when the value is any GraphQL type.
Signature:
isType(
type: unknown,
): type is GraphQLType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLType | True when the value is any GraphQL type. |
import { buildSchema } from 'graphql/utilities';
import { GraphQLList, GraphQLString, isType } from 'graphql/type';
const schema = buildSchema(`
type Query {
name: String
}
`);
isType(GraphQLString); // => true
isType(new GraphQLList(GraphQLString)); // => true
isType(schema.getType('Query')); // => true
isType('String'); // => falseassertType()
Returns the value as a GraphQL type, or throws if it is not one.
Signature:
assertType(type: unknown): GraphQLType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLType | The value typed as a GraphQL type. |
import { buildSchema } from 'graphql/utilities';
import { assertType } from 'graphql/type';
const schema = buildSchema(`
type Query {
name: String
}
`);
const queryType = assertType(schema.getType('Query'));
queryType.toString(); // => 'Query'
assertType('Query'); // throws an errorisScalarType()
There are predicates for each kind of GraphQL type.
Signature:
isScalarType(
type: unknown,
): type is GraphQLScalarType<unknown, unknown>;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLScalarType<unknown, unknown> | True when the value is a GraphQLScalarType. |
import { buildSchema } from 'graphql/utilities';
import { isScalarType } from 'graphql/type';
const schema = buildSchema(`
scalar DateTime
type Query {
createdAt: DateTime
}
`);
isScalarType(schema.getType('DateTime')); // => true
isScalarType(schema.getType('Query')); // => falseassertScalarType()
Returns the value as a GraphQLScalarType, or throws if it is not one.
Signature:
assertScalarType(type: unknown): GraphQLScalarType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLScalarType | The value typed as a GraphQLScalarType. |
import { buildSchema } from 'graphql/utilities';
import { assertScalarType } from 'graphql/type';
const schema = buildSchema(`
scalar DateTime
type Query {
createdAt: DateTime
}
`);
const dateTimeType = assertScalarType(schema.getType('DateTime'));
dateTimeType.name; // => 'DateTime'
assertScalarType(schema.getType('Query')); // throws an errorisObjectType()
Returns true when the value is a GraphQLObjectType.
Signature:
isObjectType(
type: unknown,
): type is GraphQLObjectType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLObjectType | True when the value is a GraphQLObjectType. |
import { buildSchema } from 'graphql/utilities';
import { isObjectType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type User {
name: String
}
type Query {
user: User
}
`);
isObjectType(schema.getType('User')); // => true
isObjectType(schema.getType('ReviewInput')); // => falseassertObjectType()
Returns the value as a GraphQLObjectType, or throws if it is not one.
Signature:
assertObjectType(type: unknown): GraphQLObjectType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLObjectType | The value typed as a GraphQLObjectType. |
import { buildSchema } from 'graphql/utilities';
import { assertObjectType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type User {
name: String
}
type Query {
user: User
}
`);
const userType = assertObjectType(schema.getType('User'));
Object.keys(userType.getFields()); // => ['name']
assertObjectType(schema.getType('ReviewInput')); // throws an errorisField()
Returns true when the value is a resolved GraphQL field definition.
Signature:
isField(
field: unknown,
): field is GraphQLField;
| Name | Type | Description |
|---|---|---|
| field | unknown | Value to inspect. |
| Type | Description |
|---|---|
field is GraphQLField | True when the value is a GraphQLField. |
import { buildSchema } from 'graphql/utilities';
import { isField } from 'graphql/type';
const schema = buildSchema('type Query { greeting: String }');
const field = schema.getQueryType().getFields().greeting;
isField(field); // => true
isField(schema.getQueryType()); // => falseassertField()
Returns the value as a GraphQLField, or throws if it is not one.
Signature:
assertField(field: unknown): GraphQLField;
| Name | Type | Description |
|---|---|---|
| field | unknown | Value to inspect. |
| Type | Description |
|---|---|
GraphQLField | The value typed as a GraphQLField. |
import { buildSchema } from 'graphql/utilities';
import { assertField } from 'graphql/type';
const schema = buildSchema('type Query { greeting: String }');
const field = assertField(schema.getQueryType().getFields().greeting);
field.name; // => 'greeting'
assertField(schema.getQueryType()); // throws an errorisArgument()
Returns true when the value is a resolved GraphQL argument definition.
Signature:
isArgument(
arg: unknown,
): arg is GraphQLArgument;
| Name | Type | Description |
|---|---|---|
| arg | unknown | Value to inspect. |
| Type | Description |
|---|---|
arg is GraphQLArgument | True when the value is a GraphQLArgument. |
import { buildSchema } from 'graphql/utilities';
import { isArgument } from 'graphql/type';
const schema = buildSchema('type Query { greeting(name: String): String }');
const arg = schema.getQueryType().getFields().greeting.args[0];
isArgument(arg); // => true
isArgument(schema.getQueryType()); // => falseassertArgument()
Returns the value as a GraphQLArgument, or throws if it is not one.
Signature:
assertArgument(arg: unknown): GraphQLArgument;
| Name | Type | Description |
|---|---|---|
| arg | unknown | Value to inspect. |
| Type | Description |
|---|---|
GraphQLArgument | The value typed as a GraphQLArgument. |
import { buildSchema } from 'graphql/utilities';
import { assertArgument } from 'graphql/type';
const schema = buildSchema('type Query { greeting(name: String): String }');
const arg = assertArgument(schema.getQueryType().getFields().greeting.args[0]);
arg.name; // => 'name'
assertArgument(schema.getQueryType()); // throws an errorisInterfaceType()
Returns true when the value is a GraphQLInterfaceType.
Signature:
isInterfaceType(
type: unknown,
): type is GraphQLInterfaceType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLInterfaceType | True when the value is a GraphQLInterfaceType. |
import { buildSchema } from 'graphql/utilities';
import { isInterfaceType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
type Query {
node: Node
}
`);
isInterfaceType(schema.getType('Node')); // => true
isInterfaceType(schema.getType('User')); // => falseassertInterfaceType()
Returns the value as a GraphQLInterfaceType, or throws if it is not one.
Signature:
assertInterfaceType(type: unknown): GraphQLInterfaceType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLInterfaceType | The value typed as a GraphQLInterfaceType. |
import { buildSchema } from 'graphql/utilities';
import { assertInterfaceType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
type Query {
node: Node
}
`);
const nodeType = assertInterfaceType(schema.getType('Node'));
nodeType.name; // => 'Node'
assertInterfaceType(schema.getType('User')); // throws an errorisUnionType()
Returns true when the value is a GraphQLUnionType.
Signature:
isUnionType(
type: unknown,
): type is GraphQLUnionType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLUnionType | True when the value is a GraphQLUnionType. |
import { buildSchema } from 'graphql/utilities';
import { isUnionType } from 'graphql/type';
const schema = buildSchema(`
type Photo {
url: String!
}
type Video {
url: String!
}
union Media = Photo | Video
type Query {
media: [Media]
}
`);
isUnionType(schema.getType('Media')); // => true
isUnionType(schema.getType('Photo')); // => falseassertUnionType()
Returns the value as a GraphQLUnionType, or throws if it is not one.
Signature:
assertUnionType(type: unknown): GraphQLUnionType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLUnionType | The value typed as a GraphQLUnionType. |
import { buildSchema } from 'graphql/utilities';
import { assertUnionType } from 'graphql/type';
const schema = buildSchema(`
type Photo {
url: String!
}
type Video {
url: String!
}
union Media = Photo | Video
type Query {
media: [Media]
}
`);
const mediaType = assertUnionType(schema.getType('Media'));
mediaType.getTypes().map((type) => type.name); // => ['Photo', 'Video']
assertUnionType(schema.getType('Photo')); // throws an errorisEnumType()
Returns true when the value is a GraphQLEnumType.
Signature:
isEnumType(
type: unknown,
): type is GraphQLEnumType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLEnumType | True when the value is a GraphQLEnumType. |
import { buildSchema } from 'graphql/utilities';
import { isEnumType } from 'graphql/type';
const schema = buildSchema(`
enum Episode {
NEW_HOPE
EMPIRE
}
type Query {
favoriteEpisode: Episode
}
`);
isEnumType(schema.getType('Episode')); // => true
isEnumType(schema.getType('Query')); // => falseassertEnumType()
Returns the value as a GraphQLEnumType, or throws if it is not one.
Signature:
assertEnumType(type: unknown): GraphQLEnumType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLEnumType | The value typed as a GraphQLEnumType. |
import { buildSchema } from 'graphql/utilities';
import { assertEnumType } from 'graphql/type';
const schema = buildSchema(`
enum Episode {
NEW_HOPE
EMPIRE
}
type Query {
favoriteEpisode: Episode
}
`);
const episodeType = assertEnumType(schema.getType('Episode'));
episodeType.getValues().map((value) => value.name); // => ['NEW_HOPE', 'EMPIRE']
assertEnumType(schema.getType('Query')); // throws an errorisEnumValue()
Returns true when the value is a resolved GraphQL enum value definition.
Signature:
isEnumValue(
value: unknown,
): value is GraphQLEnumValue;
| Name | Type | Description |
|---|---|---|
| value | unknown | Value to inspect. |
| Type | Description |
|---|---|
value is GraphQLEnumValue | True when the value is a GraphQLEnumValue. |
import { buildSchema } from 'graphql/utilities';
import { assertEnumType, isEnumValue } from 'graphql/type';
const schema = buildSchema('enum Episode { NEW_HOPE } type Query { episode: Episode }');
const enumValue = assertEnumType(schema.getType('Episode')).getValues()[0];
isEnumValue(enumValue); // => true
isEnumValue(schema.getType('Episode')); // => falseassertEnumValue()
Returns the value as a GraphQLEnumValue, or throws if it is not one.
Signature:
assertEnumValue(value: unknown): GraphQLEnumValue;
| Name | Type | Description |
|---|---|---|
| value | unknown | Value to inspect. |
| Type | Description |
|---|---|
GraphQLEnumValue | The value typed as a GraphQLEnumValue. |
import { buildSchema } from 'graphql/utilities';
import { assertEnumType, assertEnumValue } from 'graphql/type';
const schema = buildSchema('enum Episode { NEW_HOPE } type Query { episode: Episode }');
const enumValue = assertEnumValue(
assertEnumType(schema.getType('Episode')).getValues()[0],
);
enumValue.name; // => 'NEW_HOPE'
assertEnumValue(schema.getType('Episode')); // throws an errorisInputObjectType()
Returns true when the value is a GraphQLInputObjectType.
Signature:
isInputObjectType(
type: unknown,
): type is GraphQLInputObjectType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLInputObjectType | True when the value is a GraphQLInputObjectType. |
import { buildSchema } from 'graphql/utilities';
import { isInputObjectType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type Review {
stars: Int!
}
type Query {
review(input: ReviewInput): Review
}
`);
isInputObjectType(schema.getType('ReviewInput')); // => true
isInputObjectType(schema.getType('Review')); // => falseassertInputObjectType()
Returns the value as a GraphQLInputObjectType, or throws if it is not one.
Signature:
assertInputObjectType(type: unknown): GraphQLInputObjectType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLInputObjectType | The value typed as a GraphQLInputObjectType. |
import { buildSchema } from 'graphql/utilities';
import { assertInputObjectType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type Review {
stars: Int!
}
type Query {
review(input: ReviewInput): Review
}
`);
const inputType = assertInputObjectType(schema.getType('ReviewInput'));
Object.keys(inputType.getFields()); // => ['stars']
assertInputObjectType(schema.getType('Review')); // throws an errorisInputField()
Returns true when the value is a resolved GraphQL input field definition.
Signature:
isInputField(
field: unknown,
): field is GraphQLInputField;
| Name | Type | Description |
|---|---|---|
| field | unknown | Value to inspect. |
| Type | Description |
|---|---|
field is GraphQLInputField | True when the value is a GraphQLInputField. |
import { buildSchema } from 'graphql/utilities';
import { assertInputObjectType, isInputField } from 'graphql/type';
const schema = buildSchema('input ReviewInput { stars: Int } type Query { ok: Boolean }');
const inputField = assertInputObjectType(schema.getType('ReviewInput')).getFields().stars;
isInputField(inputField); // => true
isInputField(schema.getQueryType()); // => falseassertInputField()
Returns the value as a GraphQLInputField, or throws if it is not one.
Signature:
assertInputField(field: unknown): GraphQLInputField;
| Name | Type | Description |
|---|---|---|
| field | unknown | Value to inspect. |
| Type | Description |
|---|---|
GraphQLInputField | The value typed as a GraphQLInputField. |
import { buildSchema } from 'graphql/utilities';
import { assertInputField, assertInputObjectType } from 'graphql/type';
const schema = buildSchema('input ReviewInput { stars: Int } type Query { ok: Boolean }');
const inputField = assertInputField(
assertInputObjectType(schema.getType('ReviewInput')).getFields().stars,
);
inputField.name; // => 'stars'
assertInputField(schema.getQueryType()); // throws an errorassertListType()
Returns the value as a GraphQLList, or throws if it is not one.
Signature:
assertListType(
type: unknown,
): GraphQLList<GraphQLType>;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLList<GraphQLType> | The value typed as a GraphQLList. |
import { GraphQLList, GraphQLString, assertListType } from 'graphql/type';
const listType = assertListType(new GraphQLList(GraphQLString));
listType.ofType; // => GraphQLString
assertListType(GraphQLString); // throws an errorassertNonNullType()
Returns the value as a GraphQLNonNull, or throws if it is not one.
Signature:
assertNonNullType(
type: unknown,
): GraphQLNonNull<GraphQLNullableType>;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLNonNull<GraphQLNullableType> | The value typed as a GraphQLNonNull. |
import { GraphQLNonNull, GraphQLString, assertNonNullType } from 'graphql/type';
const nonNullType = assertNonNullType(new GraphQLNonNull(GraphQLString));
nonNullType.ofType; // => GraphQLString
assertNonNullType(GraphQLString); // throws an errorisInputType()
Returns true when the value can be used as a GraphQL input type.
Signature:
isInputType(
type: unknown,
): type is GraphQLInputType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLInputType | True when the value can be used as a GraphQL input type. |
import { buildSchema } from 'graphql/utilities';
import { isInputType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type Review {
stars: Int!
}
type Query {
review(input: ReviewInput): Review
}
`);
isInputType(schema.getType('ReviewInput')); // => true
isInputType(schema.getType('Review')); // => falseassertInputType()
Returns the value as a GraphQL input type, or throws if it is not one.
Signature:
assertInputType(type: unknown): GraphQLInputType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLInputType | The value typed as a GraphQL input type. |
import { buildSchema } from 'graphql/utilities';
import { assertInputType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type Review {
stars: Int!
}
type Query {
review(input: ReviewInput): Review
}
`);
const inputType = assertInputType(schema.getType('ReviewInput'));
inputType.toString(); // => 'ReviewInput'
assertInputType(schema.getType('Review')); // throws an errorisOutputType()
Returns true when the value can be used as a GraphQL output type.
Signature:
isOutputType(
type: unknown,
): type is GraphQLOutputType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLOutputType | True when the value can be used as a GraphQL output type. |
import { buildSchema } from 'graphql/utilities';
import { isOutputType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type Review {
stars: Int!
}
type Query {
review(input: ReviewInput): Review
}
`);
isOutputType(schema.getType('Review')); // => true
isOutputType(schema.getType('ReviewInput')); // => falseassertOutputType()
Returns the value as a GraphQL output type, or throws if it is not one.
Signature:
assertOutputType(type: unknown): GraphQLOutputType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLOutputType | The value typed as a GraphQL output type. |
import { buildSchema } from 'graphql/utilities';
import { assertOutputType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type Review {
stars: Int!
}
type Query {
review(input: ReviewInput): Review
}
`);
const outputType = assertOutputType(schema.getType('Review'));
outputType.toString(); // => 'Review'
assertOutputType(schema.getType('ReviewInput')); // throws an errorisLeafType()
Returns true when the value is a GraphQL scalar or enum type.
Signature:
isLeafType(
type: unknown,
): type is GraphQLLeafType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLLeafType | True when the value is a GraphQL scalar or enum type. |
import { buildSchema } from 'graphql/utilities';
import { isLeafType } from 'graphql/type';
const schema = buildSchema(`
enum Episode {
NEW_HOPE
}
type Review {
stars: Int!
}
type Query {
episode: Episode
review: Review
}
`);
isLeafType(schema.getType('Episode')); // => true
isLeafType(schema.getType('String')); // => true
isLeafType(schema.getType('Review')); // => falseassertLeafType()
Returns the value as a GraphQL leaf type, or throws if it is not one.
Signature:
assertLeafType(type: unknown): GraphQLLeafType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLLeafType | The value typed as a GraphQL leaf type. |
import { buildSchema } from 'graphql/utilities';
import { assertLeafType } from 'graphql/type';
const schema = buildSchema(`
enum Episode {
NEW_HOPE
}
type Review {
stars: Int!
}
type Query {
episode: Episode
review: Review
}
`);
const episodeType = assertLeafType(schema.getType('Episode'));
episodeType.toString(); // => 'Episode'
assertLeafType(schema.getType('Review')); // throws an errorisCompositeType()
Returns true when the value is a GraphQL object, interface, or union type.
Signature:
isCompositeType(
type: unknown,
): type is GraphQLCompositeType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLCompositeType | True when the value is a GraphQL object, interface, or union type. |
import { buildSchema } from 'graphql/utilities';
import { isCompositeType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
union SearchResult = User
type Query {
node: Node
search: [SearchResult]
}
`);
isCompositeType(schema.getType('User')); // => true
isCompositeType(schema.getType('Node')); // => true
isCompositeType(schema.getType('SearchResult')); // => true
isCompositeType(schema.getType('String')); // => falseassertCompositeType()
Returns the value as a GraphQL composite type, or throws if it is not one.
Signature:
assertCompositeType(type: unknown): GraphQLCompositeType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLCompositeType | The value typed as a GraphQL composite type. |
import { buildSchema } from 'graphql/utilities';
import { assertCompositeType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
type Query {
node: Node
}
`);
const userType = assertCompositeType(schema.getType('User'));
userType.toString(); // => 'User'
assertCompositeType(schema.getType('String')); // throws an errorisAbstractType()
Returns true when the value is a GraphQL interface or union type.
Signature:
isAbstractType(
type: unknown,
): type is GraphQLAbstractType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLAbstractType | True when the value is a GraphQL interface or union type. |
import { buildSchema } from 'graphql/utilities';
import { isAbstractType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
union SearchResult = User
type Query {
node: Node
search: [SearchResult]
}
`);
isAbstractType(schema.getType('Node')); // => true
isAbstractType(schema.getType('SearchResult')); // => true
isAbstractType(schema.getType('User')); // => falseassertAbstractType()
Returns the value as a GraphQL abstract type, or throws if it is not one.
Signature:
assertAbstractType(type: unknown): GraphQLAbstractType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLAbstractType | The value typed as a GraphQL abstract type. |
import { buildSchema } from 'graphql/utilities';
import { assertAbstractType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
type Query {
node: Node
}
`);
const nodeType = assertAbstractType(schema.getType('Node'));
nodeType.toString(); // => 'Node'
assertAbstractType(schema.getType('User')); // throws an errorisWrappingType()
Returns true when the value is a GraphQL list or non-null wrapper type.
Signature:
isWrappingType(
type: unknown,
): type is GraphQLWrappingType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLWrappingType | True when the value is a GraphQL list or non-null wrapper type. |
import {
GraphQLList,
GraphQLNonNull,
GraphQLString,
isWrappingType,
} from 'graphql/type';
isWrappingType(new GraphQLList(GraphQLString)); // => true
isWrappingType(new GraphQLNonNull(GraphQLString)); // => true
isWrappingType(GraphQLString); // => falseassertWrappingType()
Returns the value as a GraphQL wrapping type, or throws if it is not one.
Signature:
assertWrappingType(type: unknown): GraphQLWrappingType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLWrappingType | The value typed as a GraphQL wrapping type. |
import { GraphQLList, GraphQLString, assertWrappingType } from 'graphql/type';
const wrappingType = assertWrappingType(new GraphQLList(GraphQLString));
wrappingType.toString(); // => '[String]'
assertWrappingType(GraphQLString); // throws an errorisNullableType()
Returns true when the value is a GraphQL type that can accept null.
Signature:
isNullableType(
type: unknown,
): type is GraphQLNullableType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLNullableType | True when the value is a GraphQL type that can accept null. |
import { GraphQLNonNull, GraphQLString, isNullableType } from 'graphql/type';
isNullableType(GraphQLString); // => true
isNullableType(new GraphQLNonNull(GraphQLString)); // => false
isNullableType(null); // => falseassertNullableType()
Returns the value as a nullable GraphQL type, or throws if it is not one.
Signature:
assertNullableType(type: unknown): GraphQLNullableType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLNullableType | The value typed as a nullable GraphQL type. |
import {
GraphQLNonNull,
GraphQLString,
assertNullableType,
} from 'graphql/type';
const nullableType = assertNullableType(GraphQLString);
nullableType; // => GraphQLString
assertNullableType(new GraphQLNonNull(GraphQLString)); // throws an errorisNamedType()
Returns true when the value is a GraphQL named type.
Signature:
isNamedType(
type: unknown,
): type is GraphQLNamedType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLNamedType | True when the value is a GraphQL named type. |
import { GraphQLList, GraphQLString, isNamedType } from 'graphql/type';
isNamedType(GraphQLString); // => true
isNamedType(new GraphQLList(GraphQLString)); // => false
isNamedType(null); // => falseassertNamedType()
Returns the value as a GraphQL named type, or throws if it is not one.
Signature:
assertNamedType(type: unknown): GraphQLNamedType;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLNamedType | The value typed as a GraphQL named type. |
import { GraphQLList, GraphQLString, assertNamedType } from 'graphql/type';
const namedType = assertNamedType(GraphQLString);
namedType.name; // => 'String'
assertNamedType(new GraphQLList(GraphQLString)); // throws an errorresolveReadonlyArrayThunk()
Resolves a thunked readonly array.
| Name | Constraint | Default | Description |
|---|---|---|---|
| T | The element type resolved from the thunk or array. |
Signature:
resolveReadonlyArrayThunk<T>(
thunk: ThunkReadonlyArray<T>,
): readonly T[];
| Name | Type | Description |
|---|---|---|
| thunk | ThunkReadonlyArray<T> | The thunk or value to resolve. |
| Type | Description |
|---|---|
readonly T[] | The resolved readonly array. |
import { GraphQLString, resolveReadonlyArrayThunk } from 'graphql/type';
const lazyFields = resolveReadonlyArrayThunk(() => [GraphQLString]);
const fields = resolveReadonlyArrayThunk([GraphQLString]);
lazyFields; // => [GraphQLString]
fields; // => [GraphQLString]resolveObjMapThunk()
Resolves a thunked object map.
| Name | Constraint | Default | Description |
|---|---|---|---|
| T | The object-map value type resolved from the thunk or map. |
Signature:
resolveObjMapThunk<T>(
thunk: ThunkObjMap<T>,
): ObjMap<T>;
| Name | Type | Description |
|---|---|---|
| thunk | ThunkObjMap<T> | The thunk or value to resolve. |
| Type | Description |
|---|---|
ObjMap<T> | The resolved object map. |
import { GraphQLString, resolveObjMapThunk } from 'graphql/type';
const lazyFields = resolveObjMapThunk(() => ({ name: GraphQLString }));
const fields = resolveObjMapThunk({ name: GraphQLString });
lazyFields.name; // => GraphQLString
fields.name; // => GraphQLStringisRequiredArgument()
Returns true when the argument is non-null and has no default value.
Signature:
isRequiredArgument(
arg: GraphQLArgument | GraphQLVariableSignature,
): boolean;
| Name | Type | Description |
|---|---|---|
| arg | GraphQLArgument | GraphQLVariableSignature | The argument definition to inspect. |
| Type | Description |
|---|---|
boolean | True when the argument is non-null and has no default value. |
import {
GraphQLArgument,
GraphQLField,
GraphQLInt,
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
isRequiredArgument,
} from 'graphql/type';
const Query = new GraphQLObjectType({ name: 'Query', fields: {} });
const field = new GraphQLField(Query, 'reviews', { type: GraphQLString });
const requiredArgument = new GraphQLArgument(field, 'id', {
type: new GraphQLNonNull(GraphQLInt),
});
const optionalArgument = new GraphQLArgument(field, 'name', {
type: GraphQLString,
});
const argumentWithDefault = new GraphQLArgument(field, 'limit', {
type: new GraphQLNonNull(GraphQLInt),
default: { value: 10 },
});
isRequiredArgument(requiredArgument); // => true
isRequiredArgument(optionalArgument); // => false
isRequiredArgument(argumentWithDefault); // => falseisRequiredInputField()
Returns true when the input field is non-null and has no default value.
Signature:
isRequiredInputField(field: GraphQLInputField): boolean;
| Name | Type | Description |
|---|---|---|
| field | GraphQLInputField | The input field definition to inspect. |
| Type | Description |
|---|---|
boolean | True when the input field is non-null and has no default value. |
import {
GraphQLInputField,
GraphQLInputObjectType,
GraphQLInt,
GraphQLNonNull,
GraphQLString,
isRequiredInputField,
} from 'graphql/type';
const ReviewInput = new GraphQLInputObjectType({
name: 'ReviewInput',
fields: {},
});
const requiredField = new GraphQLInputField(ReviewInput, 'id', {
type: new GraphQLNonNull(GraphQLInt),
});
const optionalField = new GraphQLInputField(ReviewInput, 'name', {
type: GraphQLString,
});
const fieldWithDefault = new GraphQLInputField(ReviewInput, 'limit', {
type: new GraphQLNonNull(GraphQLInt),
default: { value: 10 },
});
isRequiredInputField(requiredField); // => true
isRequiredInputField(optionalField); // => false
isRequiredInputField(fieldWithDefault); // => falseTypes
GraphQLType
Type alias. These are all of the possible kinds of types.
type GraphQLType = GraphQLNamedType | GraphQLWrappingType;
GraphQLNullableInputType
Type alias. These types may be used as input types for arguments and directives.
type GraphQLNullableInputType =
| GraphQLNamedInputType
| GraphQLList<GraphQLInputType>;
GraphQLInputType
Type alias. These types may be used as input types for arguments and directives.
type GraphQLInputType =
| GraphQLNullableInputType
| GraphQLNonNull<GraphQLNullableInputType>;
GraphQLNullableOutputType
Type alias. These types may be used as output types as the result of fields.
type GraphQLNullableOutputType =
| GraphQLNamedOutputType
| GraphQLList<GraphQLOutputType>;
GraphQLOutputType
Type alias. These types may be used as output types as the result of fields.
type GraphQLOutputType =
| GraphQLNullableOutputType
| GraphQLNonNull<GraphQLNullableOutputType>;
GraphQLLeafType
Type alias. These types may describe types which may be leaf values.
type GraphQLLeafType = GraphQLScalarType | GraphQLEnumType;
GraphQLCompositeType
Type alias. These types may describe the parent context of a selection set.
type GraphQLCompositeType =
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType;
GraphQLAbstractType
Type alias. These types may describe the parent context of a selection set.
type GraphQLAbstractType = GraphQLInterfaceType | GraphQLUnionType;
GraphQLWrappingType
Type alias. These types wrap and modify other types
type GraphQLWrappingType =
| GraphQLList<GraphQLType>
| GraphQLNonNull<GraphQLNullableType>;
GraphQLNullableType
Type alias. These types can all accept null as a value.
type GraphQLNullableType =
| GraphQLNamedType
| GraphQLList<GraphQLType>;
GraphQLNamedType
Type alias. These named types do not include modifiers like List or NonNull.
type GraphQLNamedType = GraphQLNamedInputType | GraphQLNamedOutputType;
GraphQLNamedInputType
Type alias. A named GraphQL type that can be used as an input type.
type GraphQLNamedInputType =
| GraphQLScalarType
| GraphQLEnumType
| GraphQLInputObjectType;
GraphQLNamedOutputType
Type alias. A named GraphQL type that can be used as an output type.
type GraphQLNamedOutputType =
| GraphQLScalarType
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType;
ThunkReadonlyArray
Type alias. Used while defining GraphQL types to allow for circular references in otherwise immutable type definitions.
| Name | Constraint | Default | Description |
|---|---|---|---|
| T | The element type returned by the thunk or array. |
type ThunkReadonlyArray<T> =
| (() => ReadonlyArray<T>)
| ReadonlyArray<T>;
ThunkObjMap
Type alias. A thunk that resolves to an object map.
| Name | Constraint | Default | Description |
|---|---|---|---|
| T | Value type stored in the object map. |
type ThunkObjMap<T> =
| (() => ObjMap<T>)
| ObjMap<T>;
GraphQLScalarTypeExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLScalarSerializer Deprecated
Type alias. Deprecated function type that serializes a runtime value as a scalar output
value. Use GraphQLScalarOutputValueCoercer instead.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TExternal | External representation accepted from or returned to callers. |
type GraphQLScalarSerializer<TExternal> = (
outputValue: unknown,
) => TExternal;
GraphQLScalarOutputValueCoercer
Type alias. Function used to coerce internal scalar values for response output.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TExternal | External representation accepted from or returned to callers. |
type GraphQLScalarOutputValueCoercer<TExternal> = (
outputValue: unknown,
) => TExternal;
GraphQLScalarValueParser Deprecated
Type alias. Deprecated function type that parses a runtime input value as a scalar input
value. Use GraphQLScalarInputValueCoercer instead.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | Internal runtime representation for this scalar. |
type GraphQLScalarValueParser<TInternal> = (
inputValue: unknown,
) => TInternal;
GraphQLScalarInputValueCoercer
Type alias. Function used to coerce externally provided scalar input values.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | Internal runtime representation for this scalar. |
type GraphQLScalarInputValueCoercer<TInternal> = (
inputValue: unknown,
) => TInternal;
GraphQLScalarLiteralParser Deprecated
Type alias. Deprecated function type that parses a GraphQL value literal as a scalar
input value. Use GraphQLScalarInputLiteralCoercer instead.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | Internal runtime representation for this scalar. |
type GraphQLScalarLiteralParser<TInternal> = (
valueNode: ValueNode,
variables: Maybe<ObjMap<unknown>>,
) => Maybe<TInternal>;
GraphQLScalarInputLiteralCoercer
Type alias. Function used to coerce GraphQL scalar input literals.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | Internal runtime representation for this scalar. |
type GraphQLScalarInputLiteralCoercer<TInternal> = (
valueNode: ConstValueNode,
) => Maybe<TInternal>;
GraphQLScalarTypeConfig
Interface. Configuration used to construct a GraphQLScalarType.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | Internal runtime representation for this scalar. | ||
| TExternal | External representation accepted from or returned to callers. |
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| specifiedByURL? | Maybe<string> | URL identifying the behavior specified for this custom scalar. |
| serialize? | GraphQLScalarSerializer<TExternal> | Deprecated legacy serializer used to convert internal values for response output. Use coerceOutputValue() instead. |
| parseValue? | GraphQLScalarValueParser<TInternal> | Deprecated legacy parser used to convert externally provided input values. Use coerceInputValue() instead. |
| parseLiteral? | GraphQLScalarLiteralParser<TInternal> | Deprecated legacy parser used to convert externally provided input literals. Use replaceVariables() and coerceInputLiteral() instead. |
| coerceOutputValue? | GraphQLScalarOutputValueCoercer<TExternal> | Coerces an internal value to include in a response. |
| coerceInputValue? | GraphQLScalarInputValueCoercer<TInternal> | Coerces an externally provided value to use as an input. |
| coerceInputLiteral? | GraphQLScalarInputLiteralCoercer<TInternal> | Coerces an externally provided const literal value to use as an input. |
| valueToLiteral? | GraphQLScalarValueToLiteral | Translates an externally provided value to a literal (AST). |
| extensions? | Maybe<Readonly<GraphQLScalarTypeExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<ScalarTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes? | Maybe<readonly ScalarTypeExtensionNode[]> | AST extension nodes applied to this schema element. |
GraphQLObjectTypeExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need. We’ve provided these template arguments because this is an open type and you may find them useful.
| Name | Constraint | Default | Description |
|---|---|---|---|
| _TSource | any | Reserved source type parameter for extension typing. | |
| _TContext | any | Reserved context type parameter for extension typing. |
GraphQLObjectTypeConfig
Interface. Configuration used to construct a GraphQLObjectType.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | Source object type passed to resolvers. | ||
| TContext | Context object type passed to resolvers. | ||
| TAbstract | unknown | Runtime value type used for abstract type resolution. |
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| interfaces? | ThunkReadonlyArray<GraphQLInterfaceType> | Interfaces implemented by this object or interface type. |
| fields | ThunkObjMap<
GraphQLFieldConfig<TSource, TContext>
> | Fields declared by this object, interface, input object, or literal. |
| isTypeOf? | Maybe<
GraphQLIsTypeOfFn<TAbstract, TContext>
> | Predicate used to determine whether a runtime value belongs to this object type. |
| extensions? | Maybe<
Readonly<
GraphQLObjectTypeExtensions<TSource, TContext>
>
> | Extension fields to include in the formatted result. |
| astNode? | Maybe<ObjectTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes? | Maybe<readonly ObjectTypeExtensionNode[]> | AST extension nodes applied to this schema element. |
GraphQLTypeResolver
Type alias. Resolves the concrete object type for an abstract GraphQL type.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | Source object type passed to resolvers. | ||
| TContext | Context object type passed to resolvers. |
type GraphQLTypeResolver<TSource, TContext> = (
value: TSource,
context: TContext,
info: GraphQLResolveInfo,
abstractType: GraphQLAbstractType,
) => PromiseOrValue<string | undefined>;
GraphQLIsTypeOfFn
Type alias. Checks whether a runtime value belongs to a GraphQL object type.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TAbstract | Runtime value type used for abstract type resolution. | ||
| TContext | Context object type passed to resolvers. |
type GraphQLIsTypeOfFn<TAbstract, TContext> = (
value: TAbstract,
context: TContext,
info: GraphQLResolveInfo,
) => PromiseOrValue<boolean>;
GraphQLFieldResolver
Type alias. Resolves the runtime value for a GraphQL field.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | Source object type passed to resolvers. | ||
| TContext | Context object type passed to resolvers. | ||
| TArgs | any | Argument object type passed to resolvers. | |
| TResult | unknown | Result value type. |
type GraphQLFieldResolver<
TSource,
TContext,
TArgs = any,
TResult = unknown,
> = (
source: TSource,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo,
) => TResult;
GraphQLResolveInfoHelpers
Interface. Utilities available from resolver info for tracking asynchronous work.
| Name | Type | Description |
|---|---|---|
| promiseAll | (
values: readonly (
| T
| PromiseLike<T>
)[],
) => Promise<T[]> | Promise.all wrapper that allows rejected branches to be tracked as execution async work. Intended use: return or await this promise from resolver work. Un-awaited async side effects are an anti-pattern: const { promiseAll } = info.getAsyncHelpers(); promiseAll([someAsyncWork(), someOtherAsyncWork()]).catch(() => undefined); In that anti-pattern, tracking starts only after rejection (on a later microtask), so this work is not guaranteed to delay hooks.asyncWorkFinished.Use track(...) for un-awaited async side effects:const { track } = info.getAsyncHelpers(); track([ someAsyncWork().catch(() => undefined), someOtherAsyncWork().catch(() => undefined) ]); |
| track | (maybePromises: readonly unknown[]) => void | Tracks asynchronous work that should delay execution completion hooks. |
GraphQLResolveInfo
Interface. Information about the currently executing GraphQL field.
| Name | Type | Description |
|---|---|---|
| fieldName | string | The field name referenced by this schema coordinate. |
| fieldNodes | readonly FieldNode[] | AST field nodes that contributed to the current field execution. |
| returnType | GraphQLOutputType | GraphQL output type declared for the current field. |
| parentType | GraphQLObjectType | Object type that owns the current field. |
| path | Path | Response path where this error occurred during execution. |
| schema | GraphQLSchema | The schema used for validation or execution. |
| fragments | ObjMap<FragmentDefinitionNode> | Fragment definitions in the operation document keyed by fragment name. |
| rootValue | unknown | Initial root value passed to the operation. |
| operation | OperationDefinitionNode | The operation selected for execution. |
| variableValues | VariableValues | Coerced variable values and source metadata for this operation. Resolver code that needs runtime variable values should read variableValues.coerced. |
| getAbortSignal | () => any | Returns the AbortSignal supplied for this execution, if any. |
| getAsyncHelpers | () => GraphQLResolveInfoHelpers | Returns helper functions for tracking asynchronous resolver work. |
GraphQLFieldExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need. We’ve provided these template arguments because this is an open type and you may find them useful.
| Name | Constraint | Default | Description |
|---|---|---|---|
| _TSource | Reserved source type parameter for extension typing. | ||
| _TContext | Reserved context type parameter for extension typing. | ||
| _TArgs | any | Reserved argument type parameter for extension typing. |
GraphQLFieldConfig
Interface. Configuration used to define a GraphQL field.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | Source object type passed to resolvers. | ||
| TContext | Context object type passed to resolvers. | ||
| TArgs | any | Argument object type passed to resolvers. |
| Name | Type | Description |
|---|---|---|
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| type | GraphQLOutputType | The GraphQL type reference or runtime type for this element. |
| args? | GraphQLFieldConfigArgumentMap | Arguments accepted by this field or directive. |
| resolve? | GraphQLFieldResolver<
TSource,
TContext,
TArgs
> | Resolver function used to produce this field value. |
| subscribe? | GraphQLFieldResolver<
TSource,
TContext,
TArgs
> | Resolver function used to create a subscription event stream for this field. |
| deprecationReason? | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions? | Maybe<
Readonly<
GraphQLFieldExtensions<
TSource,
TContext,
TArgs
>
>
> | Extension fields to include in the formatted result. |
| astNode? | Maybe<FieldDefinitionNode> | AST node from which this schema element was built, if available. |
GraphQLFieldConfigArgumentMap
Type alias. A map of argument names to argument configuration objects.
type GraphQLFieldConfigArgumentMap = ObjMap<GraphQLArgumentConfig>;
GraphQLArgumentExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLArgumentConfig
Interface. Configuration used to define a GraphQL argument.
| Name | Type | Description |
|---|---|---|
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| type | GraphQLInputType | The GraphQL type reference or runtime type for this element. |
| defaultValue? | unknown | Deprecated legacy default value for this argument. Use default instead. |
| default? | GraphQLDefaultInput | Default value represented as either a runtime value or a GraphQL literal. |
| deprecationReason? | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions? | Maybe<Readonly<GraphQLArgumentExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<InputValueDefinitionNode> | AST node from which this schema element was built, if available. |
GraphQLFieldConfigMap
Type alias. A map of field names to field configuration objects.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | Source object type passed to resolvers. | ||
| TContext | Context object type passed to resolvers. |
type GraphQLFieldConfigMap<TSource, TContext> =
ObjMap<
GraphQLFieldConfig<TSource, TContext>
>;
GraphQLField
Interface. A resolved GraphQL field definition.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | any | Source object type passed to resolvers. | |
| TContext | any | Context object type passed to resolvers. | |
| TArgs | any | Argument object type passed to resolvers. |
| Name | Type | Description |
|---|---|---|
| parentType | | GraphQLObjectType<TSource, TContext>
| GraphQLInterfaceType<TSource, TContext>
| undefined | Object or interface type that owns this field, if known. |
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| type | GraphQLOutputType | The GraphQL type reference or runtime type for this element. |
| args | readonly GraphQLArgument[] | Arguments accepted by this field or directive. |
| resolve? | GraphQLFieldResolver<
TSource,
TContext,
TArgs
> | Resolver function used to produce this field value. |
| subscribe? | GraphQLFieldResolver<
TSource,
TContext,
TArgs
> | Resolver function used to create a subscription event stream for this field. |
| deprecationReason | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions | Readonly<
GraphQLFieldExtensions<
TSource,
TContext,
TArgs
>
> | Extension fields to include in the formatted result. |
| astNode | Maybe<FieldDefinitionNode> | AST node from which this schema element was built, if available. |
toConfig()
Returns a normalized configuration object for this field.
Signature:
toConfig(): GraphQLFieldNormalizedConfig<
TSource,
TContext,
TArgs
>;
| Type | Description |
|---|---|
GraphQLFieldNormalizedConfig<
TSource,
TContext,
TArgs
> | A configuration object that can be used to recreate this field. |
import { GraphQLField, GraphQLObjectType, GraphQLString } from 'graphql/type';
const Query = new GraphQLObjectType({ name: 'Query', fields: {} });
const field = new GraphQLField(Query, 'greeting', {
type: GraphQLString,
extensions: { cacheSeconds: 60 },
});
field.toConfig().type; // => GraphQLString
field.toConfig().extensions; // => { cacheSeconds: 60 }toString()
Returns this field as a schema coordinate string.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The field coordinate. |
import { GraphQLField, GraphQLObjectType, GraphQLString } from 'graphql/type';
const Query = new GraphQLObjectType({ name: 'Query', fields: {} });
const field = new GraphQLField(Query, 'greeting', { type: GraphQLString });
field.toString(); // => 'Query.greeting'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The field coordinate. |
import { GraphQLField, GraphQLObjectType, GraphQLString } from 'graphql/type';
const Query = new GraphQLObjectType({ name: 'Query', fields: {} });
const field = new GraphQLField(Query, 'greeting', { type: GraphQLString });
JSON.stringify(field); // => '"Query.greeting"'GraphQLArgument
Interface. A resolved GraphQL argument definition.
| Name | Type | Description |
|---|---|---|
| parent | GraphQLDirective | GraphQLField | Field or directive that owns this argument. |
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| type | GraphQLInputType | The GraphQL type reference or runtime type for this element. |
| defaultValue | unknown | Deprecated legacy default value used when no explicit value is supplied. Use default instead. |
| default | GraphQLDefaultInput | undefined | Default value represented as either a runtime value or a GraphQL literal. |
| deprecationReason | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions | Readonly<GraphQLArgumentExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<InputValueDefinitionNode> | AST node from which this schema element was built, if available. |
toConfig()
Returns a normalized configuration object for this argument.
Signature:
toConfig(): GraphQLArgumentNormalizedConfig;
| Type | Description |
|---|---|
GraphQLArgumentNormalizedConfig | A configuration object that can be used to recreate this argument. |
import {
GraphQLArgument,
GraphQLField,
GraphQLObjectType,
GraphQLString,
} from 'graphql/type';
const Query = new GraphQLObjectType({ name: 'Query', fields: {} });
const field = new GraphQLField(Query, 'greeting', { type: GraphQLString });
const arg = new GraphQLArgument(field, 'name', {
type: GraphQLString,
default: { value: 'world' },
});
arg.toConfig().default.value; // => 'world'toString()
Returns this argument as a schema coordinate string.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The argument coordinate. |
import {
GraphQLArgument,
GraphQLField,
GraphQLObjectType,
GraphQLString,
} from 'graphql/type';
const Query = new GraphQLObjectType({ name: 'Query', fields: {} });
const field = new GraphQLField(Query, 'greeting', { type: GraphQLString });
const arg = new GraphQLArgument(field, 'name', { type: GraphQLString });
arg.toString(); // => 'Query.greeting(name:)'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The argument coordinate. |
import {
GraphQLArgument,
GraphQLField,
GraphQLObjectType,
GraphQLString,
} from 'graphql/type';
const Query = new GraphQLObjectType({ name: 'Query', fields: {} });
const field = new GraphQLField(Query, 'greeting', { type: GraphQLString });
const arg = new GraphQLArgument(field, 'name', { type: GraphQLString });
JSON.stringify(arg); // => '"Query.greeting(name:)"'GraphQLFieldMap
Type alias. A map of field names to resolved field definitions.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | Source object type passed to resolvers. | ||
| TContext | Context object type passed to resolvers. |
type GraphQLFieldMap<TSource, TContext> =
ObjMap<
GraphQLField<TSource, TContext>
>;
GraphQLDefaultInput
Type alias. Default input represented as either a runtime value or a GraphQL literal.
type GraphQLDefaultInput =
| { value: unknown; literal?: never }
| { literal: ConstValueNode; value?: never };
GraphQLInterfaceTypeExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLInterfaceTypeConfig
Interface. Configuration used to construct a GraphQLInterfaceType.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | Source object type passed to resolvers. | ||
| TContext | Context object type passed to resolvers. |
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| interfaces? | ThunkReadonlyArray<GraphQLInterfaceType> | Interfaces implemented by this object or interface type. |
| fields | ThunkObjMap<
GraphQLFieldConfig<TSource, TContext>
> | Fields declared by this object, interface, input object, or literal. |
| resolveType? | Maybe<
GraphQLTypeResolver<TSource, TContext>
> | Optionally provide a custom type resolver function. If one is not provided, the default implementation will call isTypeOf on each implementingObject type. |
| extensions? | Maybe<Readonly<GraphQLInterfaceTypeExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<InterfaceTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes? | Maybe<readonly InterfaceTypeExtensionNode[]> | AST extension nodes applied to this schema element. |
GraphQLUnionTypeExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLUnionTypeConfig
Interface. Configuration used to construct a GraphQLUnionType.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TSource | Source object type passed to resolvers. | ||
| TContext | Context object type passed to resolvers. |
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| types | ThunkReadonlyArray<GraphQLObjectType> | Object types that belong to this union type. |
| resolveType? | Maybe<
GraphQLTypeResolver<TSource, TContext>
> | Optionally provide a custom type resolver function. If one is not provided, the default implementation will call isTypeOf on each implementingObject type. |
| extensions? | Maybe<Readonly<GraphQLUnionTypeExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<UnionTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes? | Maybe<readonly UnionTypeExtensionNode[]> | AST extension nodes applied to this schema element. |
GraphQLEnumTypeExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLEnumTypeConfig
Interface. Configuration used to construct a GraphQLEnumType.
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| values | ThunkObjMap<GraphQLEnumValueConfig> | Values contained in this enum, list, or input-object definition. |
| extensions? | Maybe<Readonly<GraphQLEnumTypeExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<EnumTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes? | Maybe<readonly EnumTypeExtensionNode[]> | AST extension nodes applied to this schema element. |
GraphQLEnumValueConfigMap
Type alias. A map of enum value names to enum value configuration objects.
type GraphQLEnumValueConfigMap = ObjMap<GraphQLEnumValueConfig>;
GraphQLEnumValueExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLEnumValueConfig
Interface. Configuration used to define a GraphQL enum value.
| Name | Type | Description |
|---|---|---|
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| value? | any | Parsed value represented by this node. |
| deprecationReason? | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions? | Maybe<Readonly<GraphQLEnumValueExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<EnumValueDefinitionNode> | AST node from which this schema element was built, if available. |
GraphQLEnumValue
Interface. A resolved GraphQL enum value definition.
| Name | Type | Description |
|---|---|---|
| parentEnum | GraphQLEnumType | Enum type that owns this enum value. |
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| value | any | Parsed value represented by this node. |
| deprecationReason | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions | Readonly<GraphQLEnumValueExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<EnumValueDefinitionNode> | AST node from which this schema element was built, if available. |
toConfig()
Returns a normalized configuration object for this enum value.
Signature:
toConfig(): GraphQLEnumValueNormalizedConfig;
| Type | Description |
|---|---|
GraphQLEnumValueNormalizedConfig | A configuration object that can be used to recreate this enum value. |
import { GraphQLEnumType, GraphQLEnumValue } from 'graphql/type';
const Episode = new GraphQLEnumType({
name: 'Episode',
values: { NEW_HOPE: { value: 4 } },
});
const enumValue = new GraphQLEnumValue(Episode, 'EMPIRE', {
value: 5,
extensions: { trilogy: 'original' },
});
enumValue.toConfig(); // => { description: undefined, value: 5, deprecationReason: undefined, extensions: { trilogy: 'original' }, astNode: undefined }toString()
Returns this enum value as a schema coordinate string.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | Enum value coordinate. |
import { GraphQLEnumType, GraphQLEnumValue } from 'graphql/type';
const Episode = new GraphQLEnumType({
name: 'Episode',
values: { NEW_HOPE: { value: 4 } },
});
const enumValue = new GraphQLEnumValue(Episode, 'EMPIRE', { value: 5 });
enumValue.toString(); // => 'Episode.EMPIRE'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | Enum value coordinate. |
import { GraphQLEnumType, GraphQLEnumValue } from 'graphql/type';
const Episode = new GraphQLEnumType({
name: 'Episode',
values: { NEW_HOPE: { value: 4 } },
});
const enumValue = new GraphQLEnumValue(Episode, 'EMPIRE', { value: 5 });
JSON.stringify(enumValue); // => '"Episode.EMPIRE"'GraphQLInputObjectTypeExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLInputObjectTypeConfig
Interface. Configuration used to construct a GraphQLInputObjectType.
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| fields | ThunkObjMap<GraphQLInputFieldConfig> | Fields declared by this object, interface, input object, or literal. |
| extensions? | Maybe<Readonly<GraphQLInputObjectTypeExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<InputObjectTypeDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes? | Maybe<readonly InputObjectTypeExtensionNode[]> | AST extension nodes applied to this schema element. |
| isOneOf? | boolean | Whether this input object uses the experimental OneOf input object semantics. |
GraphQLInputFieldExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLInputFieldConfig
Interface. Configuration used to define a GraphQL input field.
| Name | Type | Description |
|---|---|---|
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| type | GraphQLInputType | The GraphQL type reference or runtime type for this element. |
| defaultValue? | unknown | Deprecated legacy default value for this input field. Use defaultinstead. |
| default? | GraphQLDefaultInput | Default value represented as either a runtime value or a GraphQL literal. |
| deprecationReason? | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions? | Maybe<Readonly<GraphQLInputFieldExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<InputValueDefinitionNode> | AST node from which this schema element was built, if available. |
GraphQLInputFieldConfigMap
Type alias. A map of input field names to input field configuration objects.
type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;
GraphQLInputField
Interface. A resolved GraphQL input field definition.
| Name | Type | Description |
|---|---|---|
| parentType | GraphQLInputObjectType | Input object type that owns this input field. |
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| type | GraphQLInputType | The GraphQL type reference or runtime type for this element. |
| defaultValue | unknown | Deprecated legacy default value used when no explicit value is supplied. Use default instead. |
| default | GraphQLDefaultInput | undefined | Default value represented as either a runtime value or a GraphQL literal. |
| deprecationReason | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions | Readonly<GraphQLInputFieldExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<InputValueDefinitionNode> | AST node from which this schema element was built, if available. |
toConfig()
Returns a normalized configuration object for this input field.
Signature:
toConfig(): GraphQLInputFieldNormalizedConfig;
| Type | Description |
|---|---|
GraphQLInputFieldNormalizedConfig | A configuration object that can be used to recreate this input field. |
import { GraphQLInputField, GraphQLInputObjectType, GraphQLString } from 'graphql/type';
const ReviewInput = new GraphQLInputObjectType({
name: 'ReviewInput',
fields: {},
});
const field = new GraphQLInputField(ReviewInput, 'commentary', {
type: GraphQLString,
extensions: { form: 'review' },
});
field.toConfig().extensions; // => { form: 'review' }toString()
Returns this input field as a schema coordinate string.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The input field coordinate. |
import { GraphQLInputField, GraphQLInputObjectType, GraphQLString } from 'graphql/type';
const ReviewInput = new GraphQLInputObjectType({
name: 'ReviewInput',
fields: {},
});
const field = new GraphQLInputField(ReviewInput, 'commentary', {
type: GraphQLString,
});
field.toString(); // => 'ReviewInput.commentary'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The input field coordinate. |
import { GraphQLInputField, GraphQLInputObjectType, GraphQLString } from 'graphql/type';
const ReviewInput = new GraphQLInputObjectType({
name: 'ReviewInput',
fields: {},
});
const field = new GraphQLInputField(ReviewInput, 'commentary', {
type: GraphQLString,
});
JSON.stringify(field); // => '"ReviewInput.commentary"'GraphQLInputFieldMap
Type alias. A map of input field names to resolved input field definitions.
type GraphQLInputFieldMap = ObjMap<GraphQLInputField>;
Category: Directives
Classes:
GraphQLDirective
Functions:
isDirective()assertDirective()isSpecifiedDirective()
Constants:
GraphQLIncludeDirectiveGraphQLSkipDirectiveGraphQLDeferDirectiveGraphQLStreamDirectiveDEFAULT_DEPRECATION_REASONGraphQLDeprecatedDirectiveGraphQLSpecifiedByDirectiveGraphQLOneOfDirectivespecifiedDirectives
Classes
GraphQLDirective
Directives are used by the GraphQL runtime as a way of modifying execution behavior. Type system creators will usually not create these directly.
Constructor
Creates a GraphQLDirective instance.
Signature:
new GraphQLDirective(config: Readonly<GraphQLDirectiveConfig>);
| Name | Type | Description |
|---|---|---|
| config | Readonly<GraphQLDirectiveConfig> | Configuration describing this object. |
Members
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| locations | readonly DirectiveLocation[] | Locations where this directive may be applied. |
| args | readonly GraphQLArgument[] | Arguments accepted by this field or directive. |
| isRepeatable | boolean | Whether this directive may appear more than once at the same location. |
| deprecationReason | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions | Readonly<GraphQLDirectiveExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<DirectiveDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes | readonly DirectiveExtensionNode[] | AST extension nodes applied to this schema element. |
toConfig()
Returns a normalized configuration object for this object.
Signature:
toConfig(): GraphQLDirectiveNormalizedConfig;
| Type | Description |
|---|---|
GraphQLDirectiveNormalizedConfig | A configuration object that can be used to recreate this object. |
import { DirectiveLocation } from 'graphql/language';
import { GraphQLDirective, GraphQLString } from 'graphql/type';
const tag = new GraphQLDirective({
name: 'tag',
locations: [DirectiveLocation.FIELD_DEFINITION],
args: {
name: { type: GraphQLString },
},
});
const config = tag.toConfig();
const tagCopy = new GraphQLDirective(config);
config.args.name.type; // => GraphQLString
tagCopy.args[0].name; // => 'name'toString()
Returns the schema coordinate identifying this directive.
Signature:
toString(): string;
| Type | Description |
|---|---|
string | The directive schema coordinate. |
import { DirectiveLocation } from 'graphql/language';
import { GraphQLDirective } from 'graphql/type';
const tag = new GraphQLDirective({
name: 'tag',
locations: [DirectiveLocation.FIELD_DEFINITION],
});
tag.toString(); // => '@tag'toJSON()
Returns the JSON representation used when this object is serialized.
Signature:
toJSON(): string;
| Type | Description |
|---|---|
string | The JSON-serializable representation. |
import { DirectiveLocation } from 'graphql/language';
import { GraphQLDirective } from 'graphql/type';
const tag = new GraphQLDirective({
name: 'tag',
locations: [DirectiveLocation.FIELD_DEFINITION],
});
tag.toJSON(); // => '@tag'
JSON.stringify({ directive: tag }); // => '{"directive":"@tag"}'Functions
isDirective()
Test if the given value is a GraphQL directive.
Signature:
isDirective(
directive: unknown,
): directive is GraphQLDirective;
| Name | Type | Description |
|---|---|---|
| directive | unknown | Value to inspect. |
| Type | Description |
|---|---|
directive is GraphQLDirective | True when the value is a GraphQLDirective. |
import { DirectiveLocation } from 'graphql/language';
import { GraphQLDirective, GraphQLString, isDirective } from 'graphql/type';
const upper = new GraphQLDirective({
name: 'upper',
locations: [DirectiveLocation.FIELD_DEFINITION],
});
isDirective(upper); // => true
isDirective(GraphQLString); // => falseassertDirective()
Returns the value as a GraphQLDirective, or throws if it is not a directive.
Signature:
assertDirective(directive: unknown): GraphQLDirective;
| Name | Type | Description |
|---|---|---|
| directive | unknown | Value to inspect. |
| Type | Description |
|---|---|
GraphQLDirective | The value typed as a GraphQLDirective. |
import { DirectiveLocation } from 'graphql/language';
import { assertDirective, GraphQLDirective, GraphQLString } from 'graphql/type';
const upper = new GraphQLDirective({
name: 'upper',
locations: [DirectiveLocation.FIELD_DEFINITION],
});
assertDirective(upper); // => upper
assertDirective(GraphQLString); // throws an errorisSpecifiedDirective()
Returns true when the directive is one of the directives specified by GraphQL.
Signature:
isSpecifiedDirective(directive: GraphQLDirective): boolean;
| Name | Type | Description |
|---|---|---|
| directive | GraphQLDirective | Directive to inspect. |
| Type | Description |
|---|---|
boolean | True when the directive is specified by GraphQL. |
import {
GraphQLDirective,
GraphQLIncludeDirective,
isSpecifiedDirective,
} from 'graphql/type';
import { DirectiveLocation } from 'graphql/language';
const customDirective = new GraphQLDirective({
name: 'auth',
locations: [DirectiveLocation.FIELD_DEFINITION],
});
isSpecifiedDirective(GraphQLIncludeDirective); // => true
isSpecifiedDirective(customDirective); // => falseConstants
GraphQLIncludeDirective
Used to conditionally include fields or fragments.
GraphQLDirective
GraphQLSkipDirective
Used to conditionally skip (exclude) fields or fragments.
GraphQLDirective
GraphQLDeferDirective
Experimental directive used to conditionally defer fragments.
This directive is exported for schemas that explicitly opt in to incremental
delivery. It is not included in specifiedDirectives.
GraphQLDirective
GraphQLStreamDirective
Experimental directive used to conditionally stream list fields.
This directive is exported for schemas that explicitly opt in to incremental
delivery. It is not included in specifiedDirectives.
GraphQLDirective
DEFAULT_DEPRECATION_REASON
Constant string used for default reason for a deprecation.
'No longer supported'
GraphQLDeprecatedDirective
Used to declare element of a GraphQL schema as deprecated.
The reason argument is non-null and defaults to
DEFAULT_DEPRECATION_REASON.
GraphQLDirective
GraphQLSpecifiedByDirective
Used to provide a URL for specifying the behavior of custom scalar definitions.
GraphQLDirective
GraphQLOneOfDirective
Used to indicate an Input Object is a OneOf Input Object.
GraphQLDirective
specifiedDirectives
Full list of stable directives specified by GraphQL.js.
Experimental @defer and @stream are exported separately and are not
included in this list.
ReadonlyArray<GraphQLDirective>
Types
GraphQLDirectiveExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLDirectiveConfig
Interface. Configuration used to construct a GraphQLDirective.
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name for this schema element. |
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| locations | readonly DirectiveLocation[] | Locations where this directive may be applied. |
| args? | Maybe<ObjMap<GraphQLArgumentConfig>> | Arguments accepted by this field or directive. |
| isRepeatable? | Maybe<boolean> | Whether this directive may appear more than once at the same location. |
| deprecationReason? | Maybe<string> | Reason this element is deprecated, if one was provided. |
| extensions? | Maybe<Readonly<GraphQLDirectiveExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<DirectiveDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes? | Maybe<readonly DirectiveExtensionNode[]> | AST extension nodes applied to this schema element. |
Category: Introspection
Functions:
isIntrospectionType()
Constants:
__Schema__Directive__DirectiveLocation__Type__Field__InputValue__EnumValue__TypeKindSchemaMetaFieldDefTypeMetaFieldDefTypeNameMetaFieldDefintrospectionTypes
Enumerations:
TypeKind
Functions
isIntrospectionType()
Returns true when the type is one of the built-in introspection types.
Signature:
isIntrospectionType(type: GraphQLNamedType): boolean;
| Name | Type | Description |
|---|---|---|
| type | GraphQLNamedType | The GraphQL type to inspect. |
| Type | Description |
|---|---|
boolean | True when the type is one of the built-in introspection types. |
import { GraphQLString, isIntrospectionType, __Type } from 'graphql/type';
isIntrospectionType(__Type); // => true
isIntrospectionType(GraphQLString); // => falseConstants
__Schema
The introspection type describing a GraphQL schema.
GraphQLObjectType
__Directive
The introspection type describing a GraphQL directive.
GraphQLObjectType
__DirectiveLocation
The introspection enum describing directive locations.
GraphQLEnumType
__Type
The introspection type describing GraphQL types.
GraphQLObjectType
__Field
The introspection type describing object and interface fields.
GraphQLObjectType
__InputValue
The introspection type describing arguments and input fields.
GraphQLObjectType
__EnumValue
The introspection type describing enum values.
GraphQLObjectType
__TypeKind
The introspection enum describing GraphQL type kinds.
GraphQLEnumType
SchemaMetaFieldDef
Note that these are GraphQLField and not GraphQLFieldConfig, so the format for args is different.
GraphQLField<unknown, unknown>
TypeMetaFieldDef
The __type meta field definition used by introspection.
GraphQLField<unknown, unknown>
TypeNameMetaFieldDef
The __typename meta field definition used by execution and introspection.
GraphQLField<unknown, unknown>
introspectionTypes
All introspection types defined by the GraphQL specification.
ReadonlyArray<GraphQLNamedType>
Enumerations
TypeKind
Enumeration. The introspection enum describing the different kinds of GraphQL types.
This is not a TypeScript
enum. GraphQL.js exportsTypeKindas both a runtime const object of literal values and a TypeScript type alias for those values.
| Name | Value |
|---|---|
SCALAR | "SCALAR" |
OBJECT | "OBJECT" |
INTERFACE | "INTERFACE" |
UNION | "UNION" |
ENUM | "ENUM" |
INPUT_OBJECT | "INPUT_OBJECT" |
LIST | "LIST" |
NON_NULL | "NON_NULL" |
Category: Scalars
Functions:
isSpecifiedScalarType()
Constants:
GRAPHQL_MAX_INTGRAPHQL_MIN_INTGraphQLIntGraphQLFloatGraphQLStringGraphQLBooleanGraphQLIDspecifiedScalarTypes
Functions
isSpecifiedScalarType()
Returns true when the scalar type is one of the scalars specified by GraphQL.
Signature:
isSpecifiedScalarType(type: GraphQLNamedType): boolean;
| Name | Type | Description |
|---|---|---|
| type | GraphQLNamedType | The GraphQL type to inspect. |
| Type | Description |
|---|---|
boolean | True when the scalar type is one of the scalars specified by GraphQL. |
import {
GraphQLScalarType,
GraphQLString,
isSpecifiedScalarType,
} from 'graphql/type';
const DateTime = new GraphQLScalarType({
name: 'DateTime',
});
isSpecifiedScalarType(GraphQLString); // => true
isSpecifiedScalarType(DateTime); // => falseConstants
GRAPHQL_MAX_INT
Maximum possible Int value as per GraphQL Spec (32-bit signed integer). n.b. This differs from JavaScript’s numbers that are IEEE 754 doubles safe up-to 2^53 - 1
2147483647
GRAPHQL_MIN_INT
Minimum possible Int value as per GraphQL Spec (32-bit signed integer). n.b. This differs from JavaScript’s numbers that are IEEE 754 doubles safe starting at -(2^53 - 1)
-2147483648
GraphQLInt
The built-in Int scalar type.
GraphQLScalarType<number>
GraphQLFloat
The built-in Float scalar type.
GraphQLScalarType<number>
GraphQLString
The built-in String scalar type.
GraphQLScalarType<string>
GraphQLBoolean
The built-in Boolean scalar type.
GraphQLScalarType<boolean>
GraphQLID
The built-in ID scalar type.
GraphQLScalarType<string>
specifiedScalarTypes
All built-in scalar types defined by the GraphQL specification.
ReadonlyArray<GraphQLScalarType>
Category: Schema
Classes:
GraphQLSchema
Functions:
isSchema()assertSchema()
Classes
GraphQLSchema
Schema Definition
A Schema is created by supplying the root types of each type of operation, query and mutation (optional). A schema definition is then supplied to the validator and executor.
const MyAppQueryRootType = new GraphQLObjectType({
name: 'Query',
fields: {
greeting: { type: GraphQLString },
},
});
const MyAppMutationRootType = new GraphQLObjectType({
name: 'Mutation',
fields: {
setGreeting: { type: GraphQLString },
},
});
const MyAppSchema = new GraphQLSchema({
query: MyAppQueryRootType,
mutation: MyAppMutationRootType,
});When the schema is constructed, by default only the types that are reachable by traversing the root types are included, other types must be explicitly referenced.
const characterInterface = new GraphQLInterfaceType({
name: 'Character',
fields: {
name: { type: GraphQLString },
},
});
const humanType = new GraphQLObjectType({
name: 'Human',
interfaces: [characterInterface],
fields: {
name: { type: GraphQLString },
},
});
const droidType = new GraphQLObjectType({
name: 'Droid',
interfaces: [characterInterface],
fields: {
name: { type: GraphQLString },
},
});
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
hero: { type: characterInterface },
},
}),
// Since this schema references only the `Character` interface it's
// necessary to explicitly list the types that implement it if
// you want them to be included in the final schema.
types: [humanType, droidType],
});If an array of directives are provided to GraphQLSchema, that will be the
exact list of directives represented and allowed. If directives is not
provided then a default set of the specified directives (e.g. @include and
@skip) will be used. If you wish to provide additional directives to
these specified directives, you must explicitly declare them.
const MyAppSchema = new GraphQLSchema({
query: MyAppQueryRootType,
directives: specifiedDirectives.concat([myCustomDirective]),
});Constructor
Creates a GraphQLSchema instance.
Signature:
new GraphQLSchema(config: Readonly<GraphQLSchemaConfig>);
| Name | Type | Description |
|---|---|---|
| config | Readonly<GraphQLSchemaConfig> | Configuration describing this object. |
Members
| Name | Type | Description |
|---|---|---|
| description | Maybe<string> | Human-readable description for this schema element, if provided. |
| extensions | Readonly<GraphQLSchemaExtensions> | Extension fields to include in the formatted result. |
| astNode | Maybe<SchemaDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes | readonly SchemaExtensionNode[] | AST extension nodes applied to this schema element. |
| assumeValid | boolean | Whether this schema instance skips validation checks. |
getQueryType()
Returns the root object type for query operations.
Signature:
getQueryType(): Maybe<GraphQLObjectType>;
| Type | Description |
|---|---|
Maybe<GraphQLObjectType> | The query root type, if this schema defines one. |
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
type Query {
greeting: String
}
`);
schema.getQueryType()?.name; // => 'Query'getMutationType()
Returns the root object type for mutation operations.
Signature:
getMutationType(): Maybe<GraphQLObjectType>;
| Type | Description |
|---|---|
Maybe<GraphQLObjectType> | The mutation root type, if this schema defines one. |
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
type Query {
greeting: String
}
type Mutation {
setGreeting(value: String!): String
}
`);
schema.getMutationType()?.name; // => 'Mutation'getSubscriptionType()
Returns the root object type for subscription operations.
Signature:
getSubscriptionType(): Maybe<GraphQLObjectType>;
| Type | Description |
|---|---|
Maybe<GraphQLObjectType> | The subscription root type, if this schema defines one. |
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
type Query {
greeting: String
}
type Subscription {
greetings: String
}
`);
schema.getSubscriptionType()?.name; // => 'Subscription'getRootType()
Returns the root object type for the requested operation kind.
Signature:
getRootType(
operation: OperationTypeNode,
): Maybe<GraphQLObjectType>;
| Name | Type | Description |
|---|---|---|
| operation | OperationTypeNode | Operation kind to resolve. |
| Type | Description |
|---|---|
Maybe<GraphQLObjectType> | The root object type for the operation kind, if this schema defines one. |
import { OperationTypeNode } from 'graphql/language';
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
type Query {
greeting: String
}
type Mutation {
setGreeting(value: String!): String
}
`);
schema.getRootType(OperationTypeNode.QUERY)?.name; // => 'Query'
schema.getRootType(OperationTypeNode.MUTATION)?.name; // => 'Mutation'
schema.getRootType(OperationTypeNode.SUBSCRIPTION); // => undefinedgetTypeMap()
Returns all named types known to this schema.
Signature:
getTypeMap(): ObjMap<GraphQLNamedType>;
| Type | Description |
|---|---|
ObjMap<GraphQLNamedType> | A map of schema types keyed by type name. |
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
type User {
name: String
}
type Query {
viewer: User
}
`);
const typeMap = schema.getTypeMap();
typeMap.User.name; // => 'User'
typeMap.Query.name; // => 'Query'
typeMap.String.name; // => 'String'getType()
Returns the named type with the provided name.
Signature:
getType(
name: string,
): GraphQLNamedType | undefined;
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name to look up. |
| Type | Description |
|---|---|
GraphQLNamedType | undefined | The named schema type, if one exists. |
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
type User {
name: String
}
type Query {
viewer: User
}
`);
schema.getType('User')?.toString(); // => 'User'
schema.getType('Missing'); // => undefinedgetPossibleTypes()
Returns object types that may be returned for an abstract type.
Signature:
getPossibleTypes(
abstractType: GraphQLAbstractType,
): readonly GraphQLObjectType[];
| Name | Type | Description |
|---|---|---|
| abstractType | GraphQLAbstractType | Interface or union type to inspect. |
| Type | Description |
|---|---|
readonly GraphQLObjectType[] | Object types that may satisfy the abstract type. |
import { buildSchema } from 'graphql/utilities';
import { assertInterfaceType, assertUnionType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
type Organization implements Node {
id: ID!
}
union SearchResult = User | Organization
type Query {
node: Node
search: [SearchResult]
}
`);
const Node = assertInterfaceType(schema.getType('Node'));
const SearchResult = assertUnionType(schema.getType('SearchResult'));
schema.getPossibleTypes(Node).map((type) => type.name); // => ['User', 'Organization']
schema.getPossibleTypes(SearchResult).map((type) => type.name); // => ['User', 'Organization']getImplementations()
Returns objects and interfaces that implement an interface type.
Signature:
getImplementations(interfaceType: GraphQLInterfaceType): {
objects: readonly GraphQLObjectType[];
interfaces: readonly GraphQLInterfaceType[];
};
| Name | Type | Description |
|---|---|---|
| interfaceType | GraphQLInterfaceType | Interface type to inspect. |
| Type | Description |
|---|---|
{
objects: readonly GraphQLObjectType[];
interfaces: readonly GraphQLInterfaceType[];
} | Object and interface implementations of the interface. |
import { buildSchema } from 'graphql/utilities';
import { assertInterfaceType } from 'graphql/type';
const schema = buildSchema(`
interface Resource {
url: String!
}
interface Image implements Resource {
url: String!
width: Int
}
type Photo implements Resource & Image {
url: String!
width: Int
}
type Query {
resource: Resource
}
`);
const Resource = assertInterfaceType(schema.getType('Resource'));
const implementations = schema.getImplementations(Resource);
implementations.interfaces.map((type) => type.name); // => ['Image']
implementations.objects.map((type) => type.name); // => ['Photo']isSubType()
Returns whether one type is a possible runtime subtype of an abstract type.
Signature:
isSubType(
abstractType: GraphQLAbstractType,
maybeSubType: GraphQLObjectType | GraphQLInterfaceType,
): boolean;
| Name | Type | Description |
|---|---|---|
| abstractType | GraphQLAbstractType | Interface or union type to inspect. |
| maybeSubType | GraphQLObjectType | GraphQLInterfaceType | Object or interface type to test as a possible subtype. |
| Type | Description |
|---|---|
boolean | True when the subtype may satisfy the abstract type. |
import { buildSchema } from 'graphql/utilities';
import { assertInterfaceType, assertObjectType } from 'graphql/type';
const schema = buildSchema(`
interface Node {
id: ID!
}
type User implements Node {
id: ID!
}
type Review {
body: String
}
type Query {
node: Node
review: Review
}
`);
const Node = assertInterfaceType(schema.getType('Node'));
const User = assertObjectType(schema.getType('User'));
const Review = assertObjectType(schema.getType('Review'));
schema.isSubType(Node, User); // => true
schema.isSubType(Node, Review); // => falsegetDirectives()
Returns directives available in this schema.
Signature:
getDirectives(): readonly GraphQLDirective[];
| Type | Description |
|---|---|
readonly GraphQLDirective[] | Directives available in this schema. |
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
directive @upper on FIELD_DEFINITION
type Query {
greeting: String @upper
}
`);
schema.getDirectives().map((directive) => directive.name); // => ['include', 'skip', 'deprecated', 'specifiedBy', 'oneOf', 'upper']getDirective()
Returns the current directive definition.
Signature:
getDirective(
name: string,
): Maybe<GraphQLDirective>;
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name to look up. |
| Type | Description |
|---|---|
Maybe<GraphQLDirective> | The current directive definition, if known. |
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
directive @upper on FIELD_DEFINITION
type Query {
greeting: String @upper
}
`);
schema.getDirective('upper')?.name; // => 'upper'
schema.getDirective('missing'); // => undefinedgetField()
This method looks up the field on the given type definition.
It has special casing for the three introspection fields, __schema,
__type and __typename.
__typename is special because it can always be queried as a field, even
in situations where no other fields are allowed, like on a Union.
__schema and __type could get automatically added to the query type,
but that would require mutating type definitions, which would cause issues.
Signature:
getField(
parentType: GraphQLCompositeType,
fieldName: string,
): GraphQLField<unknown, unknown> | undefined;
| Name | Type | Description |
|---|---|---|
| parentType | GraphQLCompositeType | Composite type to look up the field on. |
| fieldName | string | Field name to look up. |
| Type | Description |
|---|---|
GraphQLField<unknown, unknown> | undefined | The field definition, including supported introspection fields. |
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
type Query {
greeting: String
}
`);
const queryType = schema.getQueryType();
schema.getField(queryType, 'greeting')?.name; // => 'greeting'
schema.getField(queryType, '__typename')?.name; // => '__typename'
schema.getField(queryType, 'missing'); // => undefinedtoConfig()
Returns a normalized configuration object for this object.
The returned config preserves the original assumeValid flag so the schema
can be recreated with the same validation behavior.
Signature:
toConfig(): GraphQLSchemaNormalizedConfig;
| Type | Description |
|---|---|
GraphQLSchemaNormalizedConfig | A configuration object that can be used to recreate this object. |
import { buildSchema } from 'graphql/utilities';
import { GraphQLSchema } from 'graphql/type';
const schema = buildSchema(`
type Query {
greeting: String
}
`);
const config = schema.toConfig();
const schemaCopy = new GraphQLSchema(config);
config.query?.name; // => 'Query'
schemaCopy.getQueryType()?.name; // => 'Query'Functions
isSchema()
Test if the given value is a GraphQL schema.
Signature:
isSchema(
schema: unknown,
): schema is GraphQLSchema;
| Name | Type | Description |
|---|---|---|
| schema | unknown | Value to inspect. |
| Type | Description |
|---|---|
schema is GraphQLSchema | True when the value is a GraphQLSchema. |
import { buildSchema } from 'graphql/utilities';
import { GraphQLString, isSchema } from 'graphql/type';
const schema = buildSchema(`
type Query {
greeting: String
}
`);
isSchema(schema); // => true
isSchema(GraphQLString); // => falseassertSchema()
Returns the value as a GraphQLSchema, or throws if it is not a schema.
Signature:
assertSchema(schema: unknown): GraphQLSchema;
| Name | Type | Description |
|---|---|---|
| schema | unknown | GraphQL schema to use. |
| Type | Description |
|---|---|
GraphQLSchema | The value typed as a GraphQLSchema. |
import { buildSchema } from 'graphql/utilities';
import { assertSchema, GraphQLString } from 'graphql/type';
const schema = buildSchema(`
type Query {
greeting: String
}
`);
assertSchema(schema); // => schema
assertSchema(GraphQLString); // throws an errorTypes
GraphQLSchemaExtensions
Interface. Custom extensions
Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.
GraphQLSchemaConfig
Interface. Configuration used to construct a GraphQLSchema.
| Name | Type | Description |
|---|---|---|
| description? | Maybe<string> | Human-readable description for this schema element, if provided. |
| query? | Maybe<GraphQLObjectType> | Root object type for query operations. |
| mutation? | Maybe<GraphQLObjectType> | Root object type for mutation operations. |
| subscription? | Maybe<GraphQLObjectType> | Root object type for subscription operations. |
| types? | Maybe<readonly GraphQLNamedType[]> | Object types that belong to this union type. |
| directives? | Maybe<readonly GraphQLDirective[]> | Directives available in this schema or applied to this AST node. |
| extensions? | Maybe<Readonly<GraphQLSchemaExtensions>> | Extension fields to include in the formatted result. |
| astNode? | Maybe<SchemaDefinitionNode> | AST node from which this schema element was built, if available. |
| extensionASTNodes? | Maybe<readonly SchemaExtensionNode[]> | AST extension nodes applied to this schema element. |
Category: Validation
Functions:
validateSchema()assertValidSchema()
Functions
validateSchema()
Implements the “Type Validation” sub-sections of the specification’s “Type System” section.
Validation runs synchronously, returning an array of encountered errors, or an empty array if no errors were encountered and the Schema is valid.
Signature:
validateSchema(
schema: GraphQLSchema,
): readonly GraphQLError[];
| Name | Type | Description |
|---|---|---|
| schema | GraphQLSchema | GraphQL schema to use. |
| Type | Description |
|---|---|
readonly GraphQLError[] | Schema validation errors, or an empty array when the schema is valid. |
import { validateSchema } from 'graphql/type';
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
type Query {
name: String
}
`);
const errors = validateSchema(schema);
errors; // => []assertValidSchema()
Utility function which asserts a schema is valid by throwing an error if it is invalid.
Signature:
assertValidSchema(schema: GraphQLSchema): void;
| Name | Type | Description |
|---|---|---|
| schema | GraphQLSchema | GraphQL schema to use. |
import { assertValidSchema } from 'graphql/type';
import { buildSchema } from 'graphql/utilities';
const schema = buildSchema(`
type Query {
name: String
}
`);
assertValidSchema(schema); // does not throw