Skip to content

Commit 6ef9152

Browse files
committed
Document how to transform request validation errors
Closes gh-487
1 parent 15b0492 commit 6ef9152

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

Diff for: spring-graphql-docs/src/docs/asciidoc/index.adoc

+47-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,40 @@ class HeaderInterceptor implements WebGraphQlInterceptor {
229229
<1> Controller adds value to the `GraphQLContext`
230230
<2> Interceptor uses the value to add an HTTP response header
231231

232-
You can add a `WebGraphQlInterceptor` through `WebGraphQlHandler` builder. The Boot starter
233-
supports this, see
232+
`WebGraphQlHandler` can modify the `ExecutionResult`, for example, to inspect and modify
233+
request validation errors that are raised before execution begins and which cannot be
234+
handled with a `DataFetcherExceptionResolver`:
235+
236+
[source,java,indent=0,subs="verbatim,quotes"]
237+
----
238+
static class RequestErrorInterceptor implements WebGraphQlInterceptor {
239+
240+
@Override
241+
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
242+
return chain.next(request).map(response -> {
243+
if (response.isValid()) {
244+
return response; <1>
245+
}
246+
247+
List<GraphQLError> errors = response.getErrors().stream() <2>
248+
.map(error -> {
249+
GraphqlErrorBuilder<?> builder = GraphqlErrorBuilder.newError();
250+
// ...
251+
return builder.build();
252+
})
253+
.collect(Collectors.toList());
254+
255+
return response.transform(builder -> builder.errors(errors).build()); <3>
256+
});
257+
}
258+
}
259+
----
260+
<1> Return the same if `ExecutionResult` has a "data" key with non-null value
261+
<2> Check and transform the GraphQL errors
262+
<3> Update the `ExecutionResult` with the modified errors
263+
264+
Use `WebGraphQlHandler` to configure the `WebGraphQlInterceptor` chain. This is supported
265+
by the Boot starter, see
234266
{spring-boot-ref-docs}/web.html#web.graphql.transports.http-websocket[Web Endpoints].
235267

236268

@@ -585,6 +617,19 @@ error details.
585617
Unresolved exception are logged at ERROR level along with the `executionId` to correlate
586618
to the error sent to the client. Resolved exceptions are logged at DEBUG level.
587619

620+
[[execution-exceptions-request]]
621+
==== Request Exceptions
622+
623+
The GraphQL Java engine may run into validation or other errors when parsing the request
624+
and that in turn prevent request execution. In such cases, the response contains a
625+
"data" key with `null` and one or more request-level "errors" that are global, i.e. not
626+
having a field path.
627+
628+
`DataFetcherExceptionResolver` cannot handle such global errors because they are raised
629+
before execution begins and before any `DataFetcher` is invoked. An application can use
630+
transport level interceptors to inspect and transform errors in the `ExecutionResult`.
631+
See examples under <<server-interception-web>>.
632+
588633

589634
[[execution-exceptions-subsctiption]]
590635
==== Subscription Exceptions

0 commit comments

Comments
 (0)