GraphQLError is the interface that represents one error in a GraphQL response. It comes from graphql-java (package graphql), the engine underneath Spring for GraphQL, not from Spring itself. Spring for GraphQL turns exceptions thrown by your data fetchers into GraphQLError objects so clients receive a spec-compliant errors array with a message, a location, a path and optional extensions.
You rarely implement the interface yourself. In practice you build errors with GraphqlErrorBuilder and map exceptions to them with @GraphQlExceptionHandler or a DataFetcherExceptionResolver.
The GraphQL specification says a response may contain an errors list alongside data. Each entry can include these fields, all exposed by GraphQLError:
| Field | GraphQLError method | Meaning |
|---|---|---|
message | getMessage() | Human-readable description |
locations | getLocations() | Line and column in the query document |
path | getPath() | Path to the field that failed, such as bookById |
extensions | getExtensions() | Free-form map for codes and extra context |
| classification | getErrorType() | Category of the error, shown under extensions.classification |
Unlike REST, a GraphQL request that partially fails still returns HTTP 200 with whatever data could be resolved plus the errors list.
When a data fetcher throws, Spring passes the exception through its DataFetcherExceptionResolver chain. If no resolver handles it, Spring returns a generic error of type INTERNAL_ERROR with a message that contains only the execution id, so internal details are not leaked to clients. Your job is to translate expected exceptions (not found, forbidden, validation) into meaningful errors.
Spring defines its own ErrorType enum (org.springframework.graphql.execution.ErrorType) with the values BAD_REQUEST, UNAUTHORIZED, FORBIDDEN, NOT_FOUND and INTERNAL_ERROR. It implements graphql-java's ErrorClassification, so you can pass it to the error builder.
Since Spring for GraphQL 1.2, you can handle exceptions directly in the controller (or in a @ControllerAdvice class for global handling).
GraphqlErrorBuilder.newError(env) copies the path and location from the failing field, which is why you pass the DataFetchingEnvironment. The handler method may return a single GraphQLError, a Collection<GraphQLError>, or a Mono/Flux of them in reactive applications.
For cross-cutting rules, register a bean that extends DataFetcherExceptionResolverAdapter. Spring Boot picks up every such bean automatically.
Returning null tells Spring this resolver does not handle the exception, so the next one runs. Never return stack traces or SQL messages in the error message.
If you need a reusable error type, implement the interface. Mark exceptions that carry their own data as GraphQLError too, or build a plain class.
For most applications the builder is simpler and less error-prone than a hand-written class, so prefer GraphqlErrorBuilder.
extensions and keep messages safe for end users.ErrorType and let everything else fall through to INTERNAL_ERROR.data plus errors for partial failures instead of failing a whole query when only one field is broken.GraphQlTester and assert on .errors().GraphQLError a Spring class?No. It is an interface from graphql-java. Spring for GraphQL builds on it and adds the ErrorType enum and the exception-handling infrastructure.
INTERNAL_ERROR with only an id?An unhandled exception was thrown and no resolver mapped it. Spring hides the details on purpose. Add a @GraphQlExceptionHandler or resolver to return a meaningful error.
Not for field-level errors. GraphQL reports them in the response body with HTTP 200. Transport failures, such as a malformed request, can still produce 4xx codes.
Yes. Return a List<GraphQLError> from the handler, or override resolveToMultipleErrors in the adapter.
GraphQLError is graphql-java's representation of a single error: message, locations, path, classification and extensions. In Spring for GraphQL you create it with GraphqlErrorBuilder, using ErrorType for the category, and you connect it to exceptions through @GraphQlExceptionHandler or a DataFetcherExceptionResolverAdapter so clients get precise, safe error responses.