Skip to content

Commit dd60198

Browse files
committed
Make SchemaValidator a single pass over types instead of a SchemaTraverser walk
SchemaValidator now applies its rules by iterating the schema's types and directive definitions directly, instead of running ten GraphQLTypeVisitor rules over every schema element with SchemaTraverser. validateSchema keeps the same signature; getRules() and the rule visitor classes are removed. Behaviour differences: - errors are reported grouped by type, so their order changes - elements shared between several parents are checked under each of them, where the traversal only checked them under the first parent it reached - schemas whose programmatic default values form a cycle now fail with an InvalidSchemaException instead of a StackOverflowError On large-schema-4 validation drops from ~407ms to ~16ms and allocation from ~532MB to ~12MB per run (SchemaValidatorBenchmark).
1 parent e1253c6 commit dd60198

24 files changed

Lines changed: 1414 additions & 1675 deletions
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package benchmark;
2+
3+
import graphql.schema.GraphQLSchema;
4+
import graphql.schema.idl.FastSchemaGenerator;
5+
import graphql.schema.idl.RuntimeWiring;
6+
import graphql.schema.idl.SchemaGenerator;
7+
import graphql.schema.idl.SchemaParser;
8+
import graphql.schema.validation.SchemaValidator;
9+
import org.openjdk.jmh.annotations.Benchmark;
10+
import org.openjdk.jmh.annotations.BenchmarkMode;
11+
import org.openjdk.jmh.annotations.Fork;
12+
import org.openjdk.jmh.annotations.Measurement;
13+
import org.openjdk.jmh.annotations.Mode;
14+
import org.openjdk.jmh.annotations.OutputTimeUnit;
15+
import org.openjdk.jmh.annotations.Param;
16+
import org.openjdk.jmh.annotations.Scope;
17+
import org.openjdk.jmh.annotations.Setup;
18+
import org.openjdk.jmh.annotations.State;
19+
import org.openjdk.jmh.annotations.Warmup;
20+
import org.openjdk.jmh.infra.Blackhole;
21+
22+
import java.util.concurrent.TimeUnit;
23+
24+
@Warmup(iterations = 2, time = 5)
25+
@Measurement(iterations = 3)
26+
@Fork(2)
27+
@State(Scope.Benchmark)
28+
@BenchmarkMode(Mode.AverageTime)
29+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
30+
public class SchemaValidatorBenchmark {
31+
32+
@Param({"large-schema-4.graphqls", "large-schema-federated-1.graphqls"})
33+
public String schemaFile;
34+
35+
private GraphQLSchema schema;
36+
37+
@Setup
38+
public void setup() {
39+
String sdl = BenchmarkUtils.loadResource(schemaFile);
40+
schema = new FastSchemaGenerator().makeExecutableSchema(
41+
SchemaGenerator.Options.defaultOptions().withValidation(false),
42+
new SchemaParser().parse(sdl),
43+
RuntimeWiring.MOCKED_WIRING);
44+
}
45+
46+
@Benchmark
47+
public void validateSchema(Blackhole blackhole) {
48+
blackhole.consume(new SchemaValidator().validateSchema(schema));
49+
}
50+
}

‎src/main/java/graphql/schema/validation/AppliedDirectiveArgumentsAreValid.java‎

Lines changed: 0 additions & 100 deletions
This file was deleted.

‎src/main/java/graphql/schema/validation/AppliedDirectivesAreValid.java‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

‎src/main/java/graphql/schema/validation/DefaultValuesAreValid.java‎

Lines changed: 0 additions & 91 deletions
This file was deleted.

‎src/main/java/graphql/schema/validation/DeprecatedInputObjectAndArgumentsAreValid.java‎

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)