🎉 Explore GraphQLConf 2026 • May 19-21 • Fremont, CA • View the schedule
v17 APIgraphql/error

Create, format, and locate GraphQL errors.

These exports are also available from the root graphql package.

Classes

GraphQLError

A GraphQLError describes an Error found during the parse, validate, or execute phases of performing a GraphQL operation. In addition to a message and stack trace, it also includes information about the locations in a GraphQL document and/or execution result that correspond to the Error.


Constructor

Creates a GraphQLError instance.

Signature:

new GraphQLError(
  message: string,
  options: GraphQLErrorOptions = {},
);

Arguments
NameTypeDefaultDescription
messagestringHuman-readable error message.
optionsGraphQLErrorOptions{}Error metadata such as source locations, response path, original error, and extensions.

Members

NameTypeDescription
locationsreadonly SourceLocation[] | undefinedAn array of { line, column } locations within the source GraphQL document
which correspond to this error.
Errors during validation often contain multiple locations, for example to
point out two things with the same name. Errors during execution include a
single location, the field which produced the error.
Enumerable, and appears in the result of JSON.stringify().
pathreadonly (string | number)[] | undefinedAn array describing the JSON-path into the execution response which
corresponds to this error. Only included for errors during execution.
Enumerable, and appears in the result of JSON.stringify().
nodesreadonly ASTNode[] | undefinedAn array of GraphQL AST Nodes corresponding to this error.
sourceSource | undefinedThe source GraphQL document for the first location of this error.
Note that if this Error represents more than one node, the source may not
represent nodes after the first node.
positionsreadonly number[] | undefinedAn array of character offsets within the source GraphQL document
which correspond to this error.
originalErrorError | undefinedOriginal error that caused this GraphQLError, if one exists.
extensionsGraphQLErrorExtensionsExtension fields to add to the formatted error.

toString()

Returns this error as a human-readable message with source locations.

Signature:

toString(): string;

Returns
TypeDescription
stringThe formatted error string.

Example
import { Source } from 'graphql/language';
import { GraphQLError } from 'graphql/error';
 
const error = new GraphQLError('Cannot query field "name".', {
  source: new Source('{ name }'),
  positions: [2],
});
 
error.toString(); // => 'Cannot query field "name".\n\nGraphQL request:1:3\n1 | { name }\n  |   ^'

toJSON()

Returns the JSON representation used when this object is serialized.

Signature:

toJSON(): GraphQLFormattedError;

Returns
TypeDescription
GraphQLFormattedErrorThe JSON-serializable representation.

Example
import { GraphQLError } from 'graphql/error';
 
const error = new GraphQLError('Resolver failed.', {
  path: ['viewer', 'name'],
  extensions: { code: 'INTERNAL' },
});
 
error.toJSON(); // => { message: 'Resolver failed.', path: ['viewer', 'name'], extensions: { code: 'INTERNAL' } }

Functions

locatedError()

Given an arbitrary value, presumably thrown while attempting to execute a GraphQL operation, produce a new GraphQLError aware of the location in the document responsible for the original Error.

Signature:

locatedError(
  rawOriginalError: unknown,
  nodes:
    | ASTNode
    | readonly ASTNode[]
    | null
    | undefined,
  path?: Maybe<readonly (string | number)[]>,
): GraphQLError;

Arguments
NameTypeDescription
rawOriginalErrorunknownThe original error value to wrap.
nodes| ASTNode | readonly ASTNode[] | null | undefinedThe AST nodes associated with the error.
path?Maybe<readonly (string | number)[]>The response path associated with the error.

Returns
TypeDescription
GraphQLErrorThe GraphQL error.

Example
import { parse } from 'graphql/language';
import { locatedError } from 'graphql/error';
 
const document = parse('{ viewer { name } }');
const fieldNode = document.definitions[0].selectionSet.selections[0];
const error = locatedError(new Error('Resolver failed'), fieldNode, [
  'viewer',
]);
 
error.message; // => 'Resolver failed'
error.locations; // => [{ line: 1, column: 3 }]
error.path; // => ['viewer']

syntaxError()

Produces a GraphQLError representing a syntax error, containing useful descriptive information about the syntax error’s position in the source.

Signature:

syntaxError(
  source: Source,
  position: number,
  description: string,
): GraphQLError;

Arguments
NameTypeDescription
sourceSourceThe GraphQL source containing the syntax error.
positionnumberCharacter offset where the syntax error was encountered.
descriptionstringHuman-readable description of the syntax error.

Returns
TypeDescription
GraphQLErrorA GraphQLError located at the syntax error position.

Example
import { Source } from 'graphql/language';
import { syntaxError } from 'graphql/error';
 
const error = syntaxError(new Source('query {'), 7, 'Expected Name');
 
error.message; // => 'Syntax Error: Expected Name'
error.locations; // => [{ line: 1, column: 8 }]

Types

GraphQLErrorExtensions

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.


GraphQLFormattedErrorExtensions

Interface. Custom formatted 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.


GraphQLErrorOptions

Interface. Options used to construct a GraphQLError.


Members
NameTypeDescription
nodes?ASTNode | readonly ASTNode[] | nullAST node or nodes associated with this error.
source?Maybe<Source>Source document used to derive error locations.
positions?Maybe<readonly number[]>Character offsets in the source document associated with this error.
path?Maybe<readonly (string | number)[]>Response path where this error occurred during execution.
originalError?Maybe< Error & { extensions?: unknown } >Original error that caused this GraphQLError, if one exists.
extensions?Maybe<GraphQLErrorExtensions>Extension fields to include in the formatted result.

GraphQLFormattedError

Interface. See: https://spec.graphql.org/draft/#sec-Errors


Members
NameTypeDescription
messagestringA short, human-readable summary of the problem that SHOULD NOT change
from occurrence to occurrence of the problem, except for purposes of
localization.
locations?readonly SourceLocation[]If an error can be associated to a particular point in the requested
GraphQL document, it should contain a list of locations.
path?readonly (string | number)[]If an error can be associated to a particular field in the GraphQL result,
it must contain an entry with the key path that details the path of
the response field which experienced the error. This allows clients to
identify whether a null result is intentional or caused by a runtime error.
extensions?GraphQLFormattedErrorExtensionsReserved for implementors to extend the protocol however they see fit,
and hence there are no additional restrictions on its contents.