Skip to content

Commit 15b0492

Browse files
committed
Polishing docs on server interception
See gh-487
1 parent 2f598ed commit 15b0492

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

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

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,47 +155,59 @@ public class GraphQlRSocketController {
155155
[[server-interception]]
156156
=== Interception
157157

158-
Transport handlers for <<server-http>> and <<server-websocket>> delegate to a
159-
`WebGraphQlInterceptor` chain with an `ExecutionGraphQlService` at the end which calls
160-
the GraphQL Java engine. Use this to access HTTP request details and customize the
161-
`ExecutionInput` for GraphQL Java.
158+
Server transports allow intercepting requests before and after the GraphQL Java engine is
159+
called to process a request.
162160

163-
For example, to extract HTTP request values and pass them to data fetchers:
161+
162+
[[server-interception-web]]
163+
==== `WebGraphQlInterceptor`
164+
165+
<<server-http>> and <<server-websocket>> transports invoke a chain of
166+
0 or more `WebGraphQlInterceptor`, followed by an `ExecutionGraphQlService` that calls
167+
the GraphQL Java engine. `WebGraphQlInterceptor` allows an application to intercept
168+
incoming requests and do one of the following:
169+
170+
- Check HTTP request details
171+
- Customize the `graphql.ExecutionInput`
172+
- Add HTTP response headers
173+
- Customize the `graphql.ExecutionResult`
174+
175+
For example, an interceptor can pass an HTTP request header to a `DataFetcher`:
164176

165177
[source,java,indent=0,subs="verbatim,quotes"]
166178
----
167-
class HeaderInterceptor implements WebGraphQlInterceptor {
179+
class HeaderInterceptor implements WebGraphQlInterceptor { <1>
168180
169181
@Override
170182
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
171-
List<String> values = request.getHeaders().get("headerName");
183+
String value = request.getHeaders().getFirst("myHeader");
172184
request.configureExecutionInput((executionInput, builder) ->
173-
builder.graphQLContext(Collections.singletonMap("headerName", values)).build());
185+
builder.graphQLContext(Collections.singletonMap("myHeader", value)).build());
174186
return chain.next(request);
175187
}
176188
}
177189
178-
// Subsequent access from a controller
179-
180190
@Controller
181-
class MyController {
191+
class MyController { <2>
182192
183193
@QueryMapping
184194
Person person(@ContextValue String myHeader) {
185195
// ...
186196
}
187197
}
188198
----
199+
<1> Interceptor adds HTTP request header value into GraphQLContext
200+
<2> Data controller method accesses the value
189201

190-
Or reversely, add values to the `GraphQLContext` and use them to update the HTTP response:
202+
Reversely, an interceptor can access values added to the `GraphQLContext` by a controller:
191203

192204
[source,java,indent=0,subs="verbatim,quotes"]
193205
----
194206
@Controller
195207
class MyController {
196208
197209
@QueryMapping
198-
Person person(GraphQLContext context) {
210+
Person person(GraphQLContext context) { <1>
199211
context.put("cookieName", "123");
200212
}
201213
}
@@ -205,7 +217,7 @@ class MyController {
205217
class HeaderInterceptor implements WebGraphQlInterceptor {
206218
207219
@Override
208-
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
220+
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) { <2>
209221
return chain.next(request).doOnNext(response -> {
210222
String value = response.getExecutionInput().getGraphQLContext().get("cookieName");
211223
ResponseCookie cookie = ResponseCookie.from("cookieName", value).build();
@@ -214,13 +226,20 @@ class HeaderInterceptor implements WebGraphQlInterceptor {
214226
}
215227
}
216228
----
229+
<1> Controller adds value to the `GraphQLContext`
230+
<2> Interceptor uses the value to add an HTTP response header
217231

218-
The `WebGraphQlInterceptor` chain can be updated through the `WebGraphQlHandler` builder,
219-
and the Boot starter uses this, see Boot's section on
232+
You can add a `WebGraphQlInterceptor` through `WebGraphQlHandler` builder. The Boot starter
233+
supports this, see
220234
{spring-boot-ref-docs}/web.html#web.graphql.transports.http-websocket[Web Endpoints].
221235

222-
The <<server-rsocket>> transport handler delegates to a similar `GraphQlInterceptor`
223-
chain that you can use to intercept GraphQL over RSocket requests.
236+
237+
[[server-interception-rsocket]]
238+
==== `RSocketQlInterceptor`
239+
240+
Similar to <<server-interception-web>>, an `RSocketQlInterceptor` allows intercepting
241+
GraphQL over RSocket requests before and after GraphQL Java engine execution. You can use
242+
this to customize the `graphql.ExecutionInput` and the `graphql.ExecutionResult`.
224243

225244

226245

0 commit comments

Comments
 (0)