Skip to content

Commit dfe17d4

Browse files
committed
implement requested changes
issue spring-projects#3501
1 parent 5fa6e43 commit dfe17d4

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

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

+9-15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22+
import org.springframework.beans.factory.BeanFactory;
2223
import org.springframework.expression.Expression;
2324
import org.springframework.expression.common.LiteralExpression;
2425
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -30,8 +31,6 @@
3031
import org.springframework.messaging.Message;
3132
import org.springframework.util.Assert;
3233

33-
import graphql.com.google.common.base.Strings;
34-
3534
/**
3635
* A {@link org.springframework.messaging.MessageHandler} capable of fielding GraphQL Query, Mutation and Subscription requests.
3736
*
@@ -53,7 +52,6 @@ public class GraphQlMessageHandler extends AbstractReplyProducingMessageHandler
5352
public GraphQlMessageHandler(final GraphQlService graphQlService) {
5453
Assert.notNull(graphQlService, "'graphQlService' must not be null");
5554

56-
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
5755
this.graphQlService = graphQlService;
5856
setAsync(true);
5957
}
@@ -92,14 +90,6 @@ public void setOperationNameExpression(Expression operationNameExpression) {
9290
this.operationNameExpression = operationNameExpression;
9391
}
9492

95-
/**
96-
* Specify variables for the GraphQL Query to execute.
97-
* @param variables the GraphQL variables to use.
98-
*/
99-
public void setVariables(Map<String, Object> variables) {
100-
setVariablesExpression(new SupplierExpression<>(() -> variables));
101-
}
102-
10393
/**
10494
* Set a SpEL expression to evaluate Variables for GraphQL Query to execute.
10595
* @param variablesExpression the expression to use.
@@ -109,6 +99,12 @@ public void setVariablesExpression(Expression variablesExpression) {
10999
this.variablesExpression = variablesExpression;
110100
}
111101

102+
@Override
103+
protected final void doInit() {
104+
BeanFactory beanFactory = getBeanFactory();
105+
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(beanFactory);
106+
}
107+
112108
@Override
113109
protected Object handleRequestMessage(Message<?> requestMessage) {
114110

@@ -117,16 +113,14 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
117113
return this.graphQlService
118114
.execute((RequestInput) requestMessage.getPayload());
119115
}
120-
else if (requestMessage.getPayload() instanceof String && !Strings.isNullOrEmpty((String) requestMessage.getPayload())) {
116+
else {
117+
Assert.notNull(this.queryExpression, "'queryExpression' must not be null");
121118
String query = evaluateQueryExpression(requestMessage);
122119
String operationName = evaluateOperationNameExpression(requestMessage);
123120
Map<String, Object> variables = evaluateVariablesExpression(requestMessage);
124121
return this.graphQlService
125122
.execute(new RequestInput(query, operationName, variables));
126123
}
127-
else {
128-
throw new IllegalArgumentException("Message payload does not meet criteria to construct a 'org.springframework.graphql.RequestInput'");
129-
}
130124
}
131125

132126
private String evaluateQueryExpression(Message<?> message) {

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.integration.dsl.IntegrationFlow;
4646
import org.springframework.integration.dsl.IntegrationFlows;
4747
import org.springframework.integration.dsl.MessageChannels;
48+
import org.springframework.integration.expression.FunctionExpression;
4849
import org.springframework.messaging.Message;
4950
import org.springframework.messaging.MessageHandlingException;
5051
import org.springframework.messaging.PollableChannel;
@@ -161,7 +162,7 @@ void testHandleMessageForQueryWithQueryAndOperationNameAndVariablesProvided() {
161162
String fakeQuery = "query FriendlyName($id: String) { testQueryById(id: $id) { id } }";
162163
this.graphQlMessageHandler.setQuery(fakeQuery);
163164
this.graphQlMessageHandler.setOperationName("FriendlyName");
164-
this.graphQlMessageHandler.setVariables(Map.of("$id", "test-data"));
165+
this.graphQlMessageHandler.setVariablesExpression(new FunctionExpression<>(m -> Map.of("$id", "test-data")));
165166

166167
StepVerifier.create(
167168
Mono.from((Mono<ExecutionResult>) this.graphQlMessageHandler.handleRequestMessage(MessageBuilder.withPayload(fakeQuery).build()))
@@ -267,15 +268,13 @@ void testHandleMessageForQueryWithInvalidPayload() {
267268
.isInstanceOf(MessageHandlingException.class)
268269
.satisfies((ex) -> assertThat((Exception) ex)
269270
.hasMessageContaining(
270-
"Message payload does not meet criteria to construct a 'org.springframework.graphql.RequestInput'"));
271+
"'queryExpression' must not be null"));
271272

272273
}
273274

274275
@Test
275276
void testHandleMessageForMutationWithInvalidPayload() {
276277

277-
String fakeId = UUID.randomUUID().toString();
278-
279278
this.inputChannel.send(
280279
MessageBuilder
281280
.withPayload(new Object())
@@ -289,7 +288,7 @@ void testHandleMessageForMutationWithInvalidPayload() {
289288
.isInstanceOf(MessageHandlingException.class)
290289
.satisfies((ex) -> assertThat((Exception) ex)
291290
.hasMessageContaining(
292-
"Message payload does not meet criteria to construct a 'org.springframework.graphql.RequestInput'"));
291+
"'queryExpression' must not be null"));
293292

294293
}
295294

@@ -309,7 +308,7 @@ void testHandleMessageForSubscriptionWithInvalidPayload() {
309308
.isInstanceOf(MessageHandlingException.class)
310309
.satisfies((ex) -> assertThat((Exception) ex)
311310
.hasMessageContaining(
312-
"Message payload does not meet criteria to construct a 'org.springframework.graphql.RequestInput'"));
311+
"'queryExpression' must not be null"));
313312

314313
}
315314

0 commit comments

Comments
 (0)