Skip to content

Commit 41cb699

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 9c4667a + c78248d commit 41cb699

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/main/java/graphql/servlet/GraphQLQueryInvoker.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,27 @@ protected Instrumentation getInstrumentation(Object context) {
6363
if (context instanceof GraphQLContext) {
6464
return ((GraphQLContext) context).getDataLoaderRegistry()
6565
.map(registry -> {
66-
List<Instrumentation> instrumentations = new ArrayList<>();
67-
instrumentations.add(getInstrumentation.get());
68-
instrumentations.add(new DataLoaderDispatcherInstrumentation(dataLoaderDispatcherInstrumentationOptionsSupplier.get()));
69-
return new ChainedInstrumentation(instrumentations);
66+
Instrumentation instrumentation = getInstrumentation.get();
67+
if (!containsDispatchInstrumentation(instrumentation)) {
68+
List<Instrumentation> instrumentations = new ArrayList<>();
69+
instrumentations.add(instrumentation);
70+
instrumentations.add(new DataLoaderDispatcherInstrumentation(dataLoaderDispatcherInstrumentationOptionsSupplier.get()));
71+
instrumentation = new ChainedInstrumentation(instrumentations);
72+
}
73+
return instrumentation;
7074
})
71-
.map(Instrumentation.class::cast)
7275
.orElse(getInstrumentation.get());
7376
}
7477
return getInstrumentation.get();
7578
}
7679

80+
private boolean containsDispatchInstrumentation(Instrumentation instrumentation) {
81+
if (instrumentation instanceof ChainedInstrumentation) {
82+
return ((ChainedInstrumentation)instrumentation).getInstrumentations().stream().anyMatch(this::containsDispatchInstrumentation);
83+
}
84+
return instrumentation instanceof DataLoaderDispatcherInstrumentation;
85+
}
86+
7787
private ExecutionResult query(GraphQLInvocationInput invocationInput, ExecutionInput executionInput) {
7888
if (Subject.getSubject(AccessController.getContext()) == null && invocationInput.getSubject().isPresent()) {
7989
return Subject.doAs(invocationInput.getSubject().get(), (PrivilegedAction<ExecutionResult>) () -> {

src/test/groovy/graphql/servlet/AbstractGraphQLHttpServletSpec.groovy

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import graphql.Scalars
55
import graphql.execution.ExecutionStepInfo
66
import graphql.execution.instrumentation.ChainedInstrumentation
77
import graphql.execution.instrumentation.Instrumentation
8+
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation
89
import graphql.schema.DataFetcher
910
import graphql.execution.reactive.SingleSubscriberPublisher
1011
import graphql.schema.GraphQLNonNull
@@ -1214,4 +1215,23 @@ class AbstractGraphQLHttpServletSpec extends Specification {
12141215
actualInstrumentation instanceof ChainedInstrumentation
12151216
actualInstrumentation != servletInstrumentation
12161217
}
1218+
1219+
def "getInstrumentation does not add dataloader dispatch instrumentation if one is provided"() {
1220+
setup:
1221+
Instrumentation servletInstrumentation = Mock()
1222+
DataLoaderDispatcherInstrumentation mockDispatchInstrumentation = Mock()
1223+
ChainedInstrumentation chainedInstrumentation = new ChainedInstrumentation(Arrays.asList(servletInstrumentation,
1224+
mockDispatchInstrumentation))
1225+
GraphQLContext context = new GraphQLContext(request, response, null, null, null)
1226+
DataLoaderRegistry dlr = Mock()
1227+
context.setDataLoaderRegistry(dlr)
1228+
SimpleGraphQLHttpServlet simpleGraphQLServlet = SimpleGraphQLHttpServlet
1229+
.newBuilder(TestUtils.createGraphQlSchema())
1230+
.withQueryInvoker(GraphQLQueryInvoker.newBuilder().withInstrumentation(chainedInstrumentation).build())
1231+
.build();
1232+
when:
1233+
Instrumentation actualInstrumentation = simpleGraphQLServlet.getQueryInvoker().getInstrumentation(context)
1234+
then:
1235+
actualInstrumentation == chainedInstrumentation
1236+
}
12171237
}

0 commit comments

Comments
 (0)