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()isInterfaceType()assertInterfaceType()isUnionType()assertUnionType()isEnumType()assertEnumType()isInputObjectType()assertInputObjectType()isListType()assertListType()isNonNullType()assertNonNullType()isInputType()assertInputType()isOutputType()assertOutputType()isLeafType()assertLeafType()isCompositeType()assertCompositeType()isAbstractType()assertAbstractType()isWrappingType()assertWrappingType()isNullableType()assertNullableType()getNullableType()isNamedType()assertNamedType()getNamedType()resolveReadonlyArrayThunk()resolveObjMapThunk()isRequiredArgument()isRequiredInputField()
Types:
GraphQLTypeGraphQLInputTypeGraphQLOutputTypeGraphQLLeafTypeGraphQLCompositeTypeGraphQLAbstractTypeGraphQLWrappingTypeGraphQLNullableTypeGraphQLNamedTypeGraphQLNamedInputTypeGraphQLNamedOutputTypeThunkReadonlyArrayThunkObjMapGraphQLScalarTypeExtensionsGraphQLScalarSerializerGraphQLScalarValueParserGraphQLScalarLiteralParserGraphQLScalarTypeConfigGraphQLObjectTypeExtensionsGraphQLObjectTypeConfigGraphQLTypeResolverGraphQLIsTypeOfFnGraphQLFieldResolverGraphQLResolveInfoGraphQLFieldExtensionsGraphQLFieldConfigGraphQLFieldConfigArgumentMapGraphQLArgumentExtensionsGraphQLArgumentConfigGraphQLFieldConfigMapGraphQLFieldGraphQLArgumentGraphQLFieldMapGraphQLInterfaceTypeExtensionsGraphQLInterfaceTypeConfigGraphQLUnionTypeExtensionsGraphQLUnionTypeConfigGraphQLEnumTypeExtensionsGraphQLEnumTypeConfigGraphQLEnumValueConfigMapGraphQLEnumValueExtensionsGraphQLEnumValueConfigGraphQLEnumValueGraphQLInputObjectTypeExtensionsGraphQLInputObjectTypeConfigGraphQLInputFieldExtensionsGraphQLInputFieldConfigGraphQLInputFieldConfigMapGraphQLInputFieldGraphQLInputFieldMap
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 serialize 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.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | unknown | The internal runtime representation accepted by this scalar. | |
| TExternal | TInternal | The serialized representation exposed in GraphQL results. |
const OddType = new GraphQLScalarType({
name: 'Odd',
serialize: (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;
}
});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> | Function that converts internal values to externally visible scalar values. |
| parseValue | GraphQLScalarValueParser<TInternal> | Function that converts variable input into this scalar’s internal value. |
| parseLiteral | GraphQLScalarLiteralParser<TInternal> | Function that converts AST input literals into this scalar’s internal value. |
| 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(): {
serialize: GraphQLScalarSerializer<TExternal>;
parseValue: GraphQLScalarValueParser<TInternal>;
parseLiteral: GraphQLScalarLiteralParser<TInternal>;
extensions: Readonly<GraphQLScalarTypeExtensions>;
extensionASTNodes: ReadonlyArray<ScalarTypeExtensionNode>;
};
| Type | Description |
|---|---|
{
serialize: GraphQLScalarSerializer<TExternal>;
parseValue: GraphQLScalarValueParser<TInternal>;
parseLiteral: GraphQLScalarLiteralParser<TInternal>;
extensions: Readonly<GraphQLScalarTypeExtensions>;
extensionASTNodes: ReadonlyArray<ScalarTypeExtensionNode>;
} | 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. |
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>
>,
);
| Name | Type | Description |
|---|---|---|
| config | Readonly<
GraphQLObjectTypeConfig<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. |
| isTypeOf | Maybe<
GraphQLIsTypeOfFn<TSource, 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(): {
interfaces: ReadonlyArray<GraphQLInterfaceType>;
fields: GraphQLFieldConfigMap<any, any>;
extensions: Readonly<
GraphQLObjectTypeExtensions<TSource, TContext>
>;
extensionASTNodes: ReadonlyArray<ObjectTypeExtensionNode>;
};
| Type | Description |
|---|---|
{
interfaces: ReadonlyArray<GraphQLInterfaceType>;
fields: GraphQLFieldConfigMap<any, any>;
extensions: Readonly<
GraphQLObjectTypeExtensions<TSource, TContext>
>;
extensionASTNodes: ReadonlyArray<ObjectTypeExtensionNode>;
} | 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.
const EntityType = new GraphQLInterfaceType({
name: 'Entity',
fields: {
name: { type: GraphQLString }
}
});Constructor
Creates a GraphQLInterfaceType instance.
Signature:
new GraphQLInterfaceType(
config: Readonly<GraphQLInterfaceTypeConfig<any, any>>,
);
| Name | Type | Description |
|---|---|---|
| config | Readonly<GraphQLInterfaceTypeConfig<any, any>> | 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<any, any>> | 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<any, any>;
| Type | Description |
|---|---|
GraphQLFieldMap<any, any> | 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;
| Type | Description |
|---|---|
GraphQLInterfaceTypeNormalizedConfig | 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.
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<any, any>>,
);
| Name | Type | Description |
|---|---|---|
| config | Readonly<GraphQLUnionTypeConfig<any, any>> | 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<any, any>> | 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(): {
types: ReadonlyArray<GraphQLObjectType>;
extensions: Readonly<GraphQLUnionTypeExtensions>;
extensionASTNodes: ReadonlyArray<UnionTypeExtensionNode>;
};
| Type | Description |
|---|---|
{
types: ReadonlyArray<GraphQLObjectType>;
extensions: Readonly<GraphQLUnionTypeExtensions>;
extensionASTNodes: ReadonlyArray<UnionTypeExtensionNode>;
} | 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()
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. |
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 errorparseValue()
Parses a GraphQL enum name from variable input.
Signature:
parseValue(inputValue: unknown): any;
| Name | Type | Description |
|---|---|---|
| inputValue | unknown | Runtime input value to parse. |
| Type | Description |
|---|---|
any | The internal enum value represented by the input 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'); // throws an error
RGB.parseValue(2); // throws an errorparseLiteral()
Parses a GraphQL enum name from an AST value literal.
Signature:
parseLiteral(
valueNode: ValueNode,
_variables: Maybe<ObjMap<unknown>>,
): any;
| Name | Type | Description |
|---|---|---|
| valueNode | ValueNode | AST value literal to parse. |
| _variables | Maybe<ObjMap<unknown>> | Runtime variable values; ignored because enum literals cannot contain variables. |
| Type | Description |
|---|---|
any | The internal enum value represented by the 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 errortoConfig()
Returns a normalized configuration object for this object.
Signature:
toConfig(): {
values: ObjMap<GraphQLEnumValueConfig>;
extensions: Readonly<GraphQLEnumTypeExtensions>;
extensionASTNodes: ReadonlyArray<EnumTypeExtensionNode>;
};
| Type | Description |
|---|---|
{
values: ObjMap<GraphQLEnumValueConfig>;
extensions: Readonly<GraphQLEnumTypeExtensions>;
extensionASTNodes: ReadonlyArray<EnumTypeExtensionNode>;
} | 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.serialize(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, defaultValue: 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.defaultValue; // => ''toConfig()
Returns a normalized configuration object for this object.
Signature:
toConfig(): {
fields: GraphQLInputFieldConfigMap;
extensions: Readonly<GraphQLInputObjectTypeExtensions>;
extensionASTNodes: ReadonlyArray<InputObjectTypeExtensionNode>;
};
| Type | Description |
|---|---|
{
fields: GraphQLInputFieldConfigMap;
extensions: Readonly<GraphQLInputObjectTypeExtensions>;
extensionASTNodes: ReadonlyArray<InputObjectTypeExtensionNode>;
} | 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 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 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 errorisListType()
Returns true when the value is a GraphQLList.
Signature:
isListType(
type: GraphQLInputType,
): type is GraphQLList<GraphQLInputType>;
| Name | Type | Description |
|---|---|---|
| type | GraphQLInputType | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLList<GraphQLInputType> | True when the value is a GraphQLList. |
import { buildSchema } from 'graphql/utilities';
import { GraphQLList, GraphQLString, isListType } from 'graphql/type';
const schema = buildSchema(`
type Query {
tags: [String!]!
}
`);
const tagsField = schema.getQueryType()?.getFields().tags;
isListType(new GraphQLList(GraphQLString)); // => true
isListType(GraphQLString); // => false
isListType(tagsField?.type); // => falseReturns true when the output type is a GraphQLList.
Signature:
isListType(
type: GraphQLOutputType,
): type is GraphQLList<GraphQLOutputType>;
| Name | Type | Description |
|---|---|---|
| type | GraphQLOutputType | The GraphQL output type to inspect. |
| Type | Description |
|---|---|
type is GraphQLList<GraphQLOutputType> | True when the output type is a list type. |
import { buildSchema } from 'graphql/utilities';
import { getNullableType, isListType } from 'graphql/type';
const schema = buildSchema(`
type Query {
tags: [String!]!
}
`);
const tagsField = schema.getQueryType()?.getFields().tags;
const nullableTagsType = getNullableType(tagsField?.type);
isListType(nullableTagsType); // => trueReturns true when the value is a GraphQLList.
Signature:
isListType(
type: unknown,
): type is GraphQLList<GraphQLType>;
| Name | Type | Description |
|---|---|---|
| type | unknown | The value to inspect. |
| Type | Description |
|---|---|
type is GraphQLList<GraphQLType> | True when the value is a list type. |
import { isListType } from 'graphql/type';
isListType('[String]'); // => false
isListType(null); // => falseassertListType()
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 errorisNonNullType()
Returns true when the value is a GraphQLNonNull.
Signature:
isNonNullType(
type: GraphQLInputType,
): type is GraphQLNonNull<GraphQLInputType>;
| Name | Type | Description |
|---|---|---|
| type | GraphQLInputType | The GraphQL type to inspect. |
| Type | Description |
|---|---|
type is GraphQLNonNull<GraphQLInputType> | True when the value is a GraphQLNonNull. |
import { buildSchema } from 'graphql/utilities';
import { GraphQLNonNull, GraphQLString, isNonNullType } from 'graphql/type';
const schema = buildSchema(`
type Query {
name: String!
nickname: String
}
`);
const fields = schema.getQueryType()?.getFields();
isNonNullType(new GraphQLNonNull(GraphQLString)); // => true
isNonNullType(fields?.name.type); // => true
isNonNullType(fields?.nickname.type); // => falseReturns true when the output type is a GraphQLNonNull.
Signature:
isNonNullType(
type: GraphQLOutputType,
): type is GraphQLNonNull<GraphQLOutputType>;
| Name | Type | Description |
|---|---|---|
| type | GraphQLOutputType | The GraphQL output type to inspect. |
| Type | Description |
|---|---|
type is GraphQLNonNull<GraphQLOutputType> | True when the output type is a non-null type. |
import { buildSchema } from 'graphql/utilities';
import { isNonNullType } from 'graphql/type';
const schema = buildSchema(`
type Query {
name: String!
nickname: String
}
`);
const fields = schema.getQueryType()?.getFields();
isNonNullType(fields?.name.type); // => true
isNonNullType(fields?.nickname.type); // => falseReturns true when the value is a GraphQLNonNull.
Signature:
isNonNullType(
type: unknown,
): type is GraphQLNonNull<GraphQLType>;
| Name | Type | Description |
|---|---|---|
| type | unknown | The value to inspect. |
| Type | Description |
|---|---|
type is GraphQLNonNull<GraphQLType> | True when the value is a non-null type. |
import { isNonNullType } from 'graphql/type';
isNonNullType('String!'); // => false
isNonNullType(null); // => falseassertNonNullType()
Returns the value as a GraphQLNonNull, or throws if it is not one.
Signature:
assertNonNullType(
type: unknown,
): GraphQLNonNull<GraphQLType>;
| Name | Type | Description |
|---|---|---|
| type | unknown | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLNonNull<GraphQLType> | 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 errorgetNullableType()
Returns the nullable type.
Signature:
getNullableType(type: null | undefined): void;
| Name | Type | Description |
|---|---|---|
| type | null | undefined | The GraphQL type to inspect. |
import { getNullableType } from 'graphql/type';
getNullableType(null); // => undefined
getNullableType(undefined); // => undefinedReturns the nullable type after removing one non-null wrapper.
| Name | Constraint | Default | Description |
|---|---|---|---|
| T | GraphQLNullableType | The nullable GraphQL type returned after removing one non-null wrapper. |
Signature:
getNullableType<T extends GraphQLNullableType>(
type:
| T
| GraphQLNonNull<T>,
): T;
| Name | Type | Description |
|---|---|---|
| type | T | GraphQLNonNull<T> | A nullable type or non-null wrapper. |
| Type | Description |
|---|---|
T | The nullable type after removing one non-null wrapper, if present. |
import {
GraphQLList,
GraphQLNonNull,
GraphQLString,
getNullableType,
} from 'graphql/type';
const requiredString = new GraphQLNonNull(GraphQLString);
const stringList = new GraphQLList(GraphQLString);
getNullableType(requiredString); // => GraphQLString
getNullableType(stringList); // => stringListReturns the nullable type after removing one non-null wrapper.
Signature:
getNullableType(
type: Maybe<GraphQLType>,
): GraphQLNullableType | undefined;
| Name | Type | Description |
|---|---|---|
| type | Maybe<GraphQLType> | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLNullableType | undefined | The nullable type after removing one non-null wrapper, if present. |
import {
GraphQLList,
GraphQLNonNull,
GraphQLString,
getNullableType,
} from 'graphql/type';
const requiredStringList = new GraphQLNonNull(
new GraphQLList(GraphQLString),
);
getNullableType(requiredStringList).toString(); // => '[String]'
getNullableType(GraphQLString); // => GraphQLStringisNamedType()
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 errorgetNamedType()
Returns the named type.
Signature:
getNamedType(type: null | undefined): void;
| Name | Type | Description |
|---|---|---|
| type | null | undefined | The GraphQL type to inspect. |
import { getNamedType } from 'graphql/type';
getNamedType(null); // => undefined
getNamedType(undefined); // => undefinedReturns the named input type after unwrapping all list and non-null wrappers.
Signature:
getNamedType(
type: GraphQLInputType,
): GraphQLNamedInputType;
| Name | Type | Description |
|---|---|---|
| type | GraphQLInputType | The GraphQL input type to inspect. |
| Type | Description |
|---|---|
GraphQLNamedInputType | The named input type after unwrapping all wrappers. |
import { buildSchema } from 'graphql/utilities';
import { getNamedType } from 'graphql/type';
const schema = buildSchema(`
input ReviewInput {
stars: Int!
}
type Query {
review(input: [ReviewInput!]!): Boolean
}
`);
const inputArg = schema.getQueryType()?.getFields().review.args[0];
getNamedType(inputArg?.type).toString(); // => 'ReviewInput'Returns the named output type after unwrapping all list and non-null wrappers.
Signature:
getNamedType(
type: GraphQLOutputType,
): GraphQLNamedOutputType;
| Name | Type | Description |
|---|---|---|
| type | GraphQLOutputType | The GraphQL output type to inspect. |
| Type | Description |
|---|---|
GraphQLNamedOutputType | The named output type after unwrapping all wrappers. |
import { buildSchema } from 'graphql/utilities';
import { getNamedType } from 'graphql/type';
const schema = buildSchema(`
type User {
name: String
}
type Query {
users: [User!]!
}
`);
const usersField = schema.getQueryType()?.getFields().users;
getNamedType(usersField?.type).toString(); // => 'User'Returns the named type after unwrapping all list and non-null wrappers.
Signature:
getNamedType(
type: GraphQLType,
): GraphQLNamedType;
| Name | Type | Description |
|---|---|---|
| type | GraphQLType | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLNamedType | The named type after unwrapping all wrappers. |
import {
GraphQLList,
GraphQLNonNull,
GraphQLString,
getNamedType,
} from 'graphql/type';
const nestedType = new GraphQLNonNull(
new GraphQLList(new GraphQLNonNull(GraphQLString)),
);
getNamedType(nestedType); // => GraphQLStringReturns the named type after unwrapping all list and non-null wrappers.
Signature:
getNamedType(
type: Maybe<GraphQLType>,
): GraphQLNamedType | undefined;
| Name | Type | Description |
|---|---|---|
| type | Maybe<GraphQLType> | The GraphQL type to inspect. |
| Type | Description |
|---|---|
GraphQLNamedType | undefined | The named type after unwrapping all wrappers, or undefined for nullish input. |
import {
GraphQLList,
GraphQLString,
getNamedType,
} from 'graphql/type';
getNamedType(new GraphQLList(GraphQLString)); // => GraphQLString
getNamedType(undefined); // => undefinedresolveReadonlyArrayThunk()
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): boolean;
| Name | Type | Description |
|---|---|---|
| arg | GraphQLArgument | The argument definition to inspect. |
| Type | Description |
|---|---|
boolean | True when the argument is non-null and has no default value. |
import {
GraphQLInt,
GraphQLNonNull,
GraphQLString,
isRequiredArgument,
} from 'graphql/type';
const requiredArgument = { name: 'id', type: new GraphQLNonNull(GraphQLInt) };
const optionalArgument = { name: 'name', type: GraphQLString };
const argumentWithDefault = {
name: 'limit',
type: new GraphQLNonNull(GraphQLInt),
defaultValue: 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 {
GraphQLInt,
GraphQLNonNull,
GraphQLString,
isRequiredInputField,
} from 'graphql/type';
const requiredField = { name: 'id', type: new GraphQLNonNull(GraphQLInt) };
const optionalField = { name: 'name', type: GraphQLString };
const fieldWithDefault = {
name: 'limit',
type: new GraphQLNonNull(GraphQLInt),
defaultValue: 10,
};
isRequiredInputField(requiredField); // => true
isRequiredInputField(optionalField); // => false
isRequiredInputField(fieldWithDefault); // => falseTypes
GraphQLType
Type alias. These are all of the possible kinds of types.
type GraphQLType =
| GraphQLScalarType
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLList<GraphQLType>
| GraphQLNonNull<
| GraphQLScalarType
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLList<GraphQLType>
>;
GraphQLInputType
Type alias. These types may be used as input types for arguments and directives.
type GraphQLInputType =
| GraphQLScalarType
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLList<GraphQLInputType>
| GraphQLNonNull<
| GraphQLScalarType
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLList<GraphQLInputType>
>;
GraphQLOutputType
Type alias. These types may be used as output types as the result of fields.
type GraphQLOutputType =
| GraphQLScalarType
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType
| GraphQLList<GraphQLOutputType>
| GraphQLNonNull<
| GraphQLScalarType
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType
| GraphQLList<GraphQLOutputType>
>;
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<GraphQLType>;
GraphQLNullableType
Type alias. These types can all accept null as a value.
type GraphQLNullableType =
| GraphQLScalarType
| GraphQLObjectType
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType
| GraphQLInputObjectType
| 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
Type alias. Serializes a runtime value as a scalar output value.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TExternal | The serialized representation returned for GraphQL results. |
type GraphQLScalarSerializer<TExternal> = (
outputValue: unknown,
) => TExternal;
GraphQLScalarValueParser
Type alias. Parses a runtime input value as a scalar input value.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | The internal runtime representation produced from variable input. |
type GraphQLScalarValueParser<TInternal> = (
inputValue: unknown,
) => TInternal;
GraphQLScalarLiteralParser
Type alias. Parses a GraphQL value literal as a scalar input value.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | The internal runtime representation produced from literal input. |
type GraphQLScalarLiteralParser<TInternal> = (
valueNode: ValueNode,
variables?: Maybe<ObjMap<unknown>>,
) => TInternal;
GraphQLScalarTypeConfig
Interface. Configuration used to construct a GraphQLScalarType.
| Name | Constraint | Default | Description |
|---|---|---|---|
| TInternal | The internal runtime representation accepted by this scalar. | ||
| TExternal | The serialized representation exposed in GraphQL results. |
| 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> | Serializes an internal value to include in a response. |
| parseValue? | GraphQLScalarValueParser<TInternal> | Parses an externally provided value to use as an input. |
| parseLiteral? | GraphQLScalarLiteralParser<TInternal> | Parses an externally provided literal value to use as an input. |
| 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. |
| 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<TSource, 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 |
|---|---|---|---|
| TSource | Source object type tested against this object type. | ||
| TContext | Context object type passed to resolvers. |
type GraphQLIsTypeOfFn<TSource, TContext> = (
source: TSource,
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;
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 | { [variable: string]: unknown } | Runtime variable values keyed by variable name. |
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 | Default value used when no explicit value is supplied. |
| 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 | Source object type passed to resolvers. | ||
| TContext | Context object type passed to resolvers. | ||
| TArgs | any | Argument 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. |
| 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. |
GraphQLArgument
Interface. A resolved GraphQL argument definition.
| Name | Type | Description |
|---|---|---|
| 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 | Default value used when no explicit value is supplied. |
| 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. |
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>
>;
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 |
|---|---|---|
| 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. |
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 | Default value used when no explicit value is supplied. |
| 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 |
|---|---|---|
| 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 | Default value used when no explicit value is supplied. |
| 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. |
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:
GraphQLIncludeDirectiveGraphQLSkipDirectiveDEFAULT_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(): {
args: GraphQLFieldConfigArgumentMap;
isRepeatable: boolean;
extensions: Readonly<GraphQLDirectiveExtensions>;
extensionASTNodes: ReadonlyArray<DirectiveExtensionNode>;
};
| Type | Description |
|---|---|
{
args: GraphQLFieldConfigArgumentMap;
isRepeatable: boolean;
extensions: Readonly<GraphQLDirectiveExtensions>;
extensionASTNodes: ReadonlyArray<DirectiveExtensionNode>;
} | 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
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 optional reason argument 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.
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<GraphQLFieldConfigArgumentMap> | 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.
| Name | Value | Description |
|---|---|---|
SCALAR | "SCALAR" | A scalar type. |
OBJECT | "OBJECT" | An object type. |
INTERFACE | "INTERFACE" | An interface type. |
UNION | "UNION" | A union type. |
ENUM | "ENUM" | An enum type. |
INPUT_OBJECT | "INPUT_OBJECT" | An input object type. |
LIST | "LIST" | A list wrapper type. |
NON_NULL | "NON_NULL" | A non-null wrapper type. |
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, number>
GraphQLFloat
The built-in Float scalar type.
GraphQLScalarType<number, number>
GraphQLString
The built-in String scalar type.
GraphQLScalarType<string, string>
GraphQLBoolean
The built-in Boolean scalar type.
GraphQLScalarType<boolean, boolean>
GraphQLID
The built-in ID scalar type.
GraphQLScalarType<string, 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. |
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'); // => 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