@@ -121,12 +121,12 @@ The following shows how to setup the SDL generation so that the build schema wil
121121 //
122122 // This contains by default the standard library provided @Directive constraints
123123 //
124- PossibleValidationRules possibleRules = PossibleValidationRules.newPossibleRules ()
124+ ValidationRules validationRules = ValidationRules.newValidationRules ()
125125 .onValidationErrorStrategy(OnValidationErrorStrategy.RETURN_NULL)
126126 .build();
127127 //
128128 // This will rewrite your data fetchers when rules apply to them so that validation
129- ValidationSchemaWiring schemaWiring = new ValidationSchemaWiring(possibleRules );
129+ ValidationSchemaWiring schemaWiring = new ValidationSchemaWiring(validationRules );
130130 //
131131 // we add this schema wiring to the graphql runtime
132132 RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().directiveWiring(schemaWiring).build();
@@ -149,62 +149,69 @@ write your own strategy if you want.
149149
150150We recommend that you use the SDL schema directive wiring and @directives for the easiest way to get input type validation.
151151
152- However there can be reasons why you cant use this approach and you have to manually use the API directly in your data fetching code.
152+ However there can be reasons why you cant use this approach and you have use the API directly in your data fetching code.
153153
154154``` java
155155
156- //
157- // an example of writing your own custom validation rule
158- //
159- ValidationRule myCustomValidationRule = new ValidationRule () {
160- @Override
161- public boolean appliesTo (GraphQLFieldDefinition fieldDefinition , GraphQLFieldsContainer fieldsContainer ) {
162- return fieldDefinition. getName(). equals(" decide whether this rule applies here" );
163- }
164-
165- @Override
166- public boolean appliesTo (GraphQLArgument argument , GraphQLFieldDefinition fieldDefinition , GraphQLFieldsContainer fieldsContainer ) {
167- return argument. getName(). equals(" decide whether this rule applies here to an argument" );
168- }
169-
170- @Override
171- public List<GraphQLError > runValidation (ValidationEnvironment validationEnvironment ) {
172-
173- List<GraphQLError > errors = new ArrayList<> ();
174- Map<String , Object > argumentValues = validationEnvironment. getArgumentValues();
175- for (String argName : argumentValues. keySet()) {
176- Object argValue = argumentValues. get(argName);
177- GraphQLError error = runCodeThatValidatesInputHere(validationEnvironment, argName, argValue);
178- if (error != null ) {
179- errors. add(error);
180- }
181- }
182- return errors;
183- }
184- };
185-
186- PossibleValidationRules possibleValidationRules = PossibleValidationRules
187- .newPossibleRules()
188- .addRule(myCustomValidationRule)
189- .build();
190-
191- DataFetcher dataFetcher = new DataFetcher () {
192- @Override
193- public Object get (DataFetchingEnvironment env ) {
194-
195- List<GraphQLError > errors = possibleValidationRules. runValidationRules(env);
196- if (! errors. isEmpty()) {
197- return DataFetcherResult . newResult(). errors(errors). data(null ). build();
198- }
199-
200- return normalDataFetchingCodeRunsNow(env);
201- }
202- };
156+ //
157+ // an example of writing your own custom validation rule
158+ //
159+ ValidationRule myCustomValidationRule = new ValidationRule () {
160+ @Override
161+ public boolean appliesTo (GraphQLFieldDefinition fieldDefinition , GraphQLFieldsContainer fieldsContainer ) {
162+ return fieldDefinition. getName(). equals(" decide whether this rule applies here" );
163+ }
164+
165+ @Override
166+ public boolean appliesTo (GraphQLArgument argument , GraphQLFieldDefinition fieldDefinition , GraphQLFieldsContainer fieldsContainer ) {
167+ return argument. getName(). equals(" decide whether this rule applies here to an argument" );
168+ }
169+
170+ @Override
171+ public List<GraphQLError > runValidation (ValidationEnvironment validationEnvironment ) {
172+
173+ List<GraphQLError > errors = new ArrayList<> ();
174+ Map<String , Object > argumentValues = validationEnvironment. getArgumentValues();
175+ for (String argName : argumentValues. keySet()) {
176+ Object argValue = argumentValues. get(argName);
177+ GraphQLError error = runCodeThatValidatesInputHere(validationEnvironment, argName, argValue);
178+ if (error != null ) {
179+ errors. add(error);
180+ }
181+ }
182+ return errors;
183+ }
184+ };
185+
186+ DataFetcher dataFetcher = new DataFetcher () {
187+ @Override
188+ public Object get (DataFetchingEnvironment env ) {
189+
190+ //
191+ // By default the ValidationRule contains the SDL @directive rules, but
192+ // you can also add your own as we do here.
193+ //
194+ ValidationRules validationRules = ValidationRules
195+ .newValidationRules()
196+ .locale(Locale . getDefault())
197+ .addRule(myCustomValidationRule)
198+ .build();
199+
200+ //
201+ // The expected strategy is to return null data and the errors if there are any validation
202+ // problems
203+ //
204+ List<GraphQLError > errors = validationRules. runValidationRules(env);
205+ if (! errors. isEmpty()) {
206+ return DataFetcherResult . newResult(). errors(errors). data(null ). build();
207+ }
208+ return normalDataFetchingCodeRunsNow(env);
209+ }
210+ };
203211```
204212
205213The above code shows a custom validation rule (with nonsense logic for demonstration purposes) and then a data fetcher
206- that uses the ` PossibleValidationRules ` API to run validation rules. Its is called "possible" because it can contain more rules that may
207- or may apply to a field, argument or parent field container.
214+ that uses the ` ValidationRules ` API to run validation rules.
208215
209216
210217## The supplied @Directive constraints
0 commit comments