Error

GraphQL Errors

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

class graphql.error.GraphQLError(message: str, nodes: Optional[Union[Collection[Node], Node]] = None, source: Optional[Source] = None, positions: Optional[Collection[int]] = None, path: Optional[Collection[Union[str, int]]] = None, original_error: Optional[Exception] = None, extensions: Optional[Dict[str, Any]] = 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: Optional[Union[Collection[Node], Node]] = None, source: Optional[Source] = None, positions: Optional[Collection[int]] = None, path: Optional[Collection[Union[str, int]]] = None, original_error: Optional[Exception] = None, extensions: Optional[Dict[str, Any]] = None) None

Initialize a GraphQLError.

args
extensions: Optional[Dict[str, Any]]

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: Optional[List[SourceLocation]]

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: Optional[List[Node]]

A list of GraphQL AST Nodes corresponding to this error

original_error: Optional[Exception]

The original error thrown from a field resolver during execution

path: Optional[List[Union[str, int]]]

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: Optional[Collection[int]]

Error positions

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

source: Optional[Source]

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: Optional[GraphQLErrorExtensions]

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: Optional[List['SourceLocation']]

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: Optional[List['Node']]

A list of GraphQL AST Nodes corresponding to this error

original_error: Optional[Exception]

The original error thrown from a field resolver during execution

path: Optional[List[Union[str, int]]]

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: Optional[Collection[int]]

Error positions

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

source: Optional['Source']

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[FormattedSourceLocation]
message: str
path: List[Union[str, int]]
graphql.error.located_error(original_error: Exception, nodes: Union[None, Collection[Node]] = None, path: Optional[Collection[Union[str, int]]] = 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.