44import graphql .GraphQLError ;
55import graphql .PublicSpi ;
66import graphql .Scalars ;
7- import graphql .execution .ExecutionPath ;
87import graphql .schema .GraphQLArgument ;
98import graphql .schema .GraphQLDirective ;
10- import graphql .schema .GraphQLDirectiveContainer ;
119import graphql .schema .GraphQLFieldDefinition ;
1210import graphql .schema .GraphQLFieldsContainer ;
13- import graphql .schema .GraphQLInputObjectField ;
1411import graphql .schema .GraphQLInputObjectType ;
1512import graphql .schema .GraphQLInputType ;
16- import graphql .schema .GraphQLList ;
1713import graphql .schema .GraphQLScalarType ;
1814import graphql .schema .GraphQLTypeUtil ;
19- import graphql .util .FpKit ;
2015import graphql .validation .rules .ValidationEnvironment ;
2116import graphql .validation .util .DirectivesAndTypeWalker ;
2217import graphql .validation .util .Util ;
2520import java .math .BigDecimal ;
2621import java .util .ArrayList ;
2722import java .util .Collection ;
28- import java .util .Collections ;
2923import java .util .LinkedHashMap ;
3024import java .util .List ;
3125import java .util .Map ;
3226
3327import static graphql .schema .GraphQLTypeUtil .isList ;
3428import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .FIELD ;
35- import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .INPUT_OBJECT_FIELD ;
3629import static java .util .Collections .singletonList ;
3730
3831@ SuppressWarnings ("UnnecessaryLocalVariable" )
@@ -115,9 +108,7 @@ public List<GraphQLError> runValidation(ValidationEnvironment validationEnvironm
115108 return runFieldValidationImpl (validationEnvironment );
116109 }
117110
118- GraphQLArgument argument = validationEnvironment .getArgument ();
119111 Object validatedValue = validationEnvironment .getValidatedValue ();
120- List <GraphQLDirective > directives = argument == null ? Collections .emptyList () : argument .getDirectives ();
121112
122113 //
123114 // 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
126117 GraphQLInputType inputType = Util .unwrapNonNull (validationEnvironment .getValidatedType ());
127118 validationEnvironment = validationEnvironment .transform (b -> b .validatedType (inputType ));
128119
129- return runValidationImpl (validationEnvironment , inputType , validatedValue , directives );
120+ return runValidationImpl (validationEnvironment , inputType , validatedValue );
130121 }
131122
132123 private List <GraphQLError > runFieldValidationImpl (ValidationEnvironment validationEnvironment ) {
133- return runConstraintOnDirectives (validationEnvironment , validationEnvironment . getFieldDefinition (). getDirectives () );
124+ return runConstraintOnDirectives (validationEnvironment );
134125 }
135126
136127 @ 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 );
161130 }
162131
163- private List <GraphQLError > runConstraintOnDirectives (ValidationEnvironment validationEnvironment , List <GraphQLDirective > directives ) {
132+ private List <GraphQLError > runConstraintOnDirectives (ValidationEnvironment validationEnvironment ) {
133+
164134 List <GraphQLError > errors = new ArrayList <>();
135+ List <GraphQLDirective > directives = validationEnvironment .getDirectives ();
165136 directives = Util .sort (directives , GraphQLDirective ::getName );
166137
167138 for (GraphQLDirective directive : directives ) {
@@ -180,63 +151,6 @@ private List<GraphQLError> runConstraintOnDirectives(ValidationEnvironment valid
180151 return errors ;
181152 }
182153
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-
240154
241155 /**
242156 * Returns true of the input type is one of the specified scalar types, regardless of non null ness
0 commit comments