GraphQLError is the interface graphql-java uses to describe one error in the errors array of a GraphQL response. It is not a Spring class: Spring for GraphQL builds on graphql-java and lets you turn Java exceptions into GraphQLError objects so clients get a stable message, error type and optional extensions.
You normally do not implement the interface yourself. You build errors with GraphqlErrorBuilder and map exceptions to them with @GraphQlExceptionHandler or a DataFetcherExceptionResolver.
The interface is graphql.GraphQLError from graphql-java. The GraphQL specification defines the shape of an error, and the interface exposes it:
| Field | Meaning |
|---|---|
message | Human-readable description of what went wrong |
locations | Line and column in the query document |
path | Field path in the result where the error occurred |
extensions | Optional map for custom data such as error codes |
errorType | A graphql-java ErrorClassification, sent as extensions.classification |
A typical response with an error looks like this:
Unlike REST, a GraphQL request that has an error still returns HTTP 200 in most cases. Clients must read the errors array, and partial data may be present next to it.
If a data fetcher throws an exception that nothing resolves, Spring for GraphQL returns a generic error with the classification INTERNAL_ERROR and a message that contains only an execution ID, such as INTERNAL_ERROR for 7a1c.... The real message and stack trace stay in the server log. This is deliberate: exception messages can leak table names, file paths or other internals.
To return something meaningful, you map the exception yourself.
Create errors with the builder instead of writing a class that implements the interface. Passing the DataFetchingEnvironment fills the path and location automatically.
ErrorType is Spring's enum with BAD_REQUEST, UNAUTHORIZED, FORBIDDEN, NOT_FOUND and INTERNAL_ERROR. It implements graphql-java's ErrorClassification, so it can be passed to errorType.
The simplest way is an exception handler method in the controller, or in a @ControllerAdvice class for all controllers.
@GraphQlExceptionHandler methods can return a single GraphQLError, a collection of errors, or a Mono of either in reactive code. Throw BookNotFoundException from a @QueryMapping method and the client receives the structured error shown earlier.
For cross-cutting rules, extend DataFetcherExceptionResolverAdapter and register it as a bean:
Returning null means the resolver did not handle the exception, so the next resolver gets a chance.
Syntax errors and queries that do not match the schema are produced by graphql-java before any controller runs, and they appear as ValidationError or InvalidSyntaxError entries. Bean Validation failures on @Argument input throw ConstraintViolationException, which you can map to ErrorType.BAD_REQUEST with a resolver in the same way.
extensions so clients do not parse messages.ErrorType consistently: BAD_REQUEST for input, NOT_FOUND for missing data, FORBIDDEN for authorization.GraphQlTester so the response shape is part of your contract.No. It is graphql.GraphQLError from graphql-java. Spring for GraphQL provides ErrorType and resolvers that produce it.
Rarely. GraphqlErrorBuilder covers the normal cases, and a custom implementation is only needed for unusual serialization.
Unresolved exceptions are masked to avoid leaking details. Add a @GraphQlExceptionHandler or an exception resolver for the exception type.
Usually not. The response is HTTP 200 with an errors array. Transport-level failures, such as invalid JSON, can return other status codes.
GraphQLError is graphql-java's representation of an error, with message, locations, path and extensions. In Spring Boot, build it with GraphqlErrorBuilder and ErrorType, and map your exceptions through @GraphQlExceptionHandler or a DataFetcherExceptionResolver so clients get clear, safe and stable errors.