Skip to content

Commit 9616132

Browse files
waschebbakerman
authored andcommitted
1 parent cca4316 commit 9616132

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

src/main/java/graphql/execution/ExecutionContext.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import org.dataloader.DataLoaderRegistry;
1616

1717
import java.util.Collections;
18+
import java.util.HashSet;
1819
import java.util.List;
1920
import java.util.Map;
21+
import java.util.Set;
2022
import java.util.concurrent.CopyOnWriteArrayList;
2123
import java.util.function.Consumer;
2224

@@ -38,6 +40,7 @@ public class ExecutionContext {
3840
private final Object context;
3941
private final Instrumentation instrumentation;
4042
private final List<GraphQLError> errors = new CopyOnWriteArrayList<>();
43+
private final Set<ExecutionPath> errorPaths = new HashSet<>();
4144
private final DataLoaderRegistry dataLoaderRegistry;
4245
private final CacheControl cacheControl;
4346
private final DeferSupport deferSupport = new DeferSupport();
@@ -128,13 +131,8 @@ public void addError(GraphQLError error, ExecutionPath fieldPath) {
128131
// field errors should be handled - ie only once per field if its already there for nullability
129132
// but unclear if its not that error path
130133
//
131-
for (GraphQLError graphQLError : errors) {
132-
List<Object> path = graphQLError.getPath();
133-
if (path != null) {
134-
if (fieldPath.equals(ExecutionPath.fromList(path))) {
135-
return;
136-
}
137-
}
134+
if (!errorPaths.add(fieldPath)) {
135+
return;
138136
}
139137
this.errors.add(error);
140138
}
@@ -149,6 +147,9 @@ public void addError(GraphQLError error) {
149147
// see https://github.com/graphql-java/graphql-java/issues/888 on how the spec is unclear
150148
// on how exactly multiple errors should be handled - ie only once per field or not outside the nullability
151149
// aspect.
150+
if (error.getPath() != null) {
151+
this.errorPaths.add(ExecutionPath.fromList(error.getPath()));
152+
}
152153
this.errors.add(error);
153154
}
154155

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package benchmark;
2+
3+
import graphql.execution.ExecutionContext;
4+
import graphql.execution.ExecutionContextBuilder;
5+
import graphql.execution.ExecutionId;
6+
import graphql.execution.ExecutionPath;
7+
import graphql.schema.idl.errors.SchemaMissingError;
8+
import org.openjdk.jmh.annotations.Benchmark;
9+
import org.openjdk.jmh.annotations.BenchmarkMode;
10+
import org.openjdk.jmh.annotations.Measurement;
11+
import org.openjdk.jmh.annotations.Mode;
12+
import org.openjdk.jmh.annotations.Scope;
13+
import org.openjdk.jmh.annotations.State;
14+
import org.openjdk.jmh.annotations.Warmup;
15+
16+
import java.util.Collections;
17+
18+
@State(Scope.Benchmark)
19+
public class AddError {
20+
21+
private ExecutionContext context = new ExecutionContextBuilder()
22+
.executionId(ExecutionId.generate())
23+
.build();
24+
25+
private volatile int x = 0;
26+
27+
@Benchmark
28+
@BenchmarkMode(Mode.SingleShotTime)
29+
@Warmup(iterations = 1, batchSize = 50000)
30+
@Measurement(iterations = 1, batchSize = 5000)
31+
public ExecutionContext benchMarkAddError() {
32+
context.addError(
33+
new SchemaMissingError(),
34+
ExecutionPath.fromList(Collections.singletonList(x++))
35+
);
36+
return context;
37+
}
38+
}

0 commit comments

Comments
 (0)