Skip to content

Commit 5c4aad8

Browse files
committed
Added Java example code
1 parent acd55e2 commit 5c4aad8

File tree

3 files changed

+113
-14
lines changed

3 files changed

+113
-14
lines changed

readme.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,43 @@ will use that as a resource bundle lookup key. This too is inspired by the java
106106
107107
Like javax.validation, this library ships with some default error message templates but you can override them.
108108
109+
# Schema Directive Wiring
110+
111+
If you are using graphql SDL to build your graphql schema then you can use a `ValidationSchemaWiring` class to automatically
112+
change your field data fetchers so that validation rules are called before the field data is fetched.
113+
114+
This allows you to automatically enhance your schema with validation by directives alone.
115+
116+
The following shows how to setup the SDL generation so that the build schema will have validation in place.
117+
118+
119+
```java
120+
//
121+
// This contains by default the standard library provided @Directive constraints
122+
//
123+
PossibleValidationRules possibleRules = PossibleValidationRules.newPossibleRules()
124+
.onValidationErrorStrategy(OnValidationErrorStrategy.RETURN_NULL)
125+
.build();
126+
//
127+
// This will rewrite your data fetchers when rules apply to them so that validation
128+
ValidationSchemaWiring schemaWiring = new ValidationSchemaWiring(possibleRules);
129+
//
130+
// we add this schema wiring to the graphql runtime
131+
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().directiveWiring(schemaWiring).build();
132+
//
133+
// then pretty much standard graphql-java code to build a graphql schema
134+
Reader sdl = buildSDL();
135+
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(sdl);
136+
GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
137+
```
109138

110-
# Complex input types
111-
112-
You can put @Directive constraints on complex input types as well as simple field arguments
113-
114-
```graphql
115-
input ProductItem {
116-
code : String @Size(max : 5)
117-
price : String @Size(max : 3)
118-
}
139+
Under the covers `ValidationSchemaWiring` asks each possible rule if it applies to a field as they are encountered (at schema build time).
119140

120-
type Mutation {
121-
updateProduct( product : ID, items : [ProductItem]) : Product
122-
}
123-
```
141+
If they do apply then it rewrites the DataFetcher so that it first calls the validation code and produces errors if the field input is not
142+
considered valid.
124143

125-
In the example above each `ProductItem` in the list of items is examined for valid values
144+
The default strategy `OnValidationErrorStrategy.RETURN_NULL` will return null for the field input if it is not considered valid. You can
145+
write your own strategy if you want.
126146

127147
## The supplied @Directive constraints
128148

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package examples;
2+
3+
import graphql.ExecutionInput;
4+
import graphql.ExecutionResult;
5+
import graphql.GraphQL;
6+
import graphql.schema.GraphQLSchema;
7+
import graphql.schema.idl.RuntimeWiring;
8+
import graphql.schema.idl.SchemaGenerator;
9+
import graphql.schema.idl.SchemaParser;
10+
import graphql.schema.idl.TypeDefinitionRegistry;
11+
import graphql.validation.rules.OnValidationErrorStrategy;
12+
import graphql.validation.rules.PossibleValidationRules;
13+
import graphql.validation.schemawiring.ValidationSchemaWiring;
14+
15+
import java.io.InputStream;
16+
import java.io.InputStreamReader;
17+
import java.io.Reader;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
@SuppressWarnings("UnnecessaryLocalVariable")
22+
public class SchemaWiringExample {
23+
24+
public static void main(String[] args) {
25+
26+
try {
27+
GraphQLSchema graphQLSchema = buildSchemaAndWiring();
28+
GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();
29+
30+
Map<String, Object> variables = new HashMap<>();
31+
variables.put("x", "y");
32+
33+
ExecutionInput ei = ExecutionInput.newExecutionInput()
34+
.query("{field}")
35+
.variables(variables)
36+
.build();
37+
38+
ExecutionResult result = graphQL.execute(ei);
39+
40+
System.out.println(result);
41+
} catch (RuntimeException rte) {
42+
rte.printStackTrace(System.out);
43+
}
44+
45+
}
46+
47+
private static GraphQLSchema buildSchemaAndWiring() {
48+
//
49+
// This contains by default the standard library provided @Directive constraints
50+
//
51+
PossibleValidationRules possibleRules = PossibleValidationRules.newPossibleRules()
52+
.onValidationErrorStrategy(OnValidationErrorStrategy.RETURN_NULL)
53+
.build();
54+
//
55+
// This will rewrite your data fetchers when rules apply to them so that validation
56+
ValidationSchemaWiring schemaWiring = new ValidationSchemaWiring(possibleRules);
57+
//
58+
// we add this schema wiring to the graphql runtime
59+
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().directiveWiring(schemaWiring).build();
60+
//
61+
// then pretty much standard graphql-java code to build a graphql schema
62+
Reader sdl = buildSDL();
63+
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(sdl);
64+
GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
65+
66+
return graphQLSchema;
67+
}
68+
69+
private static Reader buildSDL() {
70+
InputStream is = SchemaWiringExample.class.getResourceAsStream("/examples/example.graphqls");
71+
return new InputStreamReader(is);
72+
}
73+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
4+
type Query {
5+
field : String
6+
}

0 commit comments

Comments
 (0)