@@ -2,19 +2,20 @@ package graphql.execution.instrumentation
22
33import graphql.GraphQL
44import graphql.StarWarsSchema
5+ import graphql.TestUtil
56import graphql.execution.AsyncExecutionStrategy
67import graphql.execution.AsyncSerialExecutionStrategy
78import graphql.execution.batched.BatchedExecutionStrategy
89import graphql.execution.instrumentation.tracing.TracingInstrumentation
10+ import graphql.schema.DataFetcher
11+ import graphql.schema.DataFetchingEnvironment
912import spock.lang.Specification
1013
11- class TracingInstrumentationTest extends Specification {
12-
14+ import static graphql.execution.instrumentation.tracing.TracingInstrumentation.Options.newOptions
1315
14- def ' tracing captures timings as expected' () {
15- given :
16+ class TracingInstrumentationTest extends Specification {
1617
17- def query = """
18+ def query = """
1819 {
1920 hero {
2021 id
@@ -23,9 +24,14 @@ class TracingInstrumentationTest extends Specification {
2324 }
2425 """
2526
27+
28+ def ' tracing captures timings as expected' () {
29+ given :
30+
31+
2632 when :
2733
28- def instrumentation = new TracingInstrumentation ()
34+ def instrumentation = new TracingInstrumentation (newOptions() . includeTrivialDataFetchers( true ) )
2935
3036 def graphQL = GraphQL
3137 .newGraphQL(StarWarsSchema . starWarsSchema)
@@ -95,4 +101,64 @@ class TracingInstrumentationTest extends Specification {
95101 new AsyncSerialExecutionStrategy () | _
96102 new BatchedExecutionStrategy () | _
97103 }
104+
105+ def " trivial data fetchers are ignored" () {
106+ given :
107+
108+ def spec = '''
109+ type Query {
110+ hero : Hero
111+ }
112+
113+ type Hero {
114+ id : ID
115+ appearsIn : String
116+ }
117+ '''
118+
119+ DataFetcher df = new DataFetcher () {
120+ @Override
121+ Object get (DataFetchingEnvironment environment ) throws Exception {
122+ return [id : " id" , appearsIn : " appearsIn" ]
123+ }
124+
125+ // isTrivialDataFetcher defaults to false
126+ }
127+
128+ def instrumentation = new TracingInstrumentation () // defaults to false
129+
130+ def graphQL = TestUtil . graphQL(spec, [Query : [hero : df]])
131+ .queryExecutionStrategy(testExecutionStrategy)
132+ .instrumentation(instrumentation)
133+ .build()
134+ when :
135+ def executionResult = graphQL. execute(query)
136+
137+ def specExtensions = executionResult. toSpecification(). get(" extensions" )
138+ def tracing = specExtensions[' tracing' ]
139+
140+ then :
141+
142+ tracing[" version" ] == 1L
143+ tracing[" startTime" ] != null
144+ tracing[" endTime" ] != null
145+ tracing[" duration" ] > 0L
146+
147+ List resolvers = tracing[' execution' ][' resolvers' ] as List
148+ resolvers. size() == 1
149+ resolvers[0 ][' fieldName' ] == " hero"
150+ resolvers[0 ][' path' ] == [" hero" ]
151+ resolvers[0 ][' startOffset' ] > 0L
152+ resolvers[0 ][' duration' ] > 0L
153+ resolvers[0 ][' parentType' ] == " Query"
154+ resolvers[0 ][' returnType' ] == " Hero"
155+
156+ where :
157+
158+ testExecutionStrategy | _
159+ new AsyncExecutionStrategy () | _
160+ new AsyncSerialExecutionStrategy () | _
161+ new BatchedExecutionStrategy () | _
162+
163+ }
98164}
0 commit comments