Skip to content

Commit b8c4b25

Browse files
committed
Improve Server Interception section
See gh-350
1 parent 7044844 commit b8c4b25

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

spring-graphql-docs/src/docs/asciidoc/index.adoc

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,31 +153,49 @@ public class GraphQlRSocketController {
153153

154154

155155
[[server-interception]]
156-
=== Server Interception
156+
=== Interception
157157

158-
GraphQL <<server-http>> and <<server-websocket>> handlers for Spring MVC and WebFlux
159-
delegate to a common `WebGraphQlInterceptor` chain followed by an `ExecutionGraphQlService`
160-
that invokes the GraphQL Java engine.
158+
Spring MVC and Spring WebFlux transport handlers, for both <<server-http>> and
159+
<<server-websocket>>, all delegate to the same `WebGraphQlInterceptor` chain, followed by
160+
the `ExecutionGraphQlService` that invokes the GraphQL Java engine. You can use this to
161+
intercept GraphQL requests over any Web transport.
161162

162-
You can write an interceptor to check requests details or transform the
163-
`graphql.ExecutionInput` for GraphQL Java:
163+
A `WebGraphQlInterceptor` exposes the details of the underlying transport (HTTP or
164+
WebSocket handshake) request and allows customizing the `graphql.ExecutionInput`
165+
that is prepared for GraphQL Java. For example, to extract an HTTP header and make it
166+
available to data fetchers through the `GraphQLContext`:
164167

165168
[source,java,indent=0,subs="verbatim,quotes"]
166169
----
167-
class MyInterceptor implements WebGraphQlInterceptor {
170+
class HeaderInterceptor implements WebGraphQlInterceptor {
168171
169172
@Override
170173
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
171-
request.configureExecutionInput((executionInput, builder) -> {
172-
Map<String, Object> map = ... ;
173-
return builder.extensions(map).build();
174-
});
174+
List<String> headerValue = request.getHeaders().get("myHeader");
175+
request.configureExecutionInput((executionInput, builder) ->
176+
builder.graphQLContext(Collections.singletonMap("myHeader", headerValue)).build());
175177
return chain.next(request);
176178
}
177179
}
178180
----
179181

180-
Interceptors can customize HTTP response headers, or inspect and/or transform the
182+
A `DataFetcher` can then access this value, e.g. from an
183+
<<controllers,annotated controller>> method:
184+
185+
[source,java,indent=0,subs="verbatim,quotes"]
186+
----
187+
@Controller
188+
class MyController {
189+
190+
@QueryMapping
191+
Person person(@ContextValue String myHeader) {
192+
// ...
193+
}
194+
}
195+
----
196+
197+
198+
Interceptors can also customize HTTP response headers, or inspect and/or transform the
181199
`graphql.ExecutionResult` from GraphQL Java:
182200

183201
[source,java,indent=0,subs="verbatim,quotes"]
@@ -200,9 +218,8 @@ class MyInterceptor implements WebGraphQlInterceptor {
200218
starter uses this, see Boot's section on
201219
{spring-boot-ref-docs}/web.html#web.graphql.web-endpoints[Web Endpoints].
202220

203-
The <<server-rsocket>> handler delegates to a similar chain except
204-
the interceptor type is `GraphQlInterceptor`. To use, create `GraphQlRSocketHandler` with
205-
the list of interceptors to apply to requests.
221+
The <<server-rsocket>> transport handler delegates to a similar `GraphQlInterceptor`
222+
chain that you can use to intercept GraphQL over RSocket requests.
206223

207224

208225

0 commit comments

Comments
 (0)