Skip to content

Commit 681a0dc

Browse files
committed
Update documentation for RSocket
See gh-339
1 parent 5b93c1f commit 681a0dc

File tree

2 files changed

+109
-52
lines changed

2 files changed

+109
-52
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ include::attributes.adoc[]
66
[[client]]
77
= Client
88

9-
Spring for GraphQL includes client support for executing GraphQL requests over HTTP or
10-
over WebSocket.
9+
Spring for GraphQL includes client support for executing GraphQL requests over HTTP,
10+
WebSocket, and RSocket.
1111

1212

1313

@@ -108,6 +108,26 @@ on an existing `WebSocketGraphQlClient` to create another with different configu
108108

109109

110110

111+
[[client-rsocketgraphqlclient]]
112+
=== RSocket
113+
114+
`RSocketGraphQlClient` uses
115+
{spring-framework-ref-docs}/web-reactive.html#rsocket-requester[RSocketRequester]
116+
to execute GraphQL requests over RSocket requests.
117+
118+
[source,java,indent=0,subs="verbatim,quotes"]
119+
----
120+
RSocketGraphQlClient graphQlClient =
121+
RSocketGraphQlClient.builder()
122+
.websocket(URI.create("http://localhost:8080/graphql"))
123+
.build();
124+
----
125+
126+
Once created, `RSocketGraphQlClient` exposes the same transport agnostic workflow for
127+
request execution as any `GrahQlClient`.
128+
129+
130+
111131
[[client-websocketgraphqlclient-connection]]
112132
==== Connection
113133

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

Lines changed: 87 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ Spring for GraphQL requires the following as a baseline:
3535

3636

3737

38-
[[web-transports]]
39-
== Web Transports
38+
[[server-transports]]
39+
== Server Transports
4040

41-
Spring for GraphQL supports GraphQL requests over HTTP and over WebSocket.
41+
Spring for GraphQL supports server handling of GraphQL requests over HTTP, WebSocket, and
42+
RSocket.
4243

4344

44-
45-
[[web-http]]
45+
[[server-http]]
4646
=== HTTP
4747

4848
`GraphQlHttpHandler` handles GraphQL over HTTP requests and delegates to the
49-
<<web-interception>> chain for request execution. There are two variants, one for
49+
<<server-interception>> chain for request execution. There are two variants, one for
5050
Spring MVC and one for Spring WebFlux. Both handle requests asynchronously and have
5151
equivalent functionality, but rely on blocking vs non-blocking I/O respectively for
5252
writing the HTTP response.
@@ -70,15 +70,15 @@ The Spring for GraphQL repository contains a Spring MVC
7070

7171

7272

73-
[[web-websocket]]
73+
[[server-websocket]]
7474
=== WebSocket
7575

7676
`GraphQlWebSocketHandler` handles GraphQL over WebSocket requests based on the
7777
https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md[protocol] defined in the
7878
https://github.com/enisdenjo/graphql-ws[graphql-ws] library. The main reason to use
7979
GraphQL over WebSocket is subscriptions which allow sending a stream of GraphQL
8080
responses, but it can also be used for regular queries with a single response.
81-
The handler delegates every request to the <<web-interception>> chain for further
81+
The handler delegates every request to the <<server-interception>> chain for further
8282
request execution.
8383

8484
[TIP]
@@ -113,17 +113,52 @@ The Spring for GraphQL repository contains a WebFlux
113113

114114

115115

116-
[[web-interception]]
117-
=== Web Interception
116+
[[server-rsocket]]
117+
=== RSocket
118118

119-
<<web-http>> and <<web-websocket>> transport handlers delegate to a common Web
120-
interception chain for request execution. The chain consists of a sequence of
121-
`WebGraphQlInterceptor` components, followed by a `ExecutionGraphQlService` that
122-
invokes GraphQL Java.
119+
`GraphQlRSocketHandler` handles GraphQL over RSocket requests. Queries and mutations are
120+
expected and handled as an RSocket `request-response` interaction while subscriptions are
121+
handled as `request-stream`.
123122

124-
`WebGraphQlInterceptor` is as a common contract to use in both Spring MVC and
125-
WebFlux applications. Use it to intercept requests, inspect HTTP request headers, or to
126-
register a transformation of the `graphql.ExecutionInput`:
123+
`GraphQlRSocketHandler` can be used a delegate from an `@Controller` that is mapped to
124+
the route for GraphQL requests. For example:
125+
126+
[source,java,indent=0,subs="verbatim,quotes"]
127+
----
128+
@Controller
129+
public class GraphQlRSocketController {
130+
131+
private final GraphQlRSocketHandler handler;
132+
133+
GraphQlRSocketController(GraphQlRSocketHandler handler) {
134+
this.handler = handler;
135+
}
136+
137+
@MessageMapping("graphql")
138+
public Mono<Map<String, Object>> handle(Map<String, Object> payload) {
139+
return this.handler.handle(payload);
140+
}
141+
142+
@MessageMapping("graphql")
143+
public Flux<Map<String, Object>> handleSubscription(Map<String, Object> payload) {
144+
return this.handler.handleSubscription(payload);
145+
}
146+
}
147+
----
148+
149+
150+
151+
152+
153+
[[server-interception]]
154+
=== Server Interception
155+
156+
GraphQL <<server-http>> and <<server-websocket>> handlers for Spring MVC and WebFlux
157+
delegate to a common `WebGraphQlInterceptor` chain followed by an `ExecutionGraphQlService`
158+
that invokes the GraphQL Java engine.
159+
160+
You can write an interceptor to check requests details or transform the
161+
`graphql.ExecutionInput` for GraphQL Java:
127162

128163
[source,java,indent=0,subs="verbatim,quotes"]
129164
----
@@ -140,8 +175,8 @@ class MyInterceptor implements WebGraphQlInterceptor {
140175
}
141176
----
142177

143-
Use `WebGraphQlInterceptor` also to intercept responses, add HTTP response headers,
144-
or transform the `graphql.ExecutionResult`:
178+
Interceptors can customize HTTP response headers, or inspect and/or transform the
179+
`graphql.ExecutionResult` from GraphQL Java:
145180

146181
[source,java,indent=0,subs="verbatim,quotes"]
147182
----
@@ -159,19 +194,22 @@ class MyInterceptor implements WebGraphQlInterceptor {
159194
}
160195
----
161196

162-
`WebGraphQlHandler` provides a builder to initialize the Web interception chain. After
163-
you build the chain, you can use the resulting `WebGraphQlHandler` to initialize the HTTP
164-
or WebSocket transport handlers. The Boot starter configures all this, see the
165-
{spring-boot-ref-docs}/web.html#web.graphql.web-endpoints[Web Endpoints] section for
166-
details, or check `GraphQlWebMvcAutoConfiguration` or `GraphQlWebFluxAutoConfiguration`
167-
it contains, for the actual config.
197+
`WebGraphQlHandler` has a builder to create the `WebGraphInterceptor` chain. The Boot
198+
starter uses this, see Boot's section on
199+
{spring-boot-ref-docs}/web.html#web.graphql.web-endpoints[Web Endpoints].
200+
201+
The <<server-rsocket>> handler delegates to a similar chain except
202+
the interceptor type is `GraphQlInterceptor`. To use, create `GraphQlRSocketHandler` with
203+
the list of interceptors to apply to requests.
204+
205+
168206

169207

170208
[[execution]]
171209
== Request Execution
172210

173211
`ExecutionGraphQlService` is the main Spring abstraction to call GraphQL Java to execute
174-
requests. Underlying transports, such as the <<web-transports>>, delegate to
212+
requests. Underlying transports, such as the <<server-transports>>, delegate to
175213
`ExecutionGraphQlService` to handle requests.
176214

177215
The main implementation, `DefaultExecutionGraphQlService`, is configured with a
@@ -212,10 +250,9 @@ class GraphQlConfig {
212250
213251
@Bean
214252
public GraphQlSourceBuilderCustomizer sourceBuilderCustomizer() {
215-
return (builder) -> {
216-
builder.configureGraphQl(graphQlBuilder ->
217-
graphQlBuilder.executionIdProvider(new CustomExecutionIdProvider()));
218-
};
253+
return (builder) ->
254+
builder.configureGraphQl(graphQlBuilder ->
255+
graphQlBuilder.executionIdProvider(new CustomExecutionIdProvider()));
219256
}
220257
}
221258
----
@@ -380,10 +417,10 @@ such beans, so you might have something like:
380417
@Configuration
381418
public class GraphQlConfig {
382419
383-
@Bean
384-
public RuntimeWiringConfigurer runtimeWiringConfigurer() {
385-
return builder -> builder.directiveWiring(new MySchemaDirectiveWiring());
386-
}
420+
@Bean
421+
public RuntimeWiringConfigurer runtimeWiringConfigurer() {
422+
return builder -> builder.directiveWiring(new MySchemaDirectiveWiring());
423+
}
387424
388425
}
389426
----
@@ -413,7 +450,7 @@ transport layer, such as from a WebFlux request handling, see
413450
=== Context Propagation
414451

415452
Spring for GraphQL provides support to transparently propagate context from the
416-
<<web-transports>>, through GraphQL Java, and to `DataFetcher` and other components it
453+
<<server-transports>>, through GraphQL Java, and to `DataFetcher` and other components it
417454
invokes. This includes both `ThreadLocal` context from the Spring MVC request handling
418455
thread and Reactor `Context` from the WebFlux processing pipeline.
419456

@@ -423,7 +460,7 @@ thread and Reactor `Context` from the WebFlux processing pipeline.
423460

424461
A `DataFetcher` and other components invoked by GraphQL Java may not always execute on
425462
the same thread as the Spring MVC handler, for example if an asynchronous
426-
<<web-interception, `WebGraphQlInterceptor`>> or `DataFetcher` switches to a
463+
<<server-interception, `WebGraphQlInterceptor`>> or `DataFetcher` switches to a
427464
different thread.
428465

429466
Spring for GraphQL supports propagating `ThreadLocal` values from the Servlet container
@@ -457,7 +494,7 @@ public class RequestAttributesAccessor implements ThreadLocalAccessor {
457494
}
458495
----
459496

460-
A `ThreadLocalAccessor` can be registered in the <<web-interception,WebGraphHandler>>
497+
A `ThreadLocalAccessor` can be registered in the <<server-interception,WebGraphHandler>>
461498
builder. The Boot starter detects beans of this type and automatically registers them for
462499
Spring MVC application, see the
463500
{spring-boot-ref-docs}/web.html#web.graphql.web-endpoints[Web Endpoints] section.
@@ -468,7 +505,7 @@ Spring MVC application, see the
468505

469506
A <<execution-reactive-datafetcher>> can rely on access to Reactor context that
470507
originates from the WebFlux request handling chain. This includes Reactor context
471-
added by <<web-interception, WebGraphQlInterceptor>> components.
508+
added by <<server-interception, WebGraphQlInterceptor>> components.
472509

473510

474511

@@ -546,11 +583,11 @@ public class MyConfig {
546583
public MyConfig(BatchLoaderRegistry registry) {
547584
548585
registry.forTypePair(Long.class, Author.class).registerMappedBatchLoader((authorIds, env) -> {
549-
// return Mono<Map<Long, Author>
550-
});
586+
// return Mono<Map<Long, Author>
587+
});
551588
552-
// more registrations ...
553-
}
589+
// more registrations ...
590+
}
554591
555592
}
556593
----
@@ -679,12 +716,12 @@ dependencies {
679716
//...
680717
681718
annotationProcessor "com.querydsl:querydsl-apt:$querydslVersion:jpa",
682-
'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final',
683-
'javax.annotation:javax.annotation-api:1.3.2'
719+
'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final',
720+
'javax.annotation:javax.annotation-api:1.3.2'
684721
}
685722
686723
compileJava {
687-
options.annotationProcessorPath = configurations.annotationProcessor
724+
options.annotationProcessorPath = configurations.annotationProcessor
688725
}
689726
----
690727
[source,xml,indent=0,subs="verbatim,quotes,attributes",role="secondary"]
@@ -1238,12 +1275,12 @@ Bean Validation lets you declare constraints on types, as the following example
12381275
----
12391276
public class BookInput {
12401277
1241-
@NotNull
1242-
private String title;
1278+
@NotNull
1279+
private String title;
12431280
12441281
@NotNull
1245-
@Size(max=13)
1246-
private String isbn;
1282+
@Size(max=13)
1283+
private String isbn;
12471284
}
12481285
----
12491286

@@ -1502,7 +1539,7 @@ Batch mapping methods can return:
15021539
[[security]]
15031540
== Security
15041541

1505-
The path to a <<web-transports, Web>> GraphQL endpoint can be secured with HTTP
1542+
The path to a <<server-transports, Web>> GraphQL endpoint can be secured with HTTP
15061543
URL security to ensure that only authenticated users can access it. This does not,
15071544
however, differentiate among different GraphQL requests on such a shared endpoint on
15081545
a single URL.

0 commit comments

Comments
 (0)