|
1 | 1 | package graphql.validation.constraints;
|
2 | 2 |
|
3 |
| -import graphql.Assert; |
4 |
| -import graphql.GraphQLError; |
5 | 3 | import graphql.PublicApi;
|
6 |
| -import graphql.execution.ExecutionPath; |
7 | 4 | import graphql.schema.GraphQLArgument;
|
8 |
| -import graphql.schema.GraphQLDirective; |
9 |
| -import graphql.schema.GraphQLDirectiveContainer; |
10 | 5 | import graphql.schema.GraphQLFieldDefinition;
|
11 | 6 | import graphql.schema.GraphQLFieldsContainer;
|
12 |
| -import graphql.schema.GraphQLInputObjectField; |
13 |
| -import graphql.schema.GraphQLInputObjectType; |
14 |
| -import graphql.schema.GraphQLInputType; |
15 |
| -import graphql.schema.GraphQLList; |
16 |
| -import graphql.schema.GraphQLTypeUtil; |
17 |
| -import graphql.util.FpKit; |
18 | 7 | import graphql.validation.constraints.standard.ArgumentsConstraint;
|
19 | 8 | import graphql.validation.constraints.standard.AssertFalseConstraint;
|
20 | 9 | import graphql.validation.constraints.standard.AssertTrueConstraint;
|
|
32 | 21 | import graphql.validation.constraints.standard.PositiveOrZeroConstraint;
|
33 | 22 | import graphql.validation.constraints.standard.RangeConstraint;
|
34 | 23 | import graphql.validation.constraints.standard.SizeConstraint;
|
35 |
| -import graphql.validation.rules.ValidationEnvironment; |
36 |
| -import graphql.validation.rules.ValidationRule; |
37 |
| -import graphql.validation.util.Util; |
38 | 24 |
|
39 |
| -import java.util.ArrayList; |
40 | 25 | import java.util.Arrays;
|
41 | 26 | import java.util.Collections;
|
42 | 27 | import java.util.LinkedHashMap;
|
43 | 28 | import java.util.List;
|
44 | 29 | import java.util.Map;
|
45 | 30 |
|
| 31 | +import static java.util.stream.Collectors.toList; |
| 32 | + |
46 | 33 | /**
|
47 |
| - * This contains a liszt of {@link graphql.validation.constraints.DirectiveConstraint}s and |
48 |
| - * runs them as a group on a field and its argument values. |
| 34 | + * This contains a map of {@link graphql.validation.constraints.DirectiveConstraint}s and helps |
| 35 | + * run them against a specific field or argument |
49 | 36 | * <p>
|
50 | 37 | * This ships with a set of standard constraints via {@link #STANDARD_CONSTRAINTS} but you can
|
51 | 38 | * add your own implementations if you wish
|
52 | 39 | */
|
53 | 40 | @PublicApi
|
54 |
| -public class DirectiveConstraints implements ValidationRule { |
| 41 | +public class DirectiveConstraints { |
55 | 42 |
|
56 | 43 | /**
|
57 | 44 | * These are the standard directive rules that come with the system
|
@@ -98,141 +85,20 @@ public String getDirectivesSDL() {
|
98 | 85 | return sb.toString();
|
99 | 86 | }
|
100 | 87 |
|
101 |
| - @Override |
102 |
| - public boolean appliesTo(GraphQLFieldDefinition fieldDefinition, GraphQLFieldsContainer fieldsContainer) { |
103 |
| - for (DirectiveConstraint directiveRule : constraints.values()) { |
104 |
| - boolean applies = directiveRule.appliesTo(fieldDefinition, fieldsContainer); |
105 |
| - if (applies) { |
106 |
| - return true; |
107 |
| - } |
108 |
| - } |
109 |
| - return false; |
110 |
| - } |
111 |
| - |
112 |
| - @Override |
113 |
| - public boolean appliesTo(GraphQLArgument argument, GraphQLFieldDefinition fieldDefinition, GraphQLFieldsContainer fieldsContainer) { |
114 |
| - for (DirectiveConstraint directiveRule : constraints.values()) { |
115 |
| - boolean applies = directiveRule.appliesTo(argument, fieldDefinition, fieldsContainer); |
116 |
| - if (applies) { |
117 |
| - return true; |
118 |
| - } |
119 |
| - } |
120 |
| - return false; |
121 |
| - } |
122 |
| - |
123 |
| - @SuppressWarnings("unchecked") |
124 |
| - @Override |
125 |
| - public List<GraphQLError> runValidation(ValidationEnvironment validationEnvironment) { |
126 |
| - |
127 |
| - GraphQLArgument argument = validationEnvironment.getArgument(); |
128 |
| - Object validatedValue = validationEnvironment.getValidatedValue(); |
129 |
| - List<GraphQLDirective> directives = argument.getDirectives(); |
130 |
| - |
131 |
| - // |
132 |
| - // all the directives validation code does NOT care for NULL ness since the graphql engine covers that. |
133 |
| - // eg a @NonNull validation directive makes no sense in graphql like it might in Java |
134 |
| - // |
135 |
| - GraphQLInputType inputType = Util.unwrapNonNull(validationEnvironment.getFieldOrArgumentType()); |
136 |
| - validationEnvironment = validationEnvironment.transform(b -> b.fieldOrArgumentType(inputType)); |
137 |
| - |
138 |
| - return runValidationImpl(validationEnvironment, inputType, validatedValue, directives); |
139 |
| - } |
140 |
| - |
141 |
| - @SuppressWarnings("unchecked") |
142 |
| - private List<GraphQLError> runValidationImpl(ValidationEnvironment validationEnvironment, GraphQLInputType inputType, Object validatedValue, List<GraphQLDirective> directives) { |
143 |
| - List<GraphQLError> errors = new ArrayList<>(); |
144 |
| - for (GraphQLDirective directive : directives) { |
145 |
| - DirectiveConstraint validationRule = constraints.get(directive.getName()); |
146 |
| - if (validationRule == null) { |
147 |
| - continue; |
148 |
| - } |
149 |
| - |
150 |
| - validationEnvironment = validationEnvironment.transform(b -> b.context(GraphQLDirective.class, directive)); |
151 |
| - // |
152 |
| - // now run the directive rule with this directive instance |
153 |
| - List<GraphQLError> ruleErrors = validationRule.runValidation(validationEnvironment); |
154 |
| - errors.addAll(ruleErrors); |
155 |
| - } |
156 |
| - |
157 |
| - if (validatedValue == null) { |
158 |
| - return errors; |
159 |
| - } |
160 |
| - |
161 |
| - inputType = (GraphQLInputType) GraphQLTypeUtil.unwrapNonNull(inputType); |
162 |
| - |
163 |
| - if (GraphQLTypeUtil.isList(inputType)) { |
164 |
| - List<Object> values = new ArrayList<>(FpKit.toCollection(validatedValue)); |
165 |
| - List<GraphQLError> ruleErrors = walkListArg(validationEnvironment, (GraphQLList) inputType, values); |
166 |
| - errors.addAll(ruleErrors); |
167 |
| - } |
168 |
| - |
169 |
| - if (inputType instanceof GraphQLInputObjectType) { |
170 |
| - if (validatedValue instanceof Map) { |
171 |
| - Map<String, Object> objectValue = (Map<String, Object>) validatedValue; |
172 |
| - List<GraphQLError> ruleErrors = walkObjectArg(validationEnvironment, (GraphQLInputObjectType) inputType, objectValue); |
173 |
| - errors.addAll(ruleErrors); |
174 |
| - } else { |
175 |
| - Assert.assertShouldNeverHappen("How can there be a `input` object type '%s' that does not have a matching Map java value", GraphQLTypeUtil.simplePrint(inputType)); |
176 |
| - } |
177 |
| - } |
178 |
| - return errors; |
179 |
| - } |
180 |
| - |
181 |
| - private List<GraphQLError> walkObjectArg(ValidationEnvironment validationEnvironment, GraphQLInputObjectType argumentType, Map<String, Object> objectMap) { |
182 |
| - List<GraphQLError> errors = new ArrayList<>(); |
183 |
| - |
184 |
| - for (GraphQLInputObjectField inputField : argumentType.getFieldDefinitions()) { |
185 |
| - |
186 |
| - GraphQLInputType fieldType = inputField.getType(); |
187 |
| - List<GraphQLDirective> directives = inputField.getDirectives(); |
188 |
| - Object validatedValue = objectMap.getOrDefault(inputField.getName(), inputField.getDefaultValue()); |
189 |
| - if (validatedValue == null) { |
190 |
| - continue; |
191 |
| - } |
192 |
| - |
193 |
| - ExecutionPath fieldOrArgPath = validationEnvironment.getFieldOrArgumentPath().segment(inputField.getName()); |
194 |
| - |
195 |
| - ValidationEnvironment newValidationEnvironment = validationEnvironment.transform(builder -> builder |
196 |
| - .fieldOrArgumentPath(fieldOrArgPath) |
197 |
| - .validatedValue(validatedValue) |
198 |
| - .fieldOrArgumentType(fieldType) |
199 |
| - ); |
200 |
| - |
201 |
| - List<GraphQLError> ruleErrors = runValidationImpl(newValidationEnvironment, fieldType, validatedValue, directives); |
202 |
| - errors.addAll(ruleErrors); |
203 |
| - } |
204 |
| - return errors; |
| 88 | + public List<DirectiveConstraint> whichApplyTo(GraphQLFieldDefinition fieldDefinition, GraphQLFieldsContainer fieldsContainer) { |
| 89 | + return constraints.values() |
| 90 | + .stream() |
| 91 | + .filter(c -> c.appliesTo(fieldDefinition, fieldsContainer)) |
| 92 | + .collect(toList()); |
205 | 93 | }
|
206 | 94 |
|
207 |
| - private List<GraphQLError> walkListArg(ValidationEnvironment validationEnvironment, GraphQLList argumentType, List<Object> objectList) { |
208 |
| - List<GraphQLError> errors = new ArrayList<>(); |
209 |
| - |
210 |
| - GraphQLInputType listItemType = Util.unwrapOneAndAllNonNull(argumentType); |
211 |
| - List<GraphQLDirective> directives; |
212 |
| - if (!(listItemType instanceof GraphQLDirectiveContainer)) { |
213 |
| - directives = Collections.emptyList(); |
214 |
| - } else { |
215 |
| - directives = ((GraphQLDirectiveContainer) listItemType).getDirectives(); |
216 |
| - } |
217 |
| - int ix = 0; |
218 |
| - for (Object value : objectList) { |
219 |
| - |
220 |
| - ExecutionPath fieldOrArgPath = validationEnvironment.getFieldOrArgumentPath().segment(ix); |
221 |
| - |
222 |
| - ValidationEnvironment newValidationEnvironment = validationEnvironment.transform(builder -> builder |
223 |
| - .fieldOrArgumentPath(fieldOrArgPath) |
224 |
| - .validatedValue(value) |
225 |
| - .fieldOrArgumentType(listItemType) |
226 |
| - ); |
227 |
| - |
228 |
| - List<GraphQLError> ruleErrors = runValidationImpl(newValidationEnvironment, listItemType, value, directives); |
229 |
| - errors.addAll(ruleErrors); |
230 |
| - ix++; |
231 |
| - } |
232 |
| - return errors; |
| 95 | + public List<DirectiveConstraint> whichApplyTo(GraphQLArgument argument, GraphQLFieldDefinition fieldDefinition, GraphQLFieldsContainer fieldsContainer) { |
| 96 | + return constraints.values() |
| 97 | + .stream() |
| 98 | + .filter(c -> c.appliesTo(argument, fieldDefinition, fieldsContainer)) |
| 99 | + .collect(toList()); |
233 | 100 | }
|
234 | 101 |
|
235 |
| - |
236 | 102 | public static class Builder {
|
237 | 103 | private Map<String, DirectiveConstraint> directiveRules = new LinkedHashMap<>();
|
238 | 104 |
|
|
0 commit comments