Skip to content

added a check preventing a second dispatch instrumentation when one p… #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/main/java/graphql/servlet/GraphQLQueryInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,27 @@ protected Instrumentation getInstrumentation(Object context) {
if (context instanceof GraphQLContext) {
return ((GraphQLContext) context).getDataLoaderRegistry()
.map(registry -> {
List<Instrumentation> instrumentations = new ArrayList<>();
instrumentations.add(getInstrumentation.get());
instrumentations.add(new DataLoaderDispatcherInstrumentation(dataLoaderDispatcherInstrumentationOptionsSupplier.get()));
return new ChainedInstrumentation(instrumentations);
Instrumentation instrumentation = getInstrumentation.get();
if (!containsDispatchInstrumentation(instrumentation)) {
List<Instrumentation> instrumentations = new ArrayList<>();
instrumentations.add(instrumentation);
instrumentations.add(new DataLoaderDispatcherInstrumentation(dataLoaderDispatcherInstrumentationOptionsSupplier.get()));
instrumentation = new ChainedInstrumentation(instrumentations);
}
return instrumentation;
})
.map(Instrumentation.class::cast)
.orElse(getInstrumentation.get());
}
return getInstrumentation.get();
}

private boolean containsDispatchInstrumentation(Instrumentation instrumentation) {
if (instrumentation instanceof ChainedInstrumentation) {
return ((ChainedInstrumentation)instrumentation).getInstrumentations().stream().anyMatch(this::containsDispatchInstrumentation);
}
return instrumentation instanceof DataLoaderDispatcherInstrumentation;
}

private ExecutionResult query(GraphQLInvocationInput invocationInput, ExecutionInput executionInput) {
if (Subject.getSubject(AccessController.getContext()) == null && invocationInput.getSubject().isPresent()) {
return Subject.doAs(invocationInput.getSubject().get(), (PrivilegedAction<ExecutionResult>) () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import graphql.Scalars
import graphql.execution.ExecutionStepInfo
import graphql.execution.instrumentation.ChainedInstrumentation
import graphql.execution.instrumentation.Instrumentation
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation
import graphql.schema.DataFetcher
import graphql.execution.reactive.SingleSubscriberPublisher
import graphql.schema.GraphQLNonNull
Expand Down Expand Up @@ -1214,4 +1215,23 @@ class AbstractGraphQLHttpServletSpec extends Specification {
actualInstrumentation instanceof ChainedInstrumentation
actualInstrumentation != servletInstrumentation
}

def "getInstrumentation does not add dataloader dispatch instrumentation if one is provided"() {
setup:
Instrumentation servletInstrumentation = Mock()
DataLoaderDispatcherInstrumentation mockDispatchInstrumentation = Mock()
ChainedInstrumentation chainedInstrumentation = new ChainedInstrumentation(Arrays.asList(servletInstrumentation,
mockDispatchInstrumentation))
GraphQLContext context = new GraphQLContext(request, response, null, null, null)
DataLoaderRegistry dlr = Mock()
context.setDataLoaderRegistry(dlr)
SimpleGraphQLHttpServlet simpleGraphQLServlet = SimpleGraphQLHttpServlet
.newBuilder(TestUtils.createGraphQlSchema())
.withQueryInvoker(GraphQLQueryInvoker.newBuilder().withInstrumentation(chainedInstrumentation).build())
.build();
when:
Instrumentation actualInstrumentation = simpleGraphQLServlet.getQueryInvoker().getInstrumentation(context)
then:
actualInstrumentation == chainedInstrumentation
}
}