Skip to content

Commit 65cd5bf

Browse files
committed
Update docs on web interception
Closes gh-428
1 parent 0951f87 commit 65cd5bf

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -154,67 +154,68 @@ public class GraphQlRSocketController {
154154
[[server-interception]]
155155
=== Interception
156156

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.
161161

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:
166163

167164
[source,java,indent=0,subs="verbatim,quotes"]
168165
----
169166
class HeaderInterceptor implements WebGraphQlInterceptor {
170167
171168
@Override
172169
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
173-
List<String> headerValue = request.getHeaders().get("myHeader");
170+
List<String> values = request.getHeaders().get("headerName");
174171
request.configureExecutionInput((executionInput, builder) ->
175-
builder.graphQLContext(Collections.singletonMap("myHeader", headerValue)).build());
172+
builder.graphQLContext(Collections.singletonMap("headerName", values)).build());
176173
return chain.next(request);
177174
}
178175
}
179-
----
180176
181-
A `DataFetcher` can then access this value, e.g. from an
182-
<<controllers,annotated controller>> method:
177+
// Subsequent access from a controller
183178
184-
[source,java,indent=0,subs="verbatim,quotes"]
185-
----
186179
@Controller
187180
class MyController {
188181
189182
@QueryMapping
190183
Person person(@ContextValue String myHeader) {
191-
// ...
184+
// ...
192185
}
193186
}
194187
----
195188

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:
199190

200191
[source,java,indent=0,subs="verbatim,quotes"]
201192
----
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 {
203205
204206
@Override
205207
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+
});
212213
}
213214
}
214215
----
215216

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
218219
{spring-boot-ref-docs}/web.html#web.graphql.web-endpoints[Web Endpoints].
219220

220221
The <<server-rsocket>> transport handler delegates to a similar `GraphQlInterceptor`

0 commit comments

Comments
 (0)