@@ -154,67 +154,68 @@ public class GraphQlRSocketController {
154
154
[[server-interception]]
155
155
=== Interception
156
156
157
- Spring MVC and Spring WebFlux transport handlers, for both <<server-http>> and
158
- <<server-websocket>>, all delegate to the same `WebGraphQlInterceptor` chain, followed by
159
- the `ExecutionGraphQlService` that invokes the GraphQL Java engine. You can use this to
160
- intercept GraphQL requests over any Web transport .
157
+ Transport handlers for <<server-http>> and <<server-websocket>> delegate to a
158
+ `WebGraphQlInterceptor` chain with an `ExecutionGraphQlService` at the end which calls
159
+ the GraphQL Java engine. Use this to access HTTP request details and customize the
160
+ `ExecutionInput` for GraphQL Java .
161
161
162
- A `WebGraphQlInterceptor` exposes the details of the underlying transport (HTTP or
163
- WebSocket handshake) request and allows customizing the `graphql.ExecutionInput`
164
- that is prepared for GraphQL Java. For example, to extract an HTTP header and make it
165
- available to data fetchers through the `GraphQLContext`:
162
+ For example, to extract HTTP request values and pass them to data fetchers:
166
163
167
164
[source,java,indent=0,subs="verbatim,quotes"]
168
165
----
169
166
class HeaderInterceptor implements WebGraphQlInterceptor {
170
167
171
168
@Override
172
169
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
173
- List<String> headerValue = request.getHeaders().get("myHeader ");
170
+ List<String> values = request.getHeaders().get("headerName ");
174
171
request.configureExecutionInput((executionInput, builder) ->
175
- builder.graphQLContext(Collections.singletonMap("myHeader ", headerValue )).build());
172
+ builder.graphQLContext(Collections.singletonMap("headerName ", values )).build());
176
173
return chain.next(request);
177
174
}
178
175
}
179
- ----
180
176
181
- A `DataFetcher` can then access this value, e.g. from an
182
- <<controllers,annotated controller>> method:
177
+ // Subsequent access from a controller
183
178
184
- [source,java,indent=0,subs="verbatim,quotes"]
185
- ----
186
179
@Controller
187
180
class MyController {
188
181
189
182
@QueryMapping
190
183
Person person(@ContextValue String myHeader) {
191
- // ...
184
+ // ...
192
185
}
193
186
}
194
187
----
195
188
196
-
197
- Interceptors can also customize HTTP response headers, or inspect and/or transform the
198
- `graphql.ExecutionResult` from GraphQL Java:
189
+ Or reversely, add values to the `GraphQLContext` and use them to update the HTTP response:
199
190
200
191
[source,java,indent=0,subs="verbatim,quotes"]
201
192
----
202
- class MyInterceptor implements WebGraphQlInterceptor {
193
+ @Controller
194
+ class MyController {
195
+
196
+ @QueryMapping
197
+ Person person(GraphQLContext context) {
198
+ context.put("cookieName", "123");
199
+ }
200
+ }
201
+
202
+ // Subsequent access from a WebGraphQlInterceptor
203
+
204
+ class HeaderInterceptor implements WebGraphQlInterceptor {
203
205
204
206
@Override
205
207
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
206
- return chain.next(request)
207
- .map(response -> {
208
- Object data = response.getData();
209
- Object updatedData = ... ;
210
- return response.transform(builder -> builder.data(updatedData));
211
- });
208
+ return chain.next(request).doOnNext(response -> {
209
+ String value = response.getExecutionInput().getGraphQLContext().get("cookieName");
210
+ ResponseCookie cookie = ResponseCookie.from("cookieName", value).build();
211
+ response.getResponseHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString());
212
+ });
212
213
}
213
214
}
214
215
----
215
216
216
- `WebGraphQlHandler` has a builder to create the `WebGraphQlInterceptor` chain. The Boot
217
- starter uses this, see Boot's section on
217
+ The `WebGraphQlInterceptor` chain can be updated through the `WebGraphQlHandler` builder,
218
+ and the Boot starter uses this, see Boot's section on
218
219
{spring-boot-ref-docs}/web.html#web.graphql.web-endpoints[Web Endpoints].
219
220
220
221
The <<server-rsocket>> transport handler delegates to a similar `GraphQlInterceptor`
0 commit comments