The GraphQL.js type system provides comprehensive classes and utilities for defining GraphQL schemas with types, fields, directives, and resolvers. It includes all built-in GraphQL types, validation functions, and introspection capabilities.
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLScalarType,
GraphQLString,
GraphQLInt,
GraphQLFloat,
GraphQLBoolean,
GraphQLID,
GraphQLNonNull,
GraphQLList,
type GraphQLType,
type GraphQLFieldConfig,
type GraphQLScalarTypeConfig
} from "graphql";The root schema class that contains all types, directives, and root operation types.
/**
* GraphQL schema containing type definitions and root operation types
*/
class GraphQLSchema {
constructor(config: GraphQLSchemaConfig);
/** Get the query root type */
getQueryType(): GraphQLObjectType | undefined;
/** Get the mutation root type */
getMutationType(): GraphQLObjectType | undefined;
/** Get the subscription root type */
getSubscriptionType(): GraphQLObjectType | undefined;
/** Get all types in the schema */
getTypeMap(): TypeMap;
/** Get all directives in the schema */
getDirectives(): ReadonlyArray<GraphQLDirective>;
}
interface GraphQLSchemaConfig {
/** The query root type */
query?: GraphQLObjectType;
/** The mutation root type */
mutation?: GraphQLObjectType;
/** The subscription root type */
subscription?: GraphQLObjectType;
/** Additional types to include in schema */
types?: ReadonlyArray<GraphQLNamedType>;
/** Custom directives */
directives?: ReadonlyArray<GraphQLDirective>;
/** Extension metadata */
extensions?: GraphQLSchemaExtensions;
/** Whether to assume schema is valid */
assumeValid?: boolean;
}
interface GraphQLSchemaExtensions {
[attributeName: string]: unknown;
}Object types represent composite types with fields that can have arguments and return values.
/**
* GraphQL object type with fields and optional interfaces
*/
class GraphQLObjectType<TSource = any, TContext = any> {
constructor(config: GraphQLObjectTypeConfig<TSource, TContext>);
readonly name: string;
readonly description?: string;
/** Get all fields defined on this type */
getFields(): GraphQLFieldMap<TSource, TContext>;
/** Get all interfaces implemented by this type */
getInterfaces(): ReadonlyArray<GraphQLInterfaceType>;
}
interface GraphQLObjectTypeConfig<TSource, TContext> {
name: string;
description?: string;
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>;
interfaces?: ThunkReadonlyArray<GraphQLInterfaceType>;
isTypeOf?: GraphQLIsTypeOfFn<TSource, TContext>;
extensions?: GraphQLObjectTypeExtensions;
}
interface GraphQLFieldConfig<TSource, TContext, TArgs = any> {
type: GraphQLOutputType;
args?: GraphQLFieldConfigArgumentMap;
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs>;
subscribe?: GraphQLFieldResolver<TSource, TContext, TArgs>;
description?: string;
deprecationReason?: string;
extensions?: GraphQLFieldExtensions;
}
interface GraphQLField<TSource, TContext, TArgs = any> {
readonly name: string;
readonly type: GraphQLOutputType;
readonly args: ReadonlyArray<GraphQLArgument>;
readonly resolve?: GraphQLFieldResolver<TSource, TContext, TArgs>;
readonly subscribe?: GraphQLFieldResolver<TSource, TContext, TArgs>;
readonly description?: string;
readonly deprecationReason?: string;
readonly extensions?: GraphQLFieldExtensions;
}Usage Examples:
import { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLNonNull } from "graphql";
const UserType = new GraphQLObjectType({
name: 'User',
description: 'A user in the system',
fields: {
id: {
type: new GraphQLNonNull(GraphQLString),
description: 'Unique user identifier'
},
name: {
type: GraphQLString,
description: 'User display name'
},
age: {
type: GraphQLInt,
resolve: (user) => user.birthYear ? new Date().getFullYear() - user.birthYear : null
},
posts: {
type: new GraphQLList(PostType),
args: {
limit: { type: GraphQLInt, defaultValue: 10 }
},
resolve: (user, { limit }) => fetchUserPosts(user.id, limit)
}
}
});Scalar types represent primitive leaf values with custom serialization and parsing logic.
/**
* Custom scalar type with serialization and parsing functions
*/
class GraphQLScalarType<TInternal = any, TExternal = any> {
constructor(config: GraphQLScalarTypeConfig<TInternal, TExternal>);
readonly name: string;
readonly description?: string;
/** Serialize internal value to external representation */
serialize(outputValue: unknown): TExternal;
/** Parse external value to internal representation */
parseValue(inputValue: unknown): TInternal;
/** Parse AST literal to internal representation */
parseLiteral(valueNode: ValueNode, variables?: { [key: string]: unknown }): TInternal;
}
interface GraphQLScalarTypeConfig<TInternal, TExternal> {
name: string;
description?: string;
serialize?: GraphQLScalarSerializer<TExternal>;
parseValue?: GraphQLScalarValueParser<TInternal>;
parseLiteral?: GraphQLScalarLiteralParser<TInternal>;
extensions?: GraphQLScalarTypeExtensions;
specifiedByURL?: string;
}
type GraphQLScalarSerializer<TExternal> = (outputValue: unknown) => TExternal;
type GraphQLScalarValueParser<TInternal> = (inputValue: unknown) => TInternal;
type GraphQLScalarLiteralParser<TInternal> = (
valueNode: ValueNode,
variables?: Maybe<{ [key: string]: unknown }>
) => TInternal;GraphQL.js provides all standard GraphQL scalar types.
/** String scalar type */
const GraphQLString: GraphQLScalarType<string, string>;
/** Integer scalar type (32-bit signed) */
const GraphQLInt: GraphQLScalarType<number, number>;
/** Float scalar type (IEEE 754 double precision) */
const GraphQLFloat: GraphQLScalarType<number, number>;
/** Boolean scalar type */
const GraphQLBoolean: GraphQLScalarType<boolean, boolean>;
/** ID scalar type (serialized as string) */
const GraphQLID: GraphQLScalarType<string, string>;
/** Array of all built-in scalar types */
const specifiedScalarTypes: ReadonlyArray<GraphQLScalarType>;
/** Integer value constants */
const GRAPHQL_MAX_INT: number; // 2147483647
const GRAPHQL_MIN_INT: number; // -2147483648Usage Examples:
import { GraphQLScalarType, GraphQLError } from "graphql";
import { Kind } from "graphql/language";
// Custom Date scalar
const GraphQLDate = new GraphQLScalarType({
name: 'Date',
description: 'Date custom scalar type',
serialize(value) {
if (value instanceof Date) {
return value.toISOString();
}
throw new GraphQLError(`Value must be a Date: ${value}`);
},
parseValue(value) {
if (typeof value === 'string') {
return new Date(value);
}
throw new GraphQLError(`Value must be a string: ${value}`);
},
parseLiteral(ast) {
if (ast.kind === Kind.STRING) {
return new Date(ast.value);
}
throw new GraphQLError(`Can only parse strings to dates but got a: ${ast.kind}`);
}
});Interface types represent abstract types that object types can implement.
/**
* GraphQL interface type defining a contract for object types
*/
class GraphQLInterfaceType {
constructor(config: GraphQLInterfaceTypeConfig);
readonly name: string;
readonly description?: string;
/** Get all fields defined on this interface */
getFields(): GraphQLFieldMap<any, any>;
/** Get all interfaces this interface extends */
getInterfaces(): ReadonlyArray<GraphQLInterfaceType>;
}
interface GraphQLInterfaceTypeConfig<TSource = any, TContext = any> {
name: string;
description?: string;
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>;
interfaces?: ThunkReadonlyArray<GraphQLInterfaceType>;
resolveType?: GraphQLTypeResolver<TSource, TContext>;
extensions?: GraphQLInterfaceTypeExtensions;
}Union types represent objects that could be one of several possible types.
/**
* GraphQL union type representing multiple possible object types
*/
class GraphQLUnionType {
constructor(config: GraphQLUnionTypeConfig);
readonly name: string;
readonly description?: string;
/** Get all possible types for this union */
getTypes(): ReadonlyArray<GraphQLObjectType>;
}
interface GraphQLUnionTypeConfig<TSource = any, TContext = any> {
name: string;
description?: string;
types: ThunkReadonlyArray<GraphQLObjectType>;
resolveType?: GraphQLTypeResolver<TSource, TContext>;
extensions?: GraphQLUnionTypeExtensions;
}Enum types represent a limited set of possible values.
/**
* GraphQL enum type with a fixed set of values
*/
class GraphQLEnumType {
constructor(config: GraphQLEnumTypeConfig);
readonly name: string;
readonly description?: string;
/** Get all enum values */
getValues(): ReadonlyArray<GraphQLEnumValue>;
/** Get enum value by name */
getValue(name: string): GraphQLEnumValue | undefined;
}
interface GraphQLEnumTypeConfig {
name: string;
description?: string;
values: GraphQLEnumValueConfigMap;
extensions?: GraphQLEnumTypeExtensions;
}
interface GraphQLEnumValueConfigMap {
[key: string]: GraphQLEnumValueConfig;
}
interface GraphQLEnumValueConfig {
value?: any;
description?: string;
deprecationReason?: string;
extensions?: GraphQLEnumValueExtensions;
}
interface GraphQLEnumValue {
readonly name: string;
readonly value: any;
readonly description?: string;
readonly deprecationReason?: string;
readonly extensions?: GraphQLEnumValueExtensions;
}Input object types represent structured inputs for fields and directives.
/**
* GraphQL input object type for structured field arguments
*/
class GraphQLInputObjectType {
constructor(config: GraphQLInputObjectTypeConfig);
readonly name: string;
readonly description?: string;
/** Get all input fields */
getFields(): GraphQLInputFieldMap;
}
interface GraphQLInputObjectTypeConfig {
name: string;
description?: string;
fields: ThunkObjMap<GraphQLInputFieldConfig>;
extensions?: GraphQLInputObjectTypeExtensions;
isOneOf?: boolean;
}
interface GraphQLInputFieldConfig {
type: GraphQLInputType;
defaultValue?: unknown;
description?: string;
deprecationReason?: string;
extensions?: GraphQLInputFieldExtensions;
}
interface GraphQLInputField {
readonly name: string;
readonly type: GraphQLInputType;
readonly defaultValue?: unknown;
readonly description?: string;
readonly deprecationReason?: string;
readonly extensions?: GraphQLInputFieldExtensions;
}List and NonNull types wrap other types to modify their behavior.
/**
* List wrapper type for arrays of values
*/
class GraphQLList<T extends GraphQLType> {
constructor(ofType: T);
readonly ofType: T;
}
/**
* NonNull wrapper type to make types required
*/
class GraphQLNonNull<T extends GraphQLNullableType> {
constructor(ofType: T);
readonly ofType: T;
}Usage Examples:
import {
GraphQLList,
GraphQLNonNull,
GraphQLString,
GraphQLInt
} from "graphql";
// Array of strings
const stringList = new GraphQLList(GraphQLString);
// Required string
const requiredString = new GraphQLNonNull(GraphQLString);
// Required array of required strings
const requiredStringList = new GraphQLNonNull(
new GraphQLList(
new GraphQLNonNull(GraphQLString)
)
);function isType(type: unknown): type is GraphQLType;
function isScalarType(type: unknown): type is GraphQLScalarType;
function isObjectType(type: unknown): type is GraphQLObjectType;
function isInterfaceType(type: unknown): type is GraphQLInterfaceType;
function isUnionType(type: unknown): type is GraphQLUnionType;
function isEnumType(type: unknown): type is GraphQLEnumType;
function isInputObjectType(type: unknown): type is GraphQLInputObjectType;
function isListType(type: unknown): type is GraphQLList<any>;
function isNonNullType(type: unknown): type is GraphQLNonNull<any>;
function isInputType(type: unknown): type is GraphQLInputType;
function isOutputType(type: unknown): type is GraphQLOutputType;
function isLeafType(type: unknown): type is GraphQLLeafType;
function isCompositeType(type: unknown): type is GraphQLCompositeType;
function isAbstractType(type: unknown): type is GraphQLAbstractType;
function isWrappingType(type: unknown): type is GraphQLWrappingType;
function isNullableType(type: unknown): type is GraphQLNullableType;
function isNamedType(type: unknown): type is GraphQLNamedType;
function isSchema(schema: unknown): schema is GraphQLSchema;
function isDirective(directive: unknown): directive is GraphQLDirective;function assertType(type: unknown): GraphQLType;
function assertScalarType(type: unknown): GraphQLScalarType;
function assertObjectType(type: unknown): GraphQLObjectType;
function assertInterfaceType(type: unknown): GraphQLInterfaceType;
function assertUnionType(type: unknown): GraphQLUnionType;
function assertEnumType(type: unknown): GraphQLEnumType;
function assertInputObjectType(type: unknown): GraphQLInputObjectType;
function assertListType(type: unknown): GraphQLList<any>;
function assertNonNullType(type: unknown): GraphQLNonNull<any>;
function assertInputType(type: unknown): GraphQLInputType;
function assertOutputType(type: unknown): GraphQLOutputType;
function assertLeafType(type: unknown): GraphQLLeafType;
function assertCompositeType(type: unknown): GraphQLCompositeType;
function assertAbstractType(type: unknown): GraphQLAbstractType;
function assertWrappingType(type: unknown): GraphQLWrappingType;
function assertNullableType(type: unknown): GraphQLNullableType;
function assertNamedType(type: unknown): GraphQLNamedType;
function assertSchema(schema: unknown): GraphQLSchema;
function assertDirective(directive: unknown): GraphQLDirective;