@@ -2,12 +2,16 @@ package graphql.servlet
2
2
3
3
import com.fasterxml.jackson.databind.ObjectMapper
4
4
import graphql.Scalars
5
+ import graphql.analysis.QueryVisitorInlineFragmentEnvironment
5
6
import graphql.execution.ExecutionTypeInfo
7
+ import graphql.execution.instrumentation.ChainedInstrumentation
8
+ import graphql.execution.instrumentation.Instrumentation
6
9
import graphql.schema.DataFetcher
7
10
import graphql.schema.GraphQLFieldDefinition
8
11
import graphql.schema.GraphQLNonNull
9
12
import graphql.schema.GraphQLObjectType
10
13
import graphql.schema.GraphQLSchema
14
+ import org.dataloader.DataLoaderRegistry
11
15
import org.springframework.mock.web.MockHttpServletRequest
12
16
import org.springframework.mock.web.MockHttpServletResponse
13
17
import spock.lang.Ignore
@@ -39,39 +43,45 @@ class AbstractGraphQLHttpServletSpec extends Specification {
39
43
response = new MockHttpServletResponse ()
40
44
}
41
45
42
- def createServlet (DataFetcher queryDataFetcher = { env -> env. arguments. arg }, DataFetcher mutationDataFetcher = { env -> env. arguments. arg }) {
46
+ def createServlet (DataFetcher queryDataFetcher = { env -> env. arguments. arg },
47
+ DataFetcher mutationDataFetcher = { env -> env. arguments. arg }) {
48
+ return SimpleGraphQLHttpServlet . newBuilder(createGraphQlSchema(queryDataFetcher, mutationDataFetcher)). build()
49
+ }
50
+
51
+ def createGraphQlSchema (DataFetcher queryDataFetcher = { env -> env. arguments. arg },
52
+ DataFetcher mutationDataFetcher = { env -> env. arguments. arg }) {
43
53
GraphQLObjectType query = GraphQLObjectType . newObject()
44
- .name(" Query" )
45
- .field { GraphQLFieldDefinition.Builder field ->
46
- field. name(" echo" )
47
- field. type(Scalars.GraphQLString )
48
- field. argument { argument ->
49
- argument. name(" arg" )
50
- argument. type(Scalars.GraphQLString )
51
- }
52
- field. dataFetcher(queryDataFetcher)
54
+ .name(" Query" )
55
+ .field { GraphQLFieldDefinition.Builder field ->
56
+ field. name(" echo" )
57
+ field. type(Scalars.GraphQLString )
58
+ field. argument { argument ->
59
+ argument. name(" arg" )
60
+ argument. type(Scalars.GraphQLString )
53
61
}
54
- .field { GraphQLFieldDefinition.Builder field ->
55
- field. name(" returnsNullIncorrectly" )
56
- field. type(new GraphQLNonNull (Scalars.GraphQLString ))
57
- field. dataFetcher({env -> null })
58
- }
59
- .build()
62
+ field. dataFetcher(queryDataFetcher)
63
+ }
64
+ .field { GraphQLFieldDefinition.Builder field ->
65
+ field. name(" returnsNullIncorrectly" )
66
+ field. type(new GraphQLNonNull (Scalars.GraphQLString ))
67
+ field. dataFetcher({env -> null })
68
+ }
69
+ .build()
60
70
61
71
GraphQLObjectType mutation = GraphQLObjectType . newObject()
62
- .name(" Mutation" )
63
- .field { field ->
64
- field. name(" echo" )
65
- field. type(Scalars.GraphQLString )
66
- field. argument { argument ->
67
- argument. name(" arg" )
68
- argument. type(Scalars.GraphQLString )
69
- }
70
- field. dataFetcher(mutationDataFetcher)
72
+ .name(" Mutation" )
73
+ .field { field ->
74
+ field. name(" echo" )
75
+ field. type(Scalars.GraphQLString )
76
+ field. argument { argument ->
77
+ argument. name(" arg" )
78
+ argument. type(Scalars.GraphQLString )
71
79
}
72
- .build()
80
+ field. dataFetcher(mutationDataFetcher)
81
+ }
82
+ .build()
73
83
74
- return SimpleGraphQLHttpServlet . newBuilder( new GraphQLSchema (query, mutation, [query, mutation]. toSet())) . build( )
84
+ return new GraphQLSchema (query, mutation, [query, mutation]. toSet())
75
85
}
76
86
77
87
Map<String , Object > getResponseContent () {
@@ -852,4 +862,39 @@ class AbstractGraphQLHttpServletSpec extends Specification {
852
862
then :
853
863
1 * mockInputStream. reset()
854
864
}
865
+
866
+ def " getInstrumentation returns the set Instrumentation if none is provided in the context" () {
867
+
868
+ setup :
869
+ Instrumentation expectedInstrumentation = Mock ()
870
+ GraphQLContext context = new GraphQLContext (request, null , null )
871
+ SimpleGraphQLHttpServlet simpleGraphQLServlet = SimpleGraphQLHttpServlet
872
+ .newBuilder(createGraphQlSchema())
873
+ .withQueryInvoker(GraphQLQueryInvoker . newBuilder(). withInstrumentation(expectedInstrumentation). build())
874
+ .build()
875
+ when :
876
+ Instrumentation actualInstrumentation = simpleGraphQLServlet. getQueryInvoker(). getInstrumentation(context)
877
+ then :
878
+ actualInstrumentation == expectedInstrumentation;
879
+ ! (actualInstrumentation instanceof ChainedInstrumentation )
880
+
881
+ }
882
+
883
+ def " getInstrumentation returns the ChainedInstrumentation if DataLoader provided in context" () {
884
+
885
+ setup :
886
+ Instrumentation servletInstrumentation = Mock ()
887
+ GraphQLContext context = new GraphQLContext (request, null , null )
888
+ DataLoaderRegistry dlr = Mock ()
889
+ context. setDataLoaderRegistry(dlr)
890
+ SimpleGraphQLHttpServlet simpleGraphQLServlet = SimpleGraphQLHttpServlet
891
+ .newBuilder(createGraphQlSchema())
892
+ .withQueryInvoker(GraphQLQueryInvoker . newBuilder(). withInstrumentation(servletInstrumentation). build())
893
+ .build();
894
+ when :
895
+ Instrumentation actualInstrumentation = simpleGraphQLServlet. getQueryInvoker(). getInstrumentation(context)
896
+ then :
897
+ actualInstrumentation instanceof ChainedInstrumentation
898
+ actualInstrumentation != servletInstrumentation
899
+ }
855
900
}
0 commit comments