Skip to content

Commit 065abc6

Browse files
author
agairol
committed
return ChainedInstrumentation when registry present in context
1 parent 14a7906 commit 065abc6

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/main/java/graphql/servlet/OsgiGraphQLServlet.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.servlet;
22

3+
import graphql.execution.instrumentation.ChainedInstrumentation;
34
import graphql.execution.instrumentation.Instrumentation;
45
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation;
56
import graphql.execution.preparsed.NoOpPreparsedDocumentProvider;
@@ -207,7 +208,13 @@ protected ExecutionStrategyProvider getExecutionStrategyProvider() {
207208
@Override
208209
protected Instrumentation getInstrumentation(GraphQLContext context) {
209210
return context.getDataLoaderRegistry()
210-
.map(DataLoaderDispatcherInstrumentation::new)
211+
.map(registry -> {
212+
List<Instrumentation> instrumentations = new ArrayList<>();
213+
instrumentations.add(this.instrumentationProvider.getInstrumentation());
214+
instrumentations.add(new DataLoaderDispatcherInstrumentation(registry));
215+
216+
return new ChainedInstrumentation(instrumentations);
217+
})
211218
.map(Instrumentation.class::cast)
212219
.orElse(instrumentationProvider.getInstrumentation());
213220
}

src/main/java/graphql/servlet/SimpleGraphQLServlet.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package graphql.servlet;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45
import java.util.Optional;
56

67
import javax.servlet.http.HttpServletRequest;
78
import javax.servlet.http.HttpServletResponse;
89

910
import graphql.execution.ExecutionStrategy;
11+
import graphql.execution.instrumentation.ChainedInstrumentation;
1012
import graphql.execution.instrumentation.Instrumentation;
1113
import graphql.execution.instrumentation.SimpleInstrumentation;
1214
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation;
1315
import graphql.execution.preparsed.NoOpPreparsedDocumentProvider;
1416
import graphql.execution.preparsed.PreparsedDocumentProvider;
1517
import graphql.schema.GraphQLSchema;
1618

19+
20+
1721
/**
1822
* @author Andrew Potter
1923
*/
2024
public class SimpleGraphQLServlet extends GraphQLServlet {
21-
25+
2226

2327
/**
2428
* @deprecated use {@link #builder(GraphQLSchema)} instead.
@@ -52,7 +56,7 @@ public SimpleGraphQLServlet(final GraphQLSchema schema, ExecutionStrategyProvide
5256
this(new DefaultGraphQLSchemaProvider(schema), executionStrategyProvider, objectMapperConfigurer, listeners, instrumentation, errorHandler, contextBuilder, rootObjectBuilder, preparsedDocumentProvider,false);
5357
}
5458

55-
59+
5660
/**
5761
* @deprecated use {@link #builder(GraphQLSchemaProvider)} instead.
5862
*/
@@ -189,7 +193,7 @@ public Builder withListeners(List<GraphQLServletListener> listeners) {
189193
this.listeners = listeners;
190194
return this;
191195
}
192-
196+
193197
public Builder withAsyncServletMode(boolean value) {
194198
this.asyncServletMode=value;
195199
return this;
@@ -223,7 +227,13 @@ protected ExecutionStrategyProvider getExecutionStrategyProvider() {
223227
@Override
224228
protected Instrumentation getInstrumentation(GraphQLContext context) {
225229
return context.getDataLoaderRegistry()
226-
.map(DataLoaderDispatcherInstrumentation::new)
230+
.map(registry -> {
231+
List<Instrumentation> instrumentations = new ArrayList<>();
232+
instrumentations.add(this.instrumentation);
233+
instrumentations.add(new DataLoaderDispatcherInstrumentation(registry));
234+
235+
return new ChainedInstrumentation(instrumentations);
236+
})
227237
.map(Instrumentation.class::cast)
228238
.orElse(this.instrumentation);
229239
}

src/test/groovy/graphql/servlet/GraphQLServletSpec.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package graphql.servlet
33
import com.fasterxml.jackson.databind.ObjectMapper
44
import graphql.Scalars
55
import graphql.execution.ExecutionTypeInfo
6+
import graphql.execution.instrumentation.ChainedInstrumentation
67
import graphql.execution.instrumentation.Instrumentation
78
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation
89
import graphql.schema.DataFetcher
@@ -882,7 +883,7 @@ class GraphQLServletSpec extends Specification {
882883

883884
}
884885

885-
def "getInstrumentation returns the DataLoaderDispatcherInstrumentation if DataLoader provided in context"() {
886+
def "getInstrumentation returns the ChainedInstrumentation if DataLoader provided in context"() {
886887

887888
setup:
888889
Instrumentation servletInstrumentation = Mock()
@@ -896,8 +897,7 @@ class GraphQLServletSpec extends Specification {
896897
when:
897898
Instrumentation actualInstrumentation = simpleGraphQLServlet.getInstrumentation(context)
898899
then:
899-
actualInstrumentation instanceof DataLoaderDispatcherInstrumentation
900-
actualInstrumentation != servletInstrumentation;
901-
900+
actualInstrumentation instanceof ChainedInstrumentation
901+
actualInstrumentation != servletInstrumentation
902902
}
903903
}

0 commit comments

Comments
 (0)