Skip to content

Commit efb2c04

Browse files
committed
implement requested changes
issue spring-projects#3501
1 parent fbb848d commit efb2c04

File tree

3 files changed

+42
-98
lines changed

3 files changed

+42
-98
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ ext {
9999
smackVersion = '4.3.5'
100100
springAmqpVersion = project.hasProperty('springAmqpVersion') ? project.springAmqpVersion : '3.0.0-M1'
101101
springDataVersion = project.hasProperty('springDataVersion') ? project.springDataVersion : '2022.0.0-M1'
102+
springGraphqlVersion = '1.0.0-M5'
102103
springKafkaVersion = '3.0.0-M1'
103104
springRetryVersion = '1.3.1'
104105
springSecurityVersion = project.hasProperty('springSecurityVersion') ? project.springSecurityVersion : '6.0.0-M1'

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.util.Locale;
2121
import java.util.Map;
2222

23-
import javax.annotation.Nullable;
24-
2523
import org.springframework.beans.factory.BeanFactory;
2624
import org.springframework.expression.Expression;
2725
import org.springframework.expression.common.LiteralExpression;
@@ -31,6 +29,7 @@
3129
import org.springframework.integration.expression.ExpressionUtils;
3230
import org.springframework.integration.expression.SupplierExpression;
3331
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
32+
import org.springframework.lang.Nullable;
3433
import org.springframework.messaging.Message;
3534
import org.springframework.util.Assert;
3635

@@ -46,13 +45,14 @@ public class GraphQlMessageHandler extends AbstractReplyProducingMessageHandler
4645

4746
private StandardEvaluationContext evaluationContext;
4847

49-
private Expression queryExpression;
48+
private Expression operationExpression;
5049

5150
private Expression operationNameExpression = new SupplierExpression<>(() -> null);
5251

5352
private Expression variablesExpression = new SupplierExpression<>(() -> Collections.emptyMap());
5453

55-
private Locale locale = null;
54+
@Nullable
55+
private Locale locale;
5656

5757
private String executionId = null;
5858

@@ -64,21 +64,21 @@ public GraphQlMessageHandler(final GraphQlService graphQlService) {
6464
}
6565

6666
/**
67-
* Specify a GraphQL Query.
68-
* @param query the GraphQL query to use.
67+
* Specify a GraphQL Operation.
68+
* @param operation the GraphQL operation to use.
6969
*/
70-
public void setQuery(String query) {
71-
Assert.hasText(query, "'query' must not be empty");
72-
setQueryExpression(new LiteralExpression(query));
70+
public void setOperation(String operation) {
71+
Assert.hasText(operation, "'operation' must not be empty");
72+
setOperationExpression(new LiteralExpression(operation));
7373
}
7474

7575
/**
76-
* Specify a SpEL expression to evaluate a GraphQL Query
77-
* @param queryExpression the expression to evaluate a GraphQL query.
76+
* Specify a SpEL expression to evaluate a GraphQL Operation
77+
* @param operationExpression the expression to evaluate a GraphQL Operation.
7878
*/
79-
public void setQueryExpression(Expression queryExpression) {
80-
Assert.notNull(queryExpression, "'queryExpression' must not be null");
81-
this.queryExpression = queryExpression;
79+
public void setOperationExpression(Expression operationExpression) {
80+
Assert.notNull(operationExpression, "'queryExpression' must not be null");
81+
this.operationExpression = operationExpression;
8282
}
8383

8484
/**
@@ -99,7 +99,7 @@ public void setOperationNameExpression(Expression operationNameExpression) {
9999
}
100100

101101
/**
102-
* Set a SpEL expression to evaluate Variables for GraphQL Query to execute.
102+
* Set a SpEL expression to evaluate Variables for GraphQL Operation to execute.
103103
* @param variablesExpression the expression to use.
104104
*/
105105
public void setVariablesExpression(Expression variablesExpression) {
@@ -108,15 +108,15 @@ public void setVariablesExpression(Expression variablesExpression) {
108108
}
109109

110110
/**
111-
* Set a Locale for GraphQL Query to execute.
111+
* Set a Locale for GraphQL Operation to execute.
112112
* @param locale the locale to use.
113113
*/
114114
public void setLocale(@Nullable Locale locale) {
115115
this.locale = locale;
116116
}
117117

118118
/**
119-
* Set a Execution Id for GraphQL Query to execute.
119+
* Set a Execution Id for GraphQL Operation to execute.
120120
* @param executionId the executionId to use.
121121
*/
122122
public void setExecutionId(String executionId) {
@@ -139,9 +139,9 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
139139
requestInput = (RequestInput) requestMessage.getPayload();
140140
}
141141
else {
142-
Assert.notNull(this.queryExpression, "'queryExpression' must not be null");
143-
Assert.hasText(this.executionId, "'executionId' must not be empty");
144-
String query = evaluateQueryExpression(requestMessage);
142+
Assert.notNull(this.operationExpression, "'operationExpression' must not be null");
143+
// Assert.hasText(this.executionId, "'executionId' must not be empty");
144+
String query = evaluateOperationExpression(requestMessage);
145145
String operationName = evaluateOperationNameExpression(requestMessage);
146146
Map<String, Object> variables = evaluateVariablesExpression(requestMessage);
147147
requestInput = new RequestInput(query, operationName, variables, this.locale, this.executionId);
@@ -152,10 +152,10 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
152152

153153
}
154154

155-
private String evaluateQueryExpression(Message<?> message) {
156-
String query = this.queryExpression.getValue(this.evaluationContext, message, String.class);
157-
Assert.notNull(query, "'queryExpression' must not evaluate to null");
158-
return query;
155+
private String evaluateOperationExpression(Message<?> message) {
156+
String operation = this.operationExpression.getValue(this.evaluationContext, message, String.class);
157+
Assert.notNull(operation, "'operationExpression' must not evaluate to null");
158+
return operation;
159159
}
160160

161161
private String evaluateOperationNameExpression(Message<?> message) {

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

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,10 @@ public class GraphQlMessageHandlerTests {
8888
@Autowired
8989
private UpdateRepository updateRepository;
9090

91-
@Autowired
92-
private IdGenerator idGenerator;
93-
94-
9591
@Test
9692
@SuppressWarnings("unchecked")
9793
void testHandleMessageForQueryWithRequestInputProvided() {
9894

99-
Locale locale = Locale.getDefault();
100-
String executionId = this.idGenerator.generateId().toString();
101-
this.graphQlMessageHandler.setLocale(locale);
102-
this.graphQlMessageHandler.setExecutionId(executionId);
103-
10495
StepVerifier verifier = StepVerifier.create(
10596
Flux.from(this.resultChannel)
10697
.map(Message::getPayload)
@@ -117,7 +108,7 @@ void testHandleMessageForQueryWithRequestInputProvided() {
117108

118109
this.inputChannel.send(
119110
MessageBuilder
120-
.withPayload(new RequestInput("{ testQuery { id } }", null, Collections.emptyMap(), locale, executionId))
111+
.withPayload(new RequestInput("{ testQuery { id } }", null, Collections.emptyMap(), null, UUID.randomUUID().toString()))
121112
.build()
122113
);
123114

@@ -128,11 +119,14 @@ void testHandleMessageForQueryWithRequestInputProvided() {
128119
@SuppressWarnings("unchecked")
129120
void testHandleMessageForQueryWithQueryProvided() {
130121

131-
String executionId = this.idGenerator.generateId().toString();
132-
this.graphQlMessageHandler.setExecutionId(executionId);
133-
134122
String fakeQuery = "{ testQuery { id } }";
135-
this.graphQlMessageHandler.setQuery(fakeQuery);
123+
this.graphQlMessageHandler.setOperation(fakeQuery);
124+
125+
Locale locale = Locale.getDefault();
126+
this.graphQlMessageHandler.setLocale(locale);
127+
128+
String executionId = UUID.randomUUID().toString();
129+
this.graphQlMessageHandler.setExecutionId(executionId);
136130

137131
StepVerifier.create(
138132
Mono.from((Mono<RequestOutput>) this.graphQlMessageHandler.handleRequestMessage(MessageBuilder.withPayload(fakeQuery).build()))
@@ -151,9 +145,6 @@ void testHandleMessageForQueryWithQueryProvided() {
151145
@SuppressWarnings("unchecked")
152146
void testHandleMessageForMutationWithRequestInputProvided() {
153147

154-
String executionId = this.idGenerator.generateId().toString();
155-
this.graphQlMessageHandler.setExecutionId(executionId);
156-
157148
String fakeId = UUID.randomUUID().toString();
158149
Update expected = new Update(fakeId);
159150

@@ -176,7 +167,7 @@ void testHandleMessageForMutationWithRequestInputProvided() {
176167

177168
this.inputChannel.send(
178169
MessageBuilder
179-
.withPayload(new RequestInput("mutation { update(id: \"" + fakeId + "\") { id } }", null, Collections.emptyMap(), null, executionId))
170+
.withPayload(new RequestInput("mutation { update(id: \"" + fakeId + "\") { id } }", null, Collections.emptyMap(), null, UUID.randomUUID().toString()))
180171
.build()
181172
);
182173

@@ -193,9 +184,6 @@ void testHandleMessageForMutationWithRequestInputProvided() {
193184
@SuppressWarnings("unchecked")
194185
void testHandleMessageForSubscriptionWithRequestInputProvided() {
195186

196-
String executionId = this.idGenerator.generateId().toString();
197-
this.graphQlMessageHandler.setExecutionId(executionId);
198-
199187
StepVerifier verifier = StepVerifier.create(
200188
Flux.from(this.resultChannel)
201189
.map(Message::getPayload)
@@ -209,8 +197,8 @@ void testHandleMessageForSubscriptionWithRequestInputProvided() {
209197
Map<String, Object> results = (Map<String, Object>) requestOutput.getData();
210198
assertThat(results).containsKey("results");
211199

212-
Map<String, Object> queryResult = (Map<String, Object>) results.get("results");
213-
assertThat(queryResult)
200+
Map<String, Object> operationResult = (Map<String, Object>) results.get("results");
201+
assertThat(operationResult)
214202
.containsKey("id")
215203
.containsValue("test-data-01");
216204

@@ -221,35 +209,15 @@ void testHandleMessageForSubscriptionWithRequestInputProvided() {
221209

222210
this.inputChannel.send(
223211
MessageBuilder
224-
.withPayload(new RequestInput("subscription { results { id } }", null, Collections.emptyMap(), null, executionId))
212+
.withPayload(new RequestInput("subscription { results { id } }", null, Collections.emptyMap(), null, UUID.randomUUID().toString()))
225213
.build()
226214
);
227215

228216
verifier.verify(Duration.ofSeconds(10));
229217
}
230218

231219
@Test
232-
void testHandleMessageForQueryWithInvalidPayload() {
233-
234-
this.inputChannel.send(
235-
MessageBuilder
236-
.withPayload(new Object())
237-
.build()
238-
);
239-
240-
Message<?> errorMessage = errorChannel.receive(10_000);
241-
assertThat(errorMessage).isNotNull()
242-
.isInstanceOf(ErrorMessage.class)
243-
.extracting(Message::getPayload)
244-
.isInstanceOf(MessageHandlingException.class)
245-
.satisfies((ex) -> assertThat((Exception) ex)
246-
.hasMessageContaining(
247-
"'queryExpression' must not be null"));
248-
249-
}
250-
251-
@Test
252-
void testHandleMessageForMutationWithInvalidPayload() {
220+
void testHandleMessageWithInvalidPayload() {
253221

254222
this.inputChannel.send(
255223
MessageBuilder
@@ -264,27 +232,7 @@ void testHandleMessageForMutationWithInvalidPayload() {
264232
.isInstanceOf(MessageHandlingException.class)
265233
.satisfies((ex) -> assertThat((Exception) ex)
266234
.hasMessageContaining(
267-
"'queryExpression' must not be null"));
268-
269-
}
270-
271-
@Test
272-
void testHandleMessageForSubscriptionWithInvalidPayload() {
273-
274-
this.inputChannel.send(
275-
MessageBuilder
276-
.withPayload(new Object())
277-
.build()
278-
);
279-
280-
Message<?> errorMessage = errorChannel.receive(10_000);
281-
assertThat(errorMessage).isNotNull()
282-
.isInstanceOf(ErrorMessage.class)
283-
.extracting(Message::getPayload)
284-
.isInstanceOf(MessageHandlingException.class)
285-
.satisfies((ex) -> assertThat((Exception) ex)
286-
.hasMessageContaining(
287-
"'queryExpression' must not be null"));
235+
"'operationExpression' must not be null"));
288236

289237
}
290238

@@ -356,12 +304,13 @@ GraphQlMessageHandler handler(GraphQlService graphQlService) {
356304
return new GraphQlMessageHandler(graphQlService);
357305
}
358306

307+
// @artem
359308
@Bean
360309
IntegrationFlow graphqlQueryMessageHandlerFlow(GraphQlMessageHandler handler) {
361310

362-
return IntegrationFlows.from(MessageChannels.flux("inputChannel"))
311+
return IntegrationFlows.from(MessageChannels.flux("inputChannel").datatype(Object.class, RequestInput.class))
363312
.handle(handler)
364-
.channel(c -> c.flux("resultChannel"))
313+
.channel(c -> c.flux("resultChannel").datatype(RequestOutput.class))
365314
.get();
366315
}
367316

@@ -403,12 +352,6 @@ AnnotatedControllerConfigurer annotatedDataFetcherConfigurer() {
403352
return new AnnotatedControllerConfigurer();
404353
}
405354

406-
@Bean
407-
IdGenerator idGenerator() {
408-
409-
return new AlternativeJdkIdGenerator();
410-
}
411-
412355
}
413356

414357
static class QueryResult {

0 commit comments

Comments
 (0)