11package graphql.validation.schemawiring;
22
3- import graphql.GraphQLError;
43import graphql.PublicApi;
54import graphql.schema.DataFetcher;
65import graphql.schema.GraphQLFieldDefinition;
1110import graphql.validation.rules.OnValidationErrorStrategy;
1211import graphql.validation.rules.TargetedValidationRules;
1312import graphql.validation.rules.ValidationRules;
14- import graphql.validation.util.Util;
1513
16- import java.util.List;
1714import java.util.Locale;
1815
1916/**
20- * A {@link graphql.schema.idl. SchemaDirectiveWiring} that can be used to inject validation rules into the data fetchers
17+ * A {@link SchemaDirectiveWiring} that can be used to inject validation rules into the data fetchers
2118 * when the graphql schema is being built. It will use the validation rules and ask each one of they apply to the field and or its
2219 * arguments.
2320 * <p>
24- * If there are rules that apply then it will it will change the {@link graphql.schema. DataFetcher} of that field so that rules get run
21+ * If there are rules that apply then it will it will change the {@link DataFetcher} of that field so that rules get run
2522 * BEFORE the original field fetch is run.
2623 */
2724@PublicApi
@@ -38,40 +35,29 @@ public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFi
3835 GraphQLFieldsContainer fieldsContainer = env.getFieldsContainer();
3936 GraphQLFieldDefinition fieldDefinition = env.getFieldDefinition();
4037
41- TargetedValidationRules rules = ruleCandidates.buildRulesFor(fieldDefinition, fieldsContainer);
42- if (rules.isEmpty()) {
43- return fieldDefinition; // no rules - no validation needed
44- }
45-
4638 OnValidationErrorStrategy errorStrategy = ruleCandidates.getOnValidationErrorStrategy();
4739 MessageInterpolator messageInterpolator = ruleCandidates.getMessageInterpolator();
4840 Locale locale = ruleCandidates.getLocale();
4941
50- final DataFetcher currentDF = env.getCodeRegistry().getDataFetcher(fieldsContainer, fieldDefinition);
51- final DataFetcher newDF = buildValidatingDataFetcher(rules, errorStrategy, messageInterpolator, currentDF, locale);
42+ final DataFetcher<?> currentDF = env.getCodeRegistry().getDataFetcher(fieldsContainer, fieldDefinition);
43+ final DataFetcher<?> newDF = buildValidatingDataFetcher(errorStrategy, messageInterpolator, currentDF, locale);
5244
5345 env.getCodeRegistry().dataFetcher(fieldsContainer, fieldDefinition, newDF);
5446
5547 return fieldDefinition;
5648 }
5749
58- private DataFetcher buildValidatingDataFetcher(TargetedValidationRules rules, OnValidationErrorStrategy errorStrategy, MessageInterpolator messageInterpolator, DataFetcher currentDF, final Locale defaultLocale) {
59- // ok we have some rules that need to be applied to this field and its arguments
60- return environment -> {
61- List<GraphQLError> errors = rules.runValidationRules(environment, messageInterpolator, defaultLocale);
62- if (!errors.isEmpty()) {
63- // should we continue?
64- if (!errorStrategy.shouldContinue(errors, environment)) {
65- return errorStrategy.onErrorValue(errors, environment);
66- }
67- }
68- // we have no validation errors or they said continue so call the underlying data fetcher
69- Object returnValue = currentDF.get(environment);
70- if (errors.isEmpty()) {
71- return returnValue;
72- }
73- return Util.mkDFRFromFetchedResult(errors, returnValue);
74- };
50+ private DataFetcher<Object> buildValidatingDataFetcher(OnValidationErrorStrategy errorStrategy,
51+ MessageInterpolator messageInterpolator,
52+ DataFetcher<?> currentDF,
53+ final Locale defaultLocale) {
54+ return new FieldValidatorDataFetcher(
55+ errorStrategy,
56+ messageInterpolator,
57+ currentDF,
58+ defaultLocale,
59+ ruleCandidates
60+ );
7561 }
7662
7763}
0 commit comments