4
4
import graphql .GraphQLError ;
5
5
import graphql .PublicSpi ;
6
6
import graphql .Scalars ;
7
- import graphql .schema .GraphQLArgument ;
8
- import graphql .schema .GraphQLDirective ;
9
- import graphql .schema .GraphQLFieldDefinition ;
10
- import graphql .schema .GraphQLFieldsContainer ;
11
- import graphql .schema .GraphQLInputObjectType ;
12
- import graphql .schema .GraphQLInputType ;
13
- import graphql .schema .GraphQLNamedInputType ;
14
- import graphql .schema .GraphQLScalarType ;
15
- import graphql .schema .GraphQLTypeUtil ;
7
+ import graphql .schema .*;
16
8
import graphql .validation .rules .ValidationEnvironment ;
17
9
import graphql .validation .util .DirectivesAndTypeWalker ;
18
10
import graphql .validation .util .Util ;
11
+
19
12
import java .lang .reflect .Array ;
20
13
import java .math .BigDecimal ;
21
14
import java .util .ArrayList ;
25
18
import java .util .LinkedHashMap ;
26
19
import java .util .List ;
27
20
import java .util .Map ;
21
+
28
22
import static graphql .schema .GraphQLTypeUtil .isList ;
29
23
import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .FIELD ;
30
24
import static graphql .validation .util .Util .mkMap ;
@@ -138,17 +132,17 @@ private List<GraphQLError> runValidationImpl(ValidationEnvironment validationEnv
138
132
private List <GraphQLError > runConstraintOnDirectives (ValidationEnvironment validationEnvironment ) {
139
133
140
134
List <GraphQLError > errors = new ArrayList <>();
141
- List <GraphQLDirective > directives = validationEnvironment .getDirectives ();
142
- directives = Util .sort (directives , GraphQLDirective ::getName );
135
+ List <GraphQLAppliedDirective > directives = validationEnvironment .getDirectives ();
136
+ directives = Util .sort (directives , GraphQLAppliedDirective ::getName );
143
137
144
- for (GraphQLDirective directive : directives ) {
138
+ for (GraphQLAppliedDirective directive : directives ) {
145
139
// we get called for arguments and input field and field types which can have multiple directive constraints on them and hence no just for this one
146
140
boolean isOurDirective = directive .getName ().equals (this .getName ());
147
141
if (!isOurDirective ) {
148
142
continue ;
149
143
}
150
144
151
- validationEnvironment = validationEnvironment .transform (b -> b .context (GraphQLDirective .class , directive ));
145
+ validationEnvironment = validationEnvironment .transform (b -> b .context (GraphQLAppliedDirective .class , directive ));
152
146
//
153
147
// now run the directive rule with this directive instance
154
148
List <GraphQLError > ruleErrors = this .runConstrainOnPossibleListElements (validationEnvironment );
@@ -201,18 +195,15 @@ protected boolean isOneOfTheseTypes(GraphQLInputType inputType, Collection<Graph
201
195
* @param argName the argument name
202
196
* @return a non null value
203
197
*/
204
- protected int getIntArg (GraphQLDirective directive , String argName ) {
205
- GraphQLArgument argument = directive .getArgument (argName );
198
+ protected int getIntArg (GraphQLAppliedDirective directive , String argName ) {
199
+ GraphQLAppliedDirectiveArgument argument = directive .getArgument (argName );
206
200
if (argument == null ) {
207
201
return assertExpectedArgType (argName , "Int" );
208
202
}
209
203
210
- Number value = GraphQLArgument . getArgumentValue ( argument );
204
+ Number value = argument . getValue ( );
211
205
if (value == null ) {
212
- value = GraphQLArgument .getArgumentDefaultValue (argument );
213
- if (value == null ) {
214
- return assertExpectedArgType (argName , "Int" );
215
- }
206
+ return assertExpectedArgType (argName , "Int" );
216
207
}
217
208
return value .intValue ();
218
209
}
@@ -224,17 +215,14 @@ protected int getIntArg(GraphQLDirective directive, String argName) {
224
215
* @param argName the argument name
225
216
* @return a non null value
226
217
*/
227
- protected String getStrArg (GraphQLDirective directive , String argName ) {
228
- GraphQLArgument argument = directive .getArgument (argName );
218
+ protected String getStrArg (GraphQLAppliedDirective directive , String argName ) {
219
+ GraphQLAppliedDirectiveArgument argument = directive .getArgument (argName );
229
220
if (argument == null ) {
230
221
return assertExpectedArgType (argName , "String" );
231
222
}
232
- String value = GraphQLArgument . getArgumentValue ( argument );
223
+ String value = argument . getValue ( );
233
224
if (value == null ) {
234
- value = GraphQLArgument .getArgumentDefaultValue (argument );
235
- if (value == null ) {
236
- return assertExpectedArgType (argName , "String" );
237
- }
225
+ return assertExpectedArgType (argName , "String" );
238
226
}
239
227
return value ;
240
228
}
@@ -246,17 +234,14 @@ protected String getStrArg(GraphQLDirective directive, String argName) {
246
234
* @param argName the argument name
247
235
* @return a non null value
248
236
*/
249
- protected boolean getBoolArg (GraphQLDirective directive , String argName ) {
250
- GraphQLArgument argument = directive .getArgument (argName );
237
+ protected boolean getBoolArg (GraphQLAppliedDirective directive , String argName ) {
238
+ GraphQLAppliedDirectiveArgument argument = directive .getArgument (argName );
251
239
if (argument == null ) {
252
240
return assertExpectedArgType (argName , "Boolean" );
253
241
}
254
- Object value = GraphQLArgument . getArgumentValue ( argument );
242
+ Object value = argument . getValue ( );
255
243
if (value == null ) {
256
- value = GraphQLArgument .getArgumentDefaultValue (argument );
257
- if (value == null ) {
258
- return assertExpectedArgType (argName , "Boolean" );
259
- }
244
+ return assertExpectedArgType (argName , "Boolean" );
260
245
}
261
246
return Boolean .parseBoolean (String .valueOf (value ));
262
247
}
@@ -268,14 +253,11 @@ protected boolean getBoolArg(GraphQLDirective directive, String argName) {
268
253
* @param directive the directive to check
269
254
* @return a non null value
270
255
*/
271
- protected String getMessageTemplate (GraphQLDirective directive ) {
256
+ protected String getMessageTemplate (GraphQLAppliedDirective directive ) {
272
257
String msg = null ;
273
- GraphQLArgument arg = directive .getArgument ("message" );
258
+ GraphQLAppliedDirectiveArgument arg = directive .getArgument ("message" );
274
259
if (arg != null ) {
275
- msg = GraphQLArgument .getArgumentValue (arg );
276
- if (msg == null ) {
277
- msg = GraphQLArgument .getArgumentDefaultValue (arg );
278
- }
260
+ msg = arg .getValue ();
279
261
}
280
262
if (msg == null ) {
281
263
msg = "graphql.validation." + getName () + ".message" ;
@@ -310,14 +292,14 @@ protected Map<String, Object> mkMessageParams(Object validatedValue, ValidationE
310
292
* @param msgParams the map of parameters
311
293
* @return a list of a single error
312
294
*/
313
- protected List <GraphQLError > mkError (ValidationEnvironment validationEnvironment , GraphQLDirective directive , Map <String , Object > msgParams ) {
295
+ protected List <GraphQLError > mkError (ValidationEnvironment validationEnvironment , GraphQLAppliedDirective directive , Map <String , Object > msgParams ) {
314
296
String messageTemplate = getMessageTemplate (directive );
315
297
GraphQLError error = validationEnvironment .getInterpolator ().interpolate (messageTemplate , msgParams , validationEnvironment );
316
298
return singletonList (error );
317
299
}
318
300
319
301
protected List <GraphQLError > mkError (ValidationEnvironment validationEnvironment , Object ... messageParameters ) {
320
- GraphQLDirective directive = validationEnvironment .getContextObject (GraphQLDirective .class );
302
+ GraphQLAppliedDirective directive = validationEnvironment .getContextObject (GraphQLAppliedDirective .class );
321
303
String messageTemplate = getMessageTemplate (directive );
322
304
Object validatedValue = validationEnvironment .getValidatedValue ();
323
305
GraphQLError error = validationEnvironment .getInterpolator ().interpolate (messageTemplate , mkMessageParams (validatedValue , validationEnvironment , messageParameters ), validationEnvironment );
0 commit comments