Skip to content

Commit 7e7eb4a

Browse files
author
agairol
committed
return ChainedInstrumentation when registry present in context
1 parent 0ed786e commit 7e7eb4a

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ dependencies {
5353
compile 'javax.servlet:javax.servlet-api:3.0.1'
5454

5555
// GraphQL
56-
compile 'com.graphql-java:graphql-java:9.0'
56+
compile 'com.graphql-java:graphql-java:9.2'
5757

5858
testCompile 'io.github.graphql-java:graphql-java-annotations:5.2'
5959

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: 16 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,15 @@ 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+
if (this.instrumentation != null) {
233+
instrumentations.add(this.instrumentation);
234+
}
235+
instrumentations.add(new DataLoaderDispatcherInstrumentation(registry));
236+
237+
return new ChainedInstrumentation(instrumentations);
238+
})
227239
.map(Instrumentation.class::cast)
228240
.orElse(this.instrumentation);
229241
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ 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
7-
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation
88
import graphql.schema.DataFetcher
99
import graphql.schema.GraphQLFieldDefinition
1010
import graphql.schema.GraphQLNonNull
@@ -878,11 +878,11 @@ class GraphQLServletSpec extends Specification {
878878
Instrumentation actualInstrumentation = simpleGraphQLServlet.getInstrumentation(context)
879879
then:
880880
actualInstrumentation == expectedInstrumentation;
881-
! (actualInstrumentation instanceof DataLoaderDispatcherInstrumentation)
881+
! (actualInstrumentation instanceof ChainedInstrumentation)
882882

883883
}
884884

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

887887
setup:
888888
Instrumentation servletInstrumentation = Mock()
@@ -896,8 +896,7 @@ class GraphQLServletSpec extends Specification {
896896
when:
897897
Instrumentation actualInstrumentation = simpleGraphQLServlet.getInstrumentation(context)
898898
then:
899-
actualInstrumentation instanceof DataLoaderDispatcherInstrumentation
900-
actualInstrumentation != servletInstrumentation;
901-
899+
actualInstrumentation instanceof ChainedInstrumentation
900+
actualInstrumentation != servletInstrumentation
902901
}
903902
}

0 commit comments

Comments
 (0)