4
4
import graphql .GraphQLError ;
5
5
import graphql .PublicSpi ;
6
6
import graphql .Scalars ;
7
- import graphql .execution .ExecutionPath ;
8
7
import graphql .schema .GraphQLArgument ;
9
8
import graphql .schema .GraphQLDirective ;
10
- import graphql .schema .GraphQLDirectiveContainer ;
11
9
import graphql .schema .GraphQLFieldDefinition ;
12
10
import graphql .schema .GraphQLFieldsContainer ;
13
- import graphql .schema .GraphQLInputObjectField ;
14
11
import graphql .schema .GraphQLInputObjectType ;
15
12
import graphql .schema .GraphQLInputType ;
16
- import graphql .schema .GraphQLList ;
17
13
import graphql .schema .GraphQLScalarType ;
18
14
import graphql .schema .GraphQLTypeUtil ;
19
- import graphql .util .FpKit ;
20
15
import graphql .validation .rules .ValidationEnvironment ;
21
16
import graphql .validation .util .DirectivesAndTypeWalker ;
22
17
import graphql .validation .util .Util ;
25
20
import java .math .BigDecimal ;
26
21
import java .util .ArrayList ;
27
22
import java .util .Collection ;
28
- import java .util .Collections ;
29
23
import java .util .LinkedHashMap ;
30
24
import java .util .List ;
31
25
import java .util .Map ;
32
26
33
27
import static graphql .schema .GraphQLTypeUtil .isList ;
34
28
import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .FIELD ;
35
- import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .INPUT_OBJECT_FIELD ;
36
29
import static java .util .Collections .singletonList ;
37
30
38
31
@ SuppressWarnings ("UnnecessaryLocalVariable" )
@@ -115,9 +108,7 @@ public List<GraphQLError> runValidation(ValidationEnvironment validationEnvironm
115
108
return runFieldValidationImpl (validationEnvironment );
116
109
}
117
110
118
- GraphQLArgument argument = validationEnvironment .getArgument ();
119
111
Object validatedValue = validationEnvironment .getValidatedValue ();
120
- List <GraphQLDirective > directives = argument == null ? Collections .emptyList () : argument .getDirectives ();
121
112
122
113
//
123
114
// all the directives validation code does NOT care for NULL ness since the graphql engine covers that.
@@ -126,42 +117,22 @@ public List<GraphQLError> runValidation(ValidationEnvironment validationEnvironm
126
117
GraphQLInputType inputType = Util .unwrapNonNull (validationEnvironment .getValidatedType ());
127
118
validationEnvironment = validationEnvironment .transform (b -> b .validatedType (inputType ));
128
119
129
- return runValidationImpl (validationEnvironment , inputType , validatedValue , directives );
120
+ return runValidationImpl (validationEnvironment , inputType , validatedValue );
130
121
}
131
122
132
123
private List <GraphQLError > runFieldValidationImpl (ValidationEnvironment validationEnvironment ) {
133
- return runConstraintOnDirectives (validationEnvironment , validationEnvironment . getFieldDefinition (). getDirectives () );
124
+ return runConstraintOnDirectives (validationEnvironment );
134
125
}
135
126
136
127
@ SuppressWarnings ("unchecked" )
137
- private List <GraphQLError > runValidationImpl (ValidationEnvironment validationEnvironment , GraphQLInputType inputType , Object validatedValue , List <GraphQLDirective > directives ) {
138
- List <GraphQLError > errors = runConstraintOnDirectives (validationEnvironment , directives );
139
- if (validatedValue == null ) {
140
- return errors ;
141
- }
142
-
143
- inputType = (GraphQLInputType ) GraphQLTypeUtil .unwrapNonNull (inputType );
144
-
145
- if (GraphQLTypeUtil .isList (inputType )) {
146
- List <Object > values = new ArrayList <>(FpKit .toCollection (validatedValue ));
147
- List <GraphQLError > ruleErrors = walkListArg (validationEnvironment , (GraphQLList ) inputType , values );
148
- errors .addAll (ruleErrors );
149
- }
150
-
151
- if (inputType instanceof GraphQLInputObjectType ) {
152
- if (validatedValue instanceof Map ) {
153
- Map <String , Object > objectValue = (Map <String , Object >) validatedValue ;
154
- List <GraphQLError > ruleErrors = walkObjectArg (validationEnvironment , (GraphQLInputObjectType ) inputType , objectValue );
155
- errors .addAll (ruleErrors );
156
- } else {
157
- Assert .assertShouldNeverHappen ("How can there be a `input` object type '%s' that does not have a matching Map java value" , GraphQLTypeUtil .simplePrint (inputType ));
158
- }
159
- }
160
- return errors ;
128
+ private List <GraphQLError > runValidationImpl (ValidationEnvironment validationEnvironment , GraphQLInputType inputType , Object validatedValue ) {
129
+ return runConstraintOnDirectives (validationEnvironment );
161
130
}
162
131
163
- private List <GraphQLError > runConstraintOnDirectives (ValidationEnvironment validationEnvironment , List <GraphQLDirective > directives ) {
132
+ private List <GraphQLError > runConstraintOnDirectives (ValidationEnvironment validationEnvironment ) {
133
+
164
134
List <GraphQLError > errors = new ArrayList <>();
135
+ List <GraphQLDirective > directives = validationEnvironment .getDirectives ();
165
136
directives = Util .sort (directives , GraphQLDirective ::getName );
166
137
167
138
for (GraphQLDirective directive : directives ) {
@@ -180,63 +151,6 @@ private List<GraphQLError> runConstraintOnDirectives(ValidationEnvironment valid
180
151
return errors ;
181
152
}
182
153
183
- private List <GraphQLError > walkObjectArg (ValidationEnvironment validationEnvironment , GraphQLInputObjectType argumentType , Map <String , Object > objectMap ) {
184
- List <GraphQLError > errors = new ArrayList <>();
185
-
186
- // run them in a stable order
187
- List <GraphQLInputObjectField > fieldDefinitions = Util .sort (argumentType .getFieldDefinitions (), GraphQLInputObjectField ::getName );
188
- for (GraphQLInputObjectField inputField : fieldDefinitions ) {
189
-
190
- GraphQLInputType fieldType = inputField .getType ();
191
- List <GraphQLDirective > directives = inputField .getDirectives ();
192
- Object validatedValue = objectMap .getOrDefault (inputField .getName (), inputField .getDefaultValue ());
193
- if (validatedValue == null ) {
194
- continue ;
195
- }
196
-
197
- ExecutionPath newPath = validationEnvironment .getValidatedPath ().segment (inputField .getName ());
198
-
199
- ValidationEnvironment newValidationEnvironment = validationEnvironment .transform (builder -> builder
200
- .validatedPath (newPath )
201
- .validatedValue (validatedValue )
202
- .validatedType (fieldType )
203
- .validatedElement (INPUT_OBJECT_FIELD )
204
- );
205
-
206
- List <GraphQLError > ruleErrors = runValidationImpl (newValidationEnvironment , fieldType , validatedValue , directives );
207
- errors .addAll (ruleErrors );
208
- }
209
- return errors ;
210
- }
211
-
212
- private List <GraphQLError > walkListArg (ValidationEnvironment validationEnvironment , GraphQLList argumentType , List <Object > objectList ) {
213
- List <GraphQLError > errors = new ArrayList <>();
214
-
215
- GraphQLInputType listItemType = Util .unwrapOneAndAllNonNull (argumentType );
216
- List <GraphQLDirective > directives ;
217
- if (!(listItemType instanceof GraphQLDirectiveContainer )) {
218
- directives = Collections .emptyList ();
219
- } else {
220
- directives = ((GraphQLDirectiveContainer ) listItemType ).getDirectives ();
221
- }
222
- int ix = 0 ;
223
- for (Object value : objectList ) {
224
-
225
- ExecutionPath newPath = validationEnvironment .getValidatedPath ().segment (ix );
226
-
227
- ValidationEnvironment newValidationEnvironment = validationEnvironment .transform (builder -> builder
228
- .validatedPath (newPath )
229
- .validatedValue (value )
230
- .validatedType (listItemType )
231
- );
232
-
233
- List <GraphQLError > ruleErrors = runValidationImpl (newValidationEnvironment , listItemType , value , directives );
234
- errors .addAll (ruleErrors );
235
- ix ++;
236
- }
237
- return errors ;
238
- }
239
-
240
154
241
155
/**
242
156
* Returns true of the input type is one of the specified scalar types, regardless of non null ness
0 commit comments