@@ -10,13 +10,13 @@ class GraphQLResolvePlugin extends TracingPlugin {
1010 static operation = 'resolve'
1111
1212 start ( fieldCtx ) {
13- const { info, rootCtx, args } = fieldCtx
13+ const { info, rootCtx, args, path : pathAsArray , pathString } = fieldCtx
1414
15- const path = getPath ( info , this . config )
15+ const path = getPath ( this . config , pathAsArray )
1616
1717 // we need to get the parent span to the field if it exists for correct span parenting
1818 // of nested fields
19- const parentField = getParentField ( rootCtx , pathToArray ( info && info . path ) )
19+ const parentField = getParentField ( rootCtx , pathString )
2020 const childOf = parentField ?. ctx ?. currentStore ?. span
2121
2222 fieldCtx . parent = parentField
@@ -76,9 +76,9 @@ class GraphQLResolvePlugin extends TracingPlugin {
7676 super ( ...args )
7777
7878 this . addTraceSub ( 'updateField' , ( ctx ) => {
79- const { field, info , error } = ctx
79+ const { field, error , path : pathAsArray } = ctx
8080
81- const path = getPath ( info , this . config )
81+ const path = getPath ( this . config , pathAsArray )
8282
8383 if ( ! shouldInstrument ( this . config , path ) ) return
8484
@@ -118,28 +118,14 @@ function shouldInstrument (config, path) {
118118 return config . depth < 0 || config . depth >= depth
119119}
120120
121- function getPath ( info , config ) {
122- const responsePathAsArray = config . collapse
123- ? withCollapse ( pathToArray )
124- : pathToArray
125- return responsePathAsArray ( info && info . path )
121+ function getPath ( config , pathAsArray ) {
122+ return config . collapse
123+ ? withCollapse ( pathAsArray )
124+ : pathAsArray
126125}
127126
128- function pathToArray ( path ) {
129- const flattened = [ ]
130- let curr = path
131- while ( curr ) {
132- flattened . push ( curr . key )
133- curr = curr . prev
134- }
135- return flattened . reverse ( )
136- }
137-
138- function withCollapse ( responsePathAsArray ) {
139- return function ( ) {
140- return responsePathAsArray . apply ( this , arguments )
141- . map ( segment => typeof segment === 'number' ? '*' : segment )
142- }
127+ function withCollapse ( pathAsArray ) {
128+ return pathAsArray . map ( segment => typeof segment === 'number' ? '*' : segment )
143129}
144130
145131function getResolverInfo ( info , args ) {
@@ -173,19 +159,20 @@ function getResolverInfo (info, args) {
173159 return resolverInfo
174160}
175161
176- function getParentField ( parentCtx , path ) {
177- for ( let i = path . length - 1 ; i > 0 ; i -- ) {
178- const field = getField ( parentCtx , path . slice ( 0 , i ) )
179- if ( field ) {
180- return field
181- }
162+ function getParentField ( parentCtx , pathToString ) {
163+ let current = pathToString
164+
165+ while ( current ) {
166+ const lastJoin = current . lastIndexOf ( '.' )
167+ if ( lastJoin === - 1 ) break
168+
169+ current = current . slice ( 0 , lastJoin )
170+ const field = parentCtx . fields [ current ]
171+
172+ if ( field ) return field
182173 }
183174
184175 return null
185176}
186177
187- function getField ( parentCtx , path ) {
188- return parentCtx . fields [ path . join ( '.' ) ]
189- }
190-
191178module . exports = GraphQLResolvePlugin
0 commit comments