Skip to content

Commit 08882b0

Browse files
committed
add test for SchemaValidator
1 parent 959174e commit 08882b0

File tree

2 files changed

+136
-9
lines changed

2 files changed

+136
-9
lines changed

src/main/java/graphql/schema/validation/SchemaValidator.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.schema.validation;
22

3+
import graphql.Internal;
34
import graphql.schema.GraphQLFieldDefinition;
45
import graphql.schema.GraphQLFieldsContainer;
56
import graphql.schema.GraphQLOutputType;
@@ -11,22 +12,30 @@
1112
import java.util.List;
1213
import java.util.Set;
1314

15+
@Internal
1416
public class SchemaValidator {
1517

1618
private final Set<GraphQLOutputType> processed = new HashSet<>();
1719

18-
public Set<SchemaValidationError> validateSchema(GraphQLSchema schema) {
19-
SchemaValidationErrorCollector validationErrorCollector = new SchemaValidationErrorCollector();
20-
List<SchemaValidationRule> rules = new ArrayList<>();
20+
private List<SchemaValidationRule> rules = new ArrayList<>();
21+
22+
public SchemaValidator() {
2123
rules.add(new NoUnbrokenInputCycles());
2224
rules.add(new ObjectsImplementInterfaces());
25+
}
2326

24-
List<GraphQLType> types = schema.getAllTypesAsList();
25-
types.forEach(type -> {
26-
for (SchemaValidationRule rule : rules) {
27-
rule.check(type, validationErrorCollector);
28-
}
29-
});
27+
SchemaValidator(List<SchemaValidationRule> rules) {
28+
this.rules = rules;
29+
}
30+
31+
public List<SchemaValidationRule> getRules() {
32+
return rules;
33+
}
34+
35+
public Set<SchemaValidationError> validateSchema(GraphQLSchema schema) {
36+
SchemaValidationErrorCollector validationErrorCollector = new SchemaValidationErrorCollector();
37+
38+
checkTypes(schema, validationErrorCollector);
3039

3140
traverse(schema.getQueryType(), rules, validationErrorCollector);
3241
if (schema.isSupportingMutations()) {
@@ -38,6 +47,15 @@ public Set<SchemaValidationError> validateSchema(GraphQLSchema schema) {
3847
return validationErrorCollector.getErrors();
3948
}
4049

50+
private void checkTypes(GraphQLSchema schema, SchemaValidationErrorCollector validationErrorCollector) {
51+
List<GraphQLType> types = schema.getAllTypesAsList();
52+
types.forEach(type -> {
53+
for (SchemaValidationRule rule : rules) {
54+
rule.check(type, validationErrorCollector);
55+
}
56+
});
57+
}
58+
4159
private void traverse(GraphQLOutputType root, List<SchemaValidationRule> rules, SchemaValidationErrorCollector validationErrorCollector) {
4260
if (processed.contains(root)) {
4361
return;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package graphql.schema.validation
2+
3+
import graphql.Scalars
4+
import graphql.schema.GraphQLFieldDefinition
5+
import graphql.schema.GraphQLObjectType
6+
import graphql.schema.GraphQLSchema
7+
import spock.lang.Specification
8+
9+
class SchemaValidatorTest extends Specification {
10+
11+
12+
def "check used rules"() {
13+
when:
14+
def validator = new SchemaValidator()
15+
def rules = validator.rules
16+
then:
17+
rules.size() == 2
18+
rules[0] instanceof NoUnbrokenInputCycles
19+
rules[1] instanceof ObjectsImplementInterfaces
20+
}
21+
22+
def "rules are used"() {
23+
def queryType = GraphQLObjectType.newObject()
24+
.name("query")
25+
.build()
26+
def schema = GraphQLSchema.newSchema()
27+
.query(queryType)
28+
.build()
29+
def dummyRule = Mock(SchemaValidationRule)
30+
when:
31+
def validator = new SchemaValidator([dummyRule])
32+
validator.validateSchema(schema);
33+
34+
then:
35+
1 * dummyRule.check(queryType, _ as SchemaValidationErrorCollector)
36+
}
37+
38+
def "query fields are checked"() {
39+
def field = GraphQLFieldDefinition.newFieldDefinition()
40+
.name("field")
41+
.type(Scalars.GraphQLString)
42+
.build()
43+
def queryType = GraphQLObjectType.newObject()
44+
.name("query")
45+
.field(field)
46+
.build()
47+
def schema = GraphQLSchema.newSchema()
48+
.query(queryType)
49+
.build()
50+
def dummyRule = Mock(SchemaValidationRule)
51+
when:
52+
def validator = new SchemaValidator([dummyRule])
53+
validator.validateSchema(schema);
54+
55+
then:
56+
1 * dummyRule.check(field, _ as SchemaValidationErrorCollector)
57+
}
58+
59+
def "mutation fields are checked"() {
60+
def dummyRule = Mock(SchemaValidationRule)
61+
def field = GraphQLFieldDefinition.newFieldDefinition()
62+
.name("field")
63+
.type(Scalars.GraphQLString)
64+
.build()
65+
def mutation = GraphQLObjectType.newObject()
66+
.name("mutation")
67+
.field(field)
68+
.build()
69+
def queryType = GraphQLObjectType.newObject()
70+
.name("query")
71+
.build()
72+
def schema = GraphQLSchema.newSchema()
73+
.query(queryType)
74+
.mutation(mutation)
75+
.build()
76+
when:
77+
def validator = new SchemaValidator([dummyRule])
78+
validator.validateSchema(schema);
79+
80+
then:
81+
1 * dummyRule.check(field, _ as SchemaValidationErrorCollector)
82+
}
83+
84+
def "subscription fields are checked"() {
85+
def dummyRule = Mock(SchemaValidationRule)
86+
def field = GraphQLFieldDefinition.newFieldDefinition()
87+
.name("field")
88+
.type(Scalars.GraphQLString)
89+
.build()
90+
def mutation = GraphQLObjectType.newObject()
91+
.name("subscription")
92+
.field(field)
93+
.build()
94+
def queryType = GraphQLObjectType.newObject()
95+
.name("query")
96+
.build()
97+
def schema = GraphQLSchema.newSchema()
98+
.query(queryType)
99+
.subscription(mutation)
100+
.build()
101+
when:
102+
def validator = new SchemaValidator([dummyRule])
103+
validator.validateSchema(schema);
104+
105+
then:
106+
1 * dummyRule.check(field, _ as SchemaValidationErrorCollector)
107+
}
108+
109+
}

0 commit comments

Comments
 (0)