Skip to content

Commit 5304bb6

Browse files
committed
Reworked the DataLoader instrumentation to become compatible with 6.0.0
1 parent db4fcb5 commit 5304bb6

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version = 6.0.0
1+
version = 6.1.0
22
group = com.graphql-java

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import graphql.ExecutionInput;
44
import graphql.ExecutionResult;
55
import graphql.GraphQL;
6+
import graphql.execution.instrumentation.ChainedInstrumentation;
67
import graphql.execution.instrumentation.Instrumentation;
78
import graphql.execution.instrumentation.SimpleInstrumentation;
9+
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation;
810
import graphql.execution.preparsed.NoOpPreparsedDocumentProvider;
911
import graphql.execution.preparsed.PreparsedDocumentProvider;
1012
import graphql.schema.GraphQLSchema;
@@ -13,7 +15,9 @@
1315
import javax.security.auth.Subject;
1416
import java.security.AccessController;
1517
import java.security.PrivilegedAction;
18+
import java.util.ArrayList;
1619
import java.util.Iterator;
20+
import java.util.List;
1721
import java.util.function.Supplier;
1822

1923
/**
@@ -44,17 +48,32 @@ public void query(GraphQLBatchedInvocationInput batchedInvocationInput, Executio
4448
}
4549
}
4650

47-
private GraphQL newGraphQL(GraphQLSchema schema) {
51+
private GraphQL newGraphQL(GraphQLSchema schema, Object context) {
4852
ExecutionStrategyProvider executionStrategyProvider = getExecutionStrategyProvider.get();
4953
return GraphQL.newGraphQL(schema)
5054
.queryExecutionStrategy(executionStrategyProvider.getQueryExecutionStrategy())
5155
.mutationExecutionStrategy(executionStrategyProvider.getMutationExecutionStrategy())
5256
.subscriptionExecutionStrategy(executionStrategyProvider.getSubscriptionExecutionStrategy())
53-
.instrumentation(getInstrumentation.get())
57+
.instrumentation(getInstrumentation(context))
5458
.preparsedDocumentProvider(getPreparsedDocumentProvider.get())
5559
.build();
5660
}
5761

62+
protected Instrumentation getInstrumentation(Object context) {
63+
if (context instanceof GraphQLContext) {
64+
return ((GraphQLContext) context).getDataLoaderRegistry()
65+
.map(registry -> {
66+
List<Instrumentation> instrumentations = new ArrayList<>();
67+
instrumentations.add(getInstrumentation.get());
68+
instrumentations.add(new DataLoaderDispatcherInstrumentation(registry));
69+
return new ChainedInstrumentation(instrumentations);
70+
})
71+
.map(Instrumentation.class::cast)
72+
.orElse(getInstrumentation.get());
73+
}
74+
return getInstrumentation.get();
75+
}
76+
5877
private ExecutionResult query(GraphQLInvocationInput invocationInput, ExecutionInput executionInput) {
5978
if (Subject.getSubject(AccessController.getContext()) == null && invocationInput.getSubject().isPresent()) {
6079
return Subject.doAs(invocationInput.getSubject().get(), (PrivilegedAction<ExecutionResult>) () -> {
@@ -70,7 +89,7 @@ private ExecutionResult query(GraphQLInvocationInput invocationInput, ExecutionI
7089
}
7190

7291
private ExecutionResult query(GraphQLSchema schema, ExecutionInput executionInput) {
73-
return newGraphQL(schema).execute(executionInput);
92+
return newGraphQL(schema, executionInput.getContext()).execute(executionInput);
7493
}
7594

7695
public static Builder newBuilder() {

src/main/java/graphql/servlet/OsgiGraphQLHttpServlet.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,6 @@ public InstrumentationProvider getInstrumentationProvider() {
236236
return instrumentationProvider;
237237
}
238238

239-
protected Instrumentation getInstrumentation(GraphQLContext context) {
240-
return context.getDataLoaderRegistry()
241-
.map(registry -> {
242-
List<Instrumentation> instrumentations = new ArrayList<>();
243-
instrumentations.add(this.instrumentationProvider.getInstrumentation());
244-
instrumentations.add(new DataLoaderDispatcherInstrumentation(registry));
245-
246-
return new ChainedInstrumentation(instrumentations);
247-
})
248-
.map(Instrumentation.class::cast)
249-
.orElse(instrumentationProvider.getInstrumentation());
250-
}
251-
252239
public GraphQLErrorHandler getErrorHandler() {
253240
return errorHandler;
254241
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package graphql.servlet
22

33
import com.fasterxml.jackson.databind.ObjectMapper
44
import graphql.Scalars
5+
import graphql.analysis.QueryVisitorInlineFragmentEnvironment
56
import graphql.execution.ExecutionTypeInfo
67
import graphql.execution.instrumentation.ChainedInstrumentation
78
import graphql.execution.instrumentation.Instrumentation
@@ -44,7 +45,7 @@ class AbstractGraphQLHttpServletSpec extends Specification {
4445

4546
def createServlet(DataFetcher queryDataFetcher = { env -> env.arguments.arg },
4647
DataFetcher mutationDataFetcher = { env -> env.arguments.arg }) {
47-
return SimpleGraphQLHttpServlet.newBuilder(createGraphQlSchema(queryDataFetcher, mutationDataFetcher))
48+
return SimpleGraphQLHttpServlet.newBuilder(createGraphQlSchema(queryDataFetcher, mutationDataFetcher)).build()
4849
}
4950

5051
def createGraphQlSchema(DataFetcher queryDataFetcher = { env -> env.arguments.arg },
@@ -866,13 +867,13 @@ class AbstractGraphQLHttpServletSpec extends Specification {
866867

867868
setup:
868869
Instrumentation expectedInstrumentation = Mock()
869-
GraphQLContext context = new GraphQLContext(Optional.of(request), Optional.of(response))
870-
SimpleGraphQLServlet simpleGraphQLServlet = SimpleGraphQLServlet
871-
.builder(createGraphQlSchema())
872-
.withInstrumentation(expectedInstrumentation)
873-
.build();
870+
GraphQLContext context = new GraphQLContext(request, null, null)
871+
SimpleGraphQLHttpServlet simpleGraphQLServlet = SimpleGraphQLHttpServlet
872+
.newBuilder(createGraphQlSchema())
873+
.withQueryInvoker(GraphQLQueryInvoker.newBuilder().withInstrumentation(expectedInstrumentation).build())
874+
.build()
874875
when:
875-
Instrumentation actualInstrumentation = simpleGraphQLServlet.getInstrumentation(context)
876+
Instrumentation actualInstrumentation = simpleGraphQLServlet.getQueryInvoker().getInstrumentation(context)
876877
then:
877878
actualInstrumentation == expectedInstrumentation;
878879
! (actualInstrumentation instanceof ChainedInstrumentation)
@@ -883,15 +884,15 @@ class AbstractGraphQLHttpServletSpec extends Specification {
883884

884885
setup:
885886
Instrumentation servletInstrumentation = Mock()
886-
GraphQLContext context = new GraphQLContext(Optional.of(request), Optional.of(response))
887+
GraphQLContext context = new GraphQLContext(request, null, null)
887888
DataLoaderRegistry dlr = Mock()
888-
context.setDataLoaderRegistry(Optional.of(dlr))
889-
SimpleGraphQLServlet simpleGraphQLServlet = SimpleGraphQLServlet
890-
.builder(createGraphQlSchema())
891-
.withInstrumentation(servletInstrumentation)
889+
context.setDataLoaderRegistry(dlr)
890+
SimpleGraphQLHttpServlet simpleGraphQLServlet = SimpleGraphQLHttpServlet
891+
.newBuilder(createGraphQlSchema())
892+
.withQueryInvoker(GraphQLQueryInvoker.newBuilder().withInstrumentation(servletInstrumentation).build())
892893
.build();
893894
when:
894-
Instrumentation actualInstrumentation = simpleGraphQLServlet.getInstrumentation(context)
895+
Instrumentation actualInstrumentation = simpleGraphQLServlet.getQueryInvoker().getInstrumentation(context)
895896
then:
896897
actualInstrumentation instanceof ChainedInstrumentation
897898
actualInstrumentation != servletInstrumentation

0 commit comments

Comments
 (0)