v17 APIgraphql/type

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

ExportKindDescription
GraphQLSchema Changed in v17ClassGraphQL schema object containing root operation types, reachable types, directives, and extension metadata. v17 includes getField() for ordinary and meta fields.
isSchema()PredicateChecks whether a value is a GraphQLSchema.
assertSchema()AssertionReturns a GraphQLSchema or throws.
validateSchema()FunctionReturns schema validation errors.
assertValidSchema()FunctionThrows when schema validation fails.
GraphQLSchemaConfigTypeConstructor configuration for GraphQLSchema.
GraphQLSchemaExtensionsTypeSchema 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.

ExportKindDescription
GraphQLScalarType Changed in v17ClassDefines custom leaf values. v17 prefers the coerce* and valueToLiteral method names.
GraphQLObjectTypeClassDefines object types with fields.
GraphQLInterfaceTypeClassDefines shared fields and abstract runtime resolution.
GraphQLUnionTypeClassDefines abstract types with possible object members.
GraphQLEnumTypeClassDefines finite named values.
GraphQLInputObjectTypeClassDefines structured input values.
GraphQLListClassWraps another type as a list.
GraphQLNonNullClassWraps another type as non-null.
const QueryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    greeting: {
      type: GraphQLString,
      resolve: () => 'hello',
    },
  },
});

Type Predicate and Assertion Exports

PredicateAssertionDescription
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

ExportKindDescription
GraphQLField New in v17TypeRuntime field definition returned from object and interface field maps.
GraphQLArgument New in v17TypeRuntime field, directive, or fragment argument definition.
GraphQLInputField New in v17TypeRuntime input object field definition.
GraphQLEnumValue New in v17TypeRuntime enum value definition.
isField() / assertField() New in v17Predicate and assertionChecks GraphQLField instances.
isArgument() / assertArgument() New in v17Predicate and assertionChecks GraphQLArgument instances.
isInputField() / assertInputField() New in v17Predicate and assertionChecks GraphQLInputField instances.
isEnumValue() / assertEnumValue() New in v17Predicate and assertionChecks 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

ExportKindDescription
getNullableType()FunctionRemoves one or more GraphQLNonNull wrappers.
getNamedType()FunctionRemoves all list and non-null wrappers.
isRequiredArgument() Changed in v17FunctionChecks whether an argument is non-null and has no default.
isRequiredInputField() Changed in v17FunctionChecks whether an input field is non-null and has no default.
resolveObjMapThunk()FunctionResolves a lazy object map used by schema config APIs.
resolveReadonlyArrayThunk()FunctionResolves a lazy readonly array used by schema config APIs.
ThunkObjMapTypeObject map or function returning one.
ThunkReadonlyArrayTypeReadonly 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.

ExportKindDescription
GraphQLIntScalar32-bit signed integer.
GraphQLFloatScalarDouble-precision finite number.
GraphQLStringScalarUTF-8 string.
GraphQLBooleanScalarBoolean value.
GraphQLIDScalarID value serialized as a string.
specifiedScalarTypesArrayBuilt-in scalar instances.
isSpecifiedScalarType()PredicateChecks built-in scalar instances.
GRAPHQL_MAX_INTConstantMaximum GraphQL Int value.
GRAPHQL_MIN_INTConstantMinimum 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

ExportKindDescription
GraphQLDirectiveClassRuntime directive definition.
isDirective()PredicateChecks GraphQLDirective.
assertDirective()AssertionReturns a GraphQLDirective or throws.
specifiedDirectivesArrayDirectives included by default in schemas.
isSpecifiedDirective()PredicateChecks whether a directive is specified.
GraphQLIncludeDirectiveDirectiveBuilt-in @include.
GraphQLSkipDirectiveDirectiveBuilt-in @skip.
GraphQLDeprecatedDirectiveDirectiveBuilt-in @deprecated.
GraphQLSpecifiedByDirectiveDirectiveBuilt-in @specifiedBy.
GraphQLOneOfDirectiveDirectiveBuilt-in @oneOf.
GraphQLDeferDirective New in v17DirectiveExperimental @defer; not included in specifiedDirectives.
GraphQLStreamDirective New in v17DirectiveExperimental @stream; not included in specifiedDirectives.
DEFAULT_DEPRECATION_REASONConstantDefault reason text for deprecations.

GraphQLDirective includes deprecationReason and extensionASTNodes. See Directives on Directives for directive deprecation metadata.

Introspection Exports

ExportKindDescription
introspectionTypesArrayBuilt-in introspection type instances.
isIntrospectionType()PredicateChecks built-in introspection types.
__SchemaObject typeIntrospection schema type.
__DirectiveObject typeIntrospection directive type.
__DirectiveLocationEnum typeIntrospection directive location enum.
__TypeObject typeIntrospection type type.
__FieldObject typeIntrospection field type.
__InputValueObject typeIntrospection argument/input field type.
__EnumValueObject typeIntrospection enum value type.
__TypeKindEnum typeIntrospection type kind enum.
TypeKindConst objectType kind constants used by introspection.
SchemaMetaFieldDef Changed in v17FieldDefinition for __schema. In v17 this is a GraphQLField.
TypeMetaFieldDef Changed in v17FieldDefinition for __type. In v17 this is a GraphQLField.
TypeNameMetaFieldDef Changed in v17FieldDefinition for __typename. In v17 this is a GraphQLField.

Name Assertion Exports

ExportKindDescription
assertName()FunctionAsserts that a string is a valid GraphQL name and returns it.
assertEnumValueName()FunctionAsserts that a string is a valid enum value name.

Use these instead of the removed utility exports assertValidName() and isValidNameError().