graphql/type
The graphql/type entry point defines schemas, type constructors, directives,
introspection types, predicates, assertions, and TypeScript types used when
building GraphQL schemas programmatically.
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql/type';The same exports are also available from the root graphql entry point.
Contents
- Schema Exports
- Type Constructor Exports
- Type Predicate and Assertion Exports
- Field-like Schema Element Exports
- Type Unwrapping and Requirement Helpers
- Scalar Exports
- Directive Exports
- Introspection Exports
- Name Assertion Exports
Schema Exports
| Export | Kind | Description |
|---|---|---|
GraphQLSchema Changed in v17 | Class | GraphQL schema object containing root operation types, reachable types, directives, and extension metadata. v17 includes getField() for ordinary and meta fields. |
isSchema() | Predicate | Checks whether a value is a GraphQLSchema. |
assertSchema() | Assertion | Returns a GraphQLSchema or throws. |
validateSchema() | Function | Returns schema validation errors. |
assertValidSchema() | Function | Throws when schema validation fails. |
GraphQLSchemaConfig | Type | Constructor configuration for GraphQLSchema. |
GraphQLSchemaExtensions | Type | Schema extension metadata map. |
const schema = new GraphQLSchema({
query: QueryType,
mutation: MutationType,
});Type Constructor Exports
Argument and input field configs in v17 prefer default: { value } or
default: { literal }. The legacy defaultValue config still works in v17 and
is deprecated for removal in v18.
| Export | Kind | Description |
|---|---|---|
GraphQLScalarType Changed in v17 | Class | Defines custom leaf values. v17 prefers the coerce* and valueToLiteral method names. |
GraphQLObjectType | Class | Defines object types with fields. |
GraphQLInterfaceType | Class | Defines shared fields and abstract runtime resolution. |
GraphQLUnionType | Class | Defines abstract types with possible object members. |
GraphQLEnumType | Class | Defines finite named values. |
GraphQLInputObjectType | Class | Defines structured input values. |
GraphQLList | Class | Wraps another type as a list. |
GraphQLNonNull | Class | Wraps another type as non-null. |
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
greeting: {
type: GraphQLString,
resolve: () => 'hello',
},
},
});Type Predicate and Assertion Exports
| Predicate | Assertion | Description |
|---|---|---|
isType() | assertType() | Any GraphQL named or wrapping type. |
isScalarType() | assertScalarType() | GraphQLScalarType. |
isObjectType() | assertObjectType() | GraphQLObjectType. |
isInterfaceType() | assertInterfaceType() | GraphQLInterfaceType. |
isUnionType() | assertUnionType() | GraphQLUnionType. |
isEnumType() | assertEnumType() | GraphQLEnumType. |
isInputObjectType() | assertInputObjectType() | GraphQLInputObjectType. |
isListType() | assertListType() | GraphQLList. |
isNonNullType() | assertNonNullType() | GraphQLNonNull. |
isInputType() | assertInputType() | Any type valid in an input position. |
isOutputType() | assertOutputType() | Any type valid in an output position. |
isLeafType() | assertLeafType() | Scalar or enum type. |
isCompositeType() | assertCompositeType() | Object, interface, or union type. |
isAbstractType() | assertAbstractType() | Interface or union type. |
isWrappingType() | assertWrappingType() | List or non-null wrapper. |
isNullableType() | assertNullableType() | Any type except non-null wrappers. |
isNamedType() | assertNamedType() | Any named type. |
import { assertObjectType } from 'graphql';
const objectType = assertObjectType(schema.getType('User'));Field-like Schema Element Exports
| Export | Kind | Description |
|---|---|---|
GraphQLField New in v17 | Type | Runtime field definition returned from object and interface field maps. |
GraphQLArgument New in v17 | Type | Runtime field, directive, or fragment argument definition. |
GraphQLInputField New in v17 | Type | Runtime input object field definition. |
GraphQLEnumValue New in v17 | Type | Runtime enum value definition. |
isField() / assertField() New in v17 | Predicate and assertion | Checks GraphQLField instances. |
isArgument() / assertArgument() New in v17 | Predicate and assertion | Checks GraphQLArgument instances. |
isInputField() / assertInputField() New in v17 | Predicate and assertion | Checks GraphQLInputField instances. |
isEnumValue() / assertEnumValue() New in v17 | Predicate and assertion | Checks GraphQLEnumValue instances. |
const userType = assertObjectType(schema.getType('User'));
const nameField = userType.getFields().name;
if (isField(nameField)) {
console.log(nameField.type.toString());
}Type Unwrapping and Requirement Helpers
| Export | Kind | Description |
|---|---|---|
getNullableType() | Function | Removes one or more GraphQLNonNull wrappers. |
getNamedType() | Function | Removes all list and non-null wrappers. |
isRequiredArgument() Changed in v17 | Function | Checks whether an argument is non-null and has no default. |
isRequiredInputField() Changed in v17 | Function | Checks whether an input field is non-null and has no default. |
resolveObjMapThunk() | Function | Resolves a lazy object map used by schema config APIs. |
resolveReadonlyArrayThunk() | Function | Resolves a lazy readonly array used by schema config APIs. |
ThunkObjMap | Type | Object map or function returning one. |
ThunkReadonlyArray | Type | Readonly array or function returning one. |
Scalar Exports
v17 scalar configs prefer coerceOutputValue, coerceInputValue,
coerceInputLiteral, and valueToLiteral. Legacy names (serialize,
parseValue, parseLiteral) remain available for compatibility and are
deprecated for removal in v18.
| Export | Kind | Description |
|---|---|---|
GraphQLInt | Scalar | 32-bit signed integer. |
GraphQLFloat | Scalar | Double-precision finite number. |
GraphQLString | Scalar | UTF-8 string. |
GraphQLBoolean | Scalar | Boolean value. |
GraphQLID | Scalar | ID value serialized as a string. |
specifiedScalarTypes | Array | Built-in scalar instances. |
isSpecifiedScalarType() | Predicate | Checks built-in scalar instances. |
GRAPHQL_MAX_INT | Constant | Maximum GraphQL Int value. |
GRAPHQL_MIN_INT | Constant | Minimum GraphQL Int value. |
import { GraphQLScalarType, Kind } from 'graphql';
const DateTime = new GraphQLScalarType({
name: 'DateTime',
coerceOutputValue(value) {
return new Date(value).toISOString();
},
coerceInputValue(value) {
const date = new Date(value);
if (Number.isNaN(date.getTime())) {
throw new TypeError('DateTime cannot represent an invalid date');
}
return date;
},
valueToLiteral(value) {
return { kind: Kind.STRING, value: new Date(value).toISOString() };
},
});Directive Exports
| Export | Kind | Description |
|---|---|---|
GraphQLDirective | Class | Runtime directive definition. |
isDirective() | Predicate | Checks GraphQLDirective. |
assertDirective() | Assertion | Returns a GraphQLDirective or throws. |
specifiedDirectives | Array | Directives included by default in schemas. |
isSpecifiedDirective() | Predicate | Checks whether a directive is specified. |
GraphQLIncludeDirective | Directive | Built-in @include. |
GraphQLSkipDirective | Directive | Built-in @skip. |
GraphQLDeprecatedDirective | Directive | Built-in @deprecated. |
GraphQLSpecifiedByDirective | Directive | Built-in @specifiedBy. |
GraphQLOneOfDirective | Directive | Built-in @oneOf. |
GraphQLDeferDirective New in v17 | Directive | Experimental @defer; not included in specifiedDirectives. |
GraphQLStreamDirective New in v17 | Directive | Experimental @stream; not included in specifiedDirectives. |
DEFAULT_DEPRECATION_REASON | Constant | Default reason text for deprecations. |
GraphQLDirective includes deprecationReason and extensionASTNodes.
See Directives on Directives for directive
deprecation metadata.
Introspection Exports
| Export | Kind | Description |
|---|---|---|
introspectionTypes | Array | Built-in introspection type instances. |
isIntrospectionType() | Predicate | Checks built-in introspection types. |
__Schema | Object type | Introspection schema type. |
__Directive | Object type | Introspection directive type. |
__DirectiveLocation | Enum type | Introspection directive location enum. |
__Type | Object type | Introspection type type. |
__Field | Object type | Introspection field type. |
__InputValue | Object type | Introspection argument/input field type. |
__EnumValue | Object type | Introspection enum value type. |
__TypeKind | Enum type | Introspection type kind enum. |
TypeKind | Const object | Type kind constants used by introspection. |
SchemaMetaFieldDef Changed in v17 | Field | Definition for __schema. In v17 this is a GraphQLField. |
TypeMetaFieldDef Changed in v17 | Field | Definition for __type. In v17 this is a GraphQLField. |
TypeNameMetaFieldDef Changed in v17 | Field | Definition for __typename. In v17 this is a GraphQLField. |
Name Assertion Exports
| Export | Kind | Description |
|---|---|---|
assertName() | Function | Asserts that a string is a valid GraphQL name and returns it. |
assertEnumValueName() | Function | Asserts that a string is a valid enum value name. |
Use these instead of the removed utility exports assertValidName() and
isValidNameError().