Error

GraphQL Errors

The graphql.error package is responsible for creating and formatting GraphQL errors.

class graphql.error.GraphQLError(message: str, nodes: Collection[Node] | Node | None = None, source: Source | None = None, positions: Collection[int] | None = None, path: Collection[str | int] | None = None, original_error: Exception | None = None, extensions: GraphQLErrorExtensions | None = None)

Bases: Exception

GraphQL Error

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

__init__(message: str, nodes: Collection[Node] | Node | None = None, source: Source | None = None, positions: Collection[int] | None = None, path: Collection[str | int] | None = None, original_error: Exception | None = None, extensions: GraphQLErrorExtensions | None = None) None

Initialize a GraphQLError.

args
extensions: GraphQLErrorExtensions | None

Extension fields to add to the formatted error

property formatted: GraphQLFormattedError

Get error formatted according to the specification.

Given a GraphQLError, format it according to the rules described by the “Response Format, Errors” section of the GraphQL Specification.

locations: list[SourceLocation] | None

Source locations

A list 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.

message: str

A message describing the Error for debugging purposes

nodes: list[Node] | None

A list of GraphQL AST Nodes corresponding to this error

original_error: Exception | None

The original error thrown from a field resolver during execution

path: list[str | int] | None

A list of field names and array indexes describing the JSON-path into the execution response which corresponds to this error.

Only included for errors during execution.

positions: Collection[int] | None

Error positions

A list of character offsets within the source GraphQL document which correspond to this error.

source: Source | None

The 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.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class graphql.error.GraphQLSyntaxError(source: Source, position: int, description: str)

Bases: GraphQLError

A GraphQLError representing a syntax error.

__init__(source: Source, position: int, description: str) None

Initialize the GraphQLSyntaxError

args
extensions: GraphQLErrorExtensions | None

Extension fields to add to the formatted error

property formatted: GraphQLFormattedError

Get error formatted according to the specification.

Given a GraphQLError, format it according to the rules described by the “Response Format, Errors” section of the GraphQL Specification.

locations: list[SourceLocation] | None

Source locations

A list 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.

message: str

A message describing the Error for debugging purposes

nodes: list[Node] | None

A list of GraphQL AST Nodes corresponding to this error

original_error: Exception | None

The original error thrown from a field resolver during execution

path: list[str | int] | None

A list of field names and array indexes describing the JSON-path into the execution response which corresponds to this error.

Only included for errors during execution.

positions: Collection[int] | None

Error positions

A list of character offsets within the source GraphQL document which correspond to this error.

source: Source | None

The 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.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class graphql.error.GraphQLFormattedError

Bases: TypedDict

Formatted GraphQL error

extensions: Dict[str, Any]
locations: list['graphql.language.FormattedSourceLocation']
message: str
path: list[str | int]
graphql.error.located_error(original_error: Exception, nodes: None | Collection[Node] = None, path: Collection[str | int] | None = None) GraphQLError

Located GraphQL Error

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