@@ -153,31 +153,49 @@ public class GraphQlRSocketController {
153
153
154
154
155
155
[[server-interception]]
156
- === Server Interception
156
+ === Interception
157
157
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.
161
162
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`:
164
167
165
168
[source,java,indent=0,subs="verbatim,quotes"]
166
169
----
167
- class MyInterceptor implements WebGraphQlInterceptor {
170
+ class HeaderInterceptor implements WebGraphQlInterceptor {
168
171
169
172
@Override
170
173
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());
175
177
return chain.next(request);
176
178
}
177
179
}
178
180
----
179
181
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
181
199
`graphql.ExecutionResult` from GraphQL Java:
182
200
183
201
[source,java,indent=0,subs="verbatim,quotes"]
@@ -200,9 +218,8 @@ class MyInterceptor implements WebGraphQlInterceptor {
200
218
starter uses this, see Boot's section on
201
219
{spring-boot-ref-docs}/web.html#web.graphql.web-endpoints[Web Endpoints].
202
220
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.
206
223
207
224
208
225
0 commit comments