Skip to content

Commit 67e0599

Browse files
authored
Add documentation for GraphQL support (#3756)
* Add documentation for GraphQL support * * Fix to the latest Spring for GraphQL * Mention in the doc an `ExecutionGraphQlRequest` as a request message payload
1 parent eaa88cd commit 67e0599

File tree

5 files changed

+124
-33
lines changed

5 files changed

+124
-33
lines changed

spring-integration-graphql/src/main/java/org/springframework/integration/graphql/outbound/GraphQlMessageHandler.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
import org.springframework.expression.Expression;
2424
import org.springframework.expression.common.LiteralExpression;
2525
import org.springframework.expression.spel.support.StandardEvaluationContext;
26-
import org.springframework.graphql.GraphQlService;
27-
import org.springframework.graphql.RequestInput;
26+
import org.springframework.graphql.ExecutionGraphQlRequest;
27+
import org.springframework.graphql.ExecutionGraphQlService;
28+
import org.springframework.graphql.support.DefaultExecutionGraphQlRequest;
2829
import org.springframework.integration.expression.ExpressionUtils;
2930
import org.springframework.integration.expression.FunctionExpression;
3031
import org.springframework.integration.expression.SupplierExpression;
@@ -44,7 +45,7 @@
4445
*/
4546
public class GraphQlMessageHandler extends AbstractReplyProducingMessageHandler {
4647

47-
private final GraphQlService graphQlService;
48+
private final ExecutionGraphQlService graphQlService;
4849

4950
private StandardEvaluationContext evaluationContext;
5051

@@ -60,7 +61,7 @@ public class GraphQlMessageHandler extends AbstractReplyProducingMessageHandler
6061
private Expression executionIdExpression =
6162
new FunctionExpression<Message<?>>(message -> message.getHeaders().getId());
6263

63-
public GraphQlMessageHandler(final GraphQlService graphQlService) {
64+
public GraphQlMessageHandler(final ExecutionGraphQlService graphQlService) {
6465
Assert.notNull(graphQlService, "'graphQlService' must not be null");
6566
this.graphQlService = graphQlService;
6667
setAsync(true);
@@ -135,21 +136,21 @@ protected final void doInit() {
135136

136137
@Override
137138
protected Object handleRequestMessage(Message<?> requestMessage) {
138-
RequestInput requestInput;
139+
ExecutionGraphQlRequest graphQlRequest;
139140

140-
if (requestMessage.getPayload() instanceof RequestInput) {
141-
requestInput = (RequestInput) requestMessage.getPayload();
141+
if (requestMessage.getPayload() instanceof ExecutionGraphQlRequest) {
142+
graphQlRequest = (ExecutionGraphQlRequest) requestMessage.getPayload();
142143
}
143144
else {
144145
Assert.notNull(this.operationExpression, "'operationExpression' must not be null");
145146
String query = evaluateOperationExpression(requestMessage);
146147
String operationName = evaluateOperationNameExpression(requestMessage);
147148
Map<String, Object> variables = evaluateVariablesExpression(requestMessage);
148149
String id = evaluateExecutionIdExpression(requestMessage);
149-
requestInput = new RequestInput(query, operationName, variables, id, this.locale);
150+
graphQlRequest = new DefaultExecutionGraphQlRequest(query, operationName, variables, id, this.locale);
150151
}
151152

152-
return this.graphQlService.execute(requestInput);
153+
return this.graphQlService.execute(graphQlRequest);
153154

154155
}
155156

spring-integration-graphql/src/test/java/org/springframework/integration/graphql/outbound/GraphQlMessageHandlerTests.java

+24-22
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@
2929
import org.springframework.context.annotation.Bean;
3030
import org.springframework.context.annotation.Configuration;
3131
import org.springframework.core.io.ClassPathResource;
32-
import org.springframework.graphql.GraphQlService;
33-
import org.springframework.graphql.RequestInput;
34-
import org.springframework.graphql.RequestOutput;
32+
import org.springframework.graphql.ExecutionGraphQlRequest;
33+
import org.springframework.graphql.ExecutionGraphQlResponse;
34+
import org.springframework.graphql.ExecutionGraphQlService;
3535
import org.springframework.graphql.data.method.annotation.Argument;
3636
import org.springframework.graphql.data.method.annotation.MutationMapping;
3737
import org.springframework.graphql.data.method.annotation.QueryMapping;
3838
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
3939
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
40-
import org.springframework.graphql.execution.ExecutionGraphQlService;
40+
import org.springframework.graphql.execution.DefaultExecutionGraphQlService;
4141
import org.springframework.graphql.execution.GraphQlSource;
42+
import org.springframework.graphql.support.DefaultExecutionGraphQlRequest;
4243
import org.springframework.integration.channel.FluxMessageChannel;
4344
import org.springframework.integration.channel.QueueChannel;
4445
import org.springframework.integration.config.EnableIntegration;
@@ -94,18 +95,19 @@ void testHandleMessageForQueryWithRequestInputProvided() {
9495
StepVerifier.create(
9596
Flux.from(this.resultChannel)
9697
.map(Message::getPayload)
97-
.cast(RequestOutput.class)
98+
.cast(ExecutionGraphQlResponse.class)
9899
)
99100
.consumeNextWith(result -> {
100-
assertThat(result).isInstanceOf(RequestOutput.class);
101+
assertThat(result).isInstanceOf(ExecutionGraphQlResponse.class);
101102
Map<String, Object> data = result.getData();
102103
Map<String, Object> testQuery = (Map<String, Object>) data.get("testQuery");
103104
assertThat(testQuery.get("id")).isEqualTo("test-data");
104105
})
105106
.thenCancel()
106107
.verifyLater();
107108

108-
RequestInput payload = new RequestInput("{ testQuery { id } }", null, null, UUID.randomUUID().toString(), null);
109+
ExecutionGraphQlRequest payload = new DefaultExecutionGraphQlRequest("{ testQuery { id } }", null, null,
110+
UUID.randomUUID().toString(), null);
109111
this.inputChannel.send(MessageBuilder.withPayload(payload).build());
110112

111113
verifier.verify(Duration.ofSeconds(10));
@@ -120,11 +122,11 @@ void testHandleMessageForQueryWithQueryProvided() {
120122
Locale locale = Locale.getDefault();
121123
this.graphQlMessageHandler.setLocale(locale);
122124

123-
Mono<RequestOutput> resultMono =
124-
(Mono<RequestOutput>) this.graphQlMessageHandler.handleRequestMessage(new GenericMessage<>(fakeQuery));
125+
Mono<ExecutionGraphQlResponse> resultMono =
126+
(Mono<ExecutionGraphQlResponse>) this.graphQlMessageHandler.handleRequestMessage(new GenericMessage<>(fakeQuery));
125127
StepVerifier.create(resultMono)
126128
.consumeNextWith(result -> {
127-
assertThat(result).isInstanceOf(RequestOutput.class);
129+
assertThat(result).isInstanceOf(ExecutionGraphQlResponse.class);
128130
Map<String, Object> data = result.getData();
129131
Map<String, Object> testQuery = (Map<String, Object>) data.get("testQuery");
130132
assertThat(testQuery.get("id")).isEqualTo("test-data");
@@ -142,10 +144,10 @@ void testHandleMessageForMutationWithRequestInputProvided() {
142144
StepVerifier verifier = StepVerifier.create(
143145
Flux.from(this.resultChannel)
144146
.map(Message::getPayload)
145-
.cast(RequestOutput.class)
147+
.cast(ExecutionGraphQlResponse.class)
146148
)
147149
.consumeNextWith(result -> {
148-
assertThat(result).isInstanceOf(RequestOutput.class);
150+
assertThat(result).isInstanceOf(ExecutionGraphQlResponse.class);
149151
Map<String, Object> data = result.getData();
150152
Map<String, Object> update = (Map<String, Object>) data.get("update");
151153
assertThat(update.get("id")).isEqualTo(fakeId);
@@ -156,8 +158,8 @@ void testHandleMessageForMutationWithRequestInputProvided() {
156158
.thenCancel()
157159
.verifyLater();
158160

159-
RequestInput payload =
160-
new RequestInput("mutation { update(id: \"" + fakeId + "\") { id } }", null, null,
161+
ExecutionGraphQlRequest payload =
162+
new DefaultExecutionGraphQlRequest("mutation { update(id: \"" + fakeId + "\") { id } }", null, null,
161163
UUID.randomUUID().toString(), null);
162164
this.inputChannel.send(MessageBuilder.withPayload(payload).build());
163165

@@ -175,8 +177,8 @@ void testHandleMessageForSubscriptionWithRequestInputProvided() {
175177
StepVerifier verifier = StepVerifier.create(
176178
Flux.from(this.resultChannel)
177179
.map(Message::getPayload)
178-
.cast(RequestOutput.class)
179-
.mapNotNull(RequestOutput::getData)
180+
.cast(ExecutionGraphQlResponse.class)
181+
.mapNotNull(ExecutionGraphQlResponse::getData)
180182
.cast(SubscriptionPublisher.class)
181183
.map(Flux::from)
182184
.flatMap(data -> data)
@@ -195,8 +197,9 @@ void testHandleMessageForSubscriptionWithRequestInputProvided() {
195197
.thenCancel()
196198
.verifyLater();
197199

198-
RequestInput payload =
199-
new RequestInput("subscription { results { id } }", null, null, UUID.randomUUID().toString(), null);
200+
ExecutionGraphQlRequest payload =
201+
new DefaultExecutionGraphQlRequest("subscription { results { id } }", null, null,
202+
UUID.randomUUID().toString(), null);
200203
this.inputChannel.send(MessageBuilder.withPayload(payload).build());
201204

202205
verifier.verify(Duration.ofSeconds(10));
@@ -279,8 +282,7 @@ Mono<Update> current() {
279282
static class TestConfig {
280283

281284
@Bean
282-
GraphQlMessageHandler handler(GraphQlService graphQlService) {
283-
285+
GraphQlMessageHandler handler(ExecutionGraphQlService graphQlService) {
284286
return new GraphQlMessageHandler(graphQlService);
285287
}
286288

@@ -308,8 +310,8 @@ GraphQlController graphqlQueryController(UpdateRepository updateRepository) {
308310
}
309311

310312
@Bean
311-
GraphQlService graphQlService(GraphQlSource graphQlSource) {
312-
return new ExecutionGraphQlService(graphQlSource);
313+
ExecutionGraphQlService graphQlService(GraphQlSource graphQlSource) {
314+
return new DefaultExecutionGraphQlService(graphQlSource);
313315
}
314316

315317
@Bean

src/reference/asciidoc/endpoint-summary.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ The following table summarizes the various endpoints with quick links to the app
8484
| N
8585
| N
8686

87+
| *GraphQL*
88+
| N
89+
| N
90+
| N
91+
| <<./graphql.adoc#graphql-outbound-gateway,GraphQL Outbound Gateway>>
92+
8793
| *HTTP*
8894
| <<./http.adoc#http-namespace,HTTP Namespace Support>>
8995
| <<./http.adoc#http-namespace,HTTP Namespace Support>>

src/reference/asciidoc/graphql.adoc

+83-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[[graphql]]
22
== GraphQL Support
33

4-
Spring Integration provides support for GraphQL.
4+
Spring Integration provides channel adapters for interaction with https://graphql.org/[GraphQL] protocol.
5+
The implementation is based on the https://spring.io/projects/spring-graphql[Spring for GraphQL].
56

67
You need to include this dependency into your project:
78

@@ -21,3 +22,84 @@ You need to include this dependency into your project:
2122
compile "org.springframework.integration:spring-integration-graphql:{project-version}"
2223
----
2324
====
25+
26+
[[graphql-outbound-gateway]]
27+
=== GraphQL Outbound Gateway
28+
29+
The `GraphQlMessageHandler` is an `AbstractReplyProducingMessageHandler` extension representing an outbound gateway contract to perform GraphQL `query`, `mutation` or `subscription` operation and produce their result.
30+
It requires a `org.springframework.graphql.ExecutionGraphQlService` for execution of `operation`, which can be configured statically or via SpEL expression against a request message.
31+
The `operationName` is optional and also can be configured statically or via SpEL expression.
32+
The `variablesExpression` is also optional and used for parametrized operations.
33+
The `locale` is optional and used for operation execution context in the https://www.graphql-java.com/[GraphQL Java] library.
34+
The `executionId` can be configured via SpEL expression and defaults to `id` header of the request message.
35+
36+
If the payload of request message is an instance of `ExecutionGraphQlRequest`, then there's no any setup actions are performed in the `GraphQlMessageHandler` and such an input is used as is for the `ExecutionGraphQlService.execute()`.
37+
Otherwise, the `operation`, `operationName`, `variables` and `executionId` are determined against request message using SpEL expressions mentioned above.
38+
39+
The `GraphQlMessageHandler` is a reactive streams component and produces a `Mono<ExecutionGraphQlResponse>` reply as a result of the `ExecutionGraphQlService.execute(ExecutionGraphQlRequest)`.
40+
Such a `Mono` is subscribed by the framework in the `ReactiveStreamsSubscribableChannel` output channel or in the `AbstractMessageProducingHandler` asynchronously when the output channel is not reactive.
41+
See documentation for the `ExecutionGraphQlResponse` how to process the GraphQL operation result.
42+
43+
====
44+
[source, java]
45+
----
46+
@Bean
47+
GraphQlMessageHandler handler(ExecutionGraphQlService graphQlService) {
48+
GraphQlMessageHandler graphQlMessageHandler = new GraphQlMessageHandler(graphQlService);
49+
graphQlMessageHandler.setOperation("""
50+
query HeroNameAndFriends($episode: Episode) {
51+
hero(episode: $episode) {
52+
name
53+
friends {
54+
name
55+
}
56+
}
57+
}""");
58+
graphQlMessageHandler.setVariablesExpression(new SpelExpressionParser().parseExpression("{episode:'JEDI'}"));
59+
return graphQlMessageHandler;
60+
}
61+
62+
@Bean
63+
IntegrationFlow graphqlQueryMessageHandlerFlow(GraphQlMessageHandler handler) {
64+
return IntegrationFlows.from(MessageChannels.flux("inputChannel"))
65+
.handle(handler)
66+
.channel(c -> c.flux("resultChannel"))
67+
.get();
68+
}
69+
70+
@Bean
71+
ExecutionGraphQlService graphQlService(GraphQlSource graphQlSource) {
72+
return new DefaultExecutionGraphQlService(graphQlSource);
73+
}
74+
75+
@Bean
76+
GraphQlSource graphQlSource(AnnotatedControllerConfigurer annotatedDataFetcherConfigurer) {
77+
return GraphQlSource.builder()
78+
.schemaResources(new ClassPathResource("graphql/test-schema.graphqls"))
79+
.configureRuntimeWiring(annotatedDataFetcherConfigurer)
80+
.build();
81+
}
82+
83+
@Bean
84+
AnnotatedControllerConfigurer annotatedDataFetcherConfigurer() {
85+
return new AnnotatedControllerConfigurer();
86+
}
87+
----
88+
====
89+
90+
The special treatment should be applied for the result of a subscription operation.
91+
In this case the `RequestOutput.getData()` returns a `SubscriptionPublisher` which has to subscribed and processed manually.
92+
Or it can be flat-mapped via plain service activator to the reply for the `FluxMessageChannel`:
93+
94+
====
95+
[source, java]
96+
----
97+
@ServiceActivator(inputChannel = "graphQlResultChannel", outputChannel="graphQlSubscriptionChannel")
98+
public SubscriptionPublisher obtainSubscriptionResult(RequestOutput output) {
99+
return output.getData(0);
100+
}
101+
----
102+
====
103+
104+
Such an outbound gateway can be used not only for GraphQL request via HTTP, but from any upstream endpoint which produces or carries a GraphQL operation or its arguments in the message.
105+
The result of the `GraphQlMessageHandler` handling can be produces as a reply to the upstream request or sent downstream for further processing in the integration flow.

src/reference/asciidoc/reactive-streams.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public class MainFlow {
326326
----
327327
====
328328

329-
Currently, Spring Integration provides channel adapter (or gateway) implementations for <<./webflux.adoc#webflux,WebFlux>>, <<./rsocket.adoc#rsocket,RSocket>>, <<./mongodb.adoc#mongodb,MongoDb>>, <<./r2dbc.adoc#r2dbc,R2DBC>>, <<./zeromq.adoc#zeromq,ZeroMQ>>.
329+
Currently, Spring Integration provides channel adapter (or gateway) implementations for <<./webflux.adoc#webflux,WebFlux>>, <<./rsocket.adoc#rsocket,RSocket>>, <<./mongodb.adoc#mongodb,MongoDb>>, <<./r2dbc.adoc#r2dbc,R2DBC>>, <<./zeromq.adoc#zeromq,ZeroMQ>>, <<./graphql.adoc#graphql,GraphQL>>.
330330
The <<./redis.adoc#redis-stream-outbound,Redis Stream Channel Adapters>> are also reactive and uses `ReactiveStreamOperations` from Spring Data.
331331
Also, an https://github.com/spring-projects/spring-integration-extensions/tree/main/spring-integration-cassandra[Apache Cassandra Extension] provides a `MessageHandler` implementation for the Cassandra reactive driver.
332332
More reactive channel adapters are coming, for example for Apache Kafka in <<./kafka.adoc#kafka,Kafka>> based on the `ReactiveKafkaProducerTemplate` and `ReactiveKafkaConsumerTemplate` from https://spring.io/projects/spring-kafka[Spring for Apache Kafka] etc.

0 commit comments

Comments
 (0)