@@ -5,22 +5,17 @@ This library provides extended validation of fields and field arguments for [gra
55
66# Status
77
8- This code is currently under construction. There is no release artifact and not all parts of it are ready.
8+ This code is currently under construction. It is fairly complete in providing powerful validation
9+ but as it has NOT be consumed by a production like project then its API usefulness has not been tested
10+ and its code has not been battle tested.
911
1012But the project welcomes all feedback and input on code design and validation requirements.
1113
12- It currently passes MOST of its tests - but not all. This is a matter of attention and time.
13-
14- It has NOT yet been consumed by a production like project and hence its API usefulness has not been tested
15-
16- # Using the code
17-
18- TODO
19-
2014
2115# SDL @Directive constraints
2216
23- This library a series of directives that can be applied to field arguments and input type fields which will constrain their allowable values.
17+ This library provides a series of directives that can be applied to field arguments and input type fields which will
18+ constrain their allowable values.
2419
2520These names and semantics are inspired from the javax.validation annotations
2621
@@ -144,6 +139,68 @@ considered valid.
144139The default strategy ` OnValidationErrorStrategy.RETURN_NULL ` will return null for the field input if it is not considered valid. You can
145140write your own strategy if you want.
146141
142+ ## Using the API direct in your own data fetchers
143+
144+ We recommend that you use the SDL schema directive wiring and @directives for the easiest way to get input type validation.
145+
146+ 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.
147+
148+ ``` java
149+
150+ //
151+ // an example of writing your own custom validation rule
152+ //
153+ ValidationRule myCustomValidationRule = new ValidationRule () {
154+ @Override
155+ public boolean appliesTo (GraphQLFieldDefinition fieldDefinition , GraphQLFieldsContainer fieldsContainer ) {
156+ return fieldDefinition. getName(). equals(" decide whether this rule applies here" );
157+ }
158+
159+ @Override
160+ public boolean appliesTo (GraphQLArgument argument , GraphQLFieldDefinition fieldDefinition , GraphQLFieldsContainer fieldsContainer ) {
161+ return argument. getName(). equals(" decide whether this rule applies here to an argument" );
162+ }
163+
164+ @Override
165+ public List<GraphQLError > runValidation (ValidationEnvironment validationEnvironment ) {
166+
167+ List<GraphQLError > errors = new ArrayList<> ();
168+ Map<String , Object > argumentValues = validationEnvironment. getArgumentValues();
169+ for (String argName : argumentValues. keySet()) {
170+ Object argValue = argumentValues. get(argName);
171+ GraphQLError error = runCodeThatValidatesInputHere(validationEnvironment, argName, argValue);
172+ if (error != null ) {
173+ errors. add(error);
174+ }
175+ }
176+ return errors;
177+ }
178+ };
179+
180+ PossibleValidationRules possibleValidationRules = PossibleValidationRules
181+ .newPossibleRules()
182+ .addRule(myCustomValidationRule)
183+ .build();
184+
185+ DataFetcher dataFetcher = new DataFetcher () {
186+ @Override
187+ public Object get (DataFetchingEnvironment env ) {
188+
189+ List<GraphQLError > errors = possibleValidationRules. runValidationRules(env);
190+ if (! errors. isEmpty()) {
191+ return DataFetcherResult . newResult(). errors(errors). data(null ). build();
192+ }
193+
194+ return normalDataFetchingCodeRunsNow(env);
195+ }
196+ };
197+ ```
198+
199+ The above code shows a custom validation rule (with nonsense logic for demonstration purposes) and then a data fetcher
200+ that uses the ` PossibleValidationRules ` API to run validation rules. Its is called "possible" because it can contain more rules that may
201+ or may apply to a field, argument or parent field container.
202+
203+
147204## The supplied @Directive constraints
148205
149206<!-- generated by DocHelper on 2019-08-17T11:55:22.933Z -->
0 commit comments