19
19
import java .util .Collections ;
20
20
import java .util .Map ;
21
21
22
+ import org .springframework .beans .factory .BeanFactory ;
22
23
import org .springframework .expression .Expression ;
23
24
import org .springframework .expression .common .LiteralExpression ;
24
25
import org .springframework .expression .spel .support .StandardEvaluationContext ;
30
31
import org .springframework .messaging .Message ;
31
32
import org .springframework .util .Assert ;
32
33
33
- import graphql .com .google .common .base .Strings ;
34
-
35
34
/**
36
35
* A {@link org.springframework.messaging.MessageHandler} capable of fielding GraphQL Query, Mutation and Subscription requests.
37
36
*
@@ -53,7 +52,6 @@ public class GraphQlMessageHandler extends AbstractReplyProducingMessageHandler
53
52
public GraphQlMessageHandler (final GraphQlService graphQlService ) {
54
53
Assert .notNull (graphQlService , "'graphQlService' must not be null" );
55
54
56
- this .evaluationContext = ExpressionUtils .createStandardEvaluationContext (getBeanFactory ());
57
55
this .graphQlService = graphQlService ;
58
56
setAsync (true );
59
57
}
@@ -92,14 +90,6 @@ public void setOperationNameExpression(Expression operationNameExpression) {
92
90
this .operationNameExpression = operationNameExpression ;
93
91
}
94
92
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
-
103
93
/**
104
94
* Set a SpEL expression to evaluate Variables for GraphQL Query to execute.
105
95
* @param variablesExpression the expression to use.
@@ -109,6 +99,12 @@ public void setVariablesExpression(Expression variablesExpression) {
109
99
this .variablesExpression = variablesExpression ;
110
100
}
111
101
102
+ @ Override
103
+ protected final void doInit () {
104
+ BeanFactory beanFactory = getBeanFactory ();
105
+ this .evaluationContext = ExpressionUtils .createStandardEvaluationContext (beanFactory );
106
+ }
107
+
112
108
@ Override
113
109
protected Object handleRequestMessage (Message <?> requestMessage ) {
114
110
@@ -117,16 +113,14 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
117
113
return this .graphQlService
118
114
.execute ((RequestInput ) requestMessage .getPayload ());
119
115
}
120
- else if (requestMessage .getPayload () instanceof String && !Strings .isNullOrEmpty ((String ) requestMessage .getPayload ())) {
116
+ else {
117
+ Assert .notNull (this .queryExpression , "'queryExpression' must not be null" );
121
118
String query = evaluateQueryExpression (requestMessage );
122
119
String operationName = evaluateOperationNameExpression (requestMessage );
123
120
Map <String , Object > variables = evaluateVariablesExpression (requestMessage );
124
121
return this .graphQlService
125
122
.execute (new RequestInput (query , operationName , variables ));
126
123
}
127
- else {
128
- throw new IllegalArgumentException ("Message payload does not meet criteria to construct a 'org.springframework.graphql.RequestInput'" );
129
- }
130
124
}
131
125
132
126
private String evaluateQueryExpression (Message <?> message ) {
0 commit comments