@@ -88,28 +88,28 @@ function wrapParse (parse) {
88
88
return parse . apply ( this , arguments )
89
89
}
90
90
91
- const ctx = { }
91
+ const ctx = { source }
92
92
return parseStartCh . runStores ( ctx , ( ) => {
93
- let document
94
93
try {
95
- document = parse . apply ( this , arguments )
96
- const operation = getOperation ( document )
94
+ ctx . document = parse . apply ( this , arguments )
95
+ const operation = getOperation ( ctx . document )
97
96
98
- if ( ! operation ) return document
97
+ if ( ! operation ) return ctx . document
99
98
100
99
if ( source ) {
101
- documentSources . set ( document , source . body || source )
100
+ documentSources . set ( ctx . document , source . body || source )
102
101
}
102
+ ctx . docSource = documentSources . get ( ctx . document )
103
103
104
- return document
104
+ return ctx . document
105
105
} catch ( err ) {
106
106
err . stack
107
107
ctx . error = err
108
108
parseErrorCh . publish ( ctx )
109
109
110
110
throw err
111
111
} finally {
112
- parseFinishCh . publish ( { source , document , docSource : documentSources . get ( document ) , ... ctx } )
112
+ parseFinishCh . publish ( ctx )
113
113
}
114
114
} )
115
115
}
@@ -138,7 +138,8 @@ function wrapValidate (validate) {
138
138
139
139
throw err
140
140
} finally {
141
- validateFinishCh . publish ( { errors, ...ctx } )
141
+ ctx . errors = errors
142
+ validateFinishCh . publish ( ctx )
142
143
}
143
144
} )
144
145
}
@@ -159,7 +160,14 @@ function wrapExecute (execute) {
159
160
const contextValue = args . contextValue
160
161
const operation = getOperation ( document , args . operationName )
161
162
162
- const ctx = { operation, args, docSource : documentSources . get ( document ) }
163
+ const ctx = {
164
+ operation,
165
+ args,
166
+ docSource : documentSources . get ( document ) ,
167
+ source,
168
+ fields : { } ,
169
+ abortController : new AbortController ( )
170
+ }
163
171
return startExecuteCh . runStores ( ctx , ( ) => {
164
172
if ( contexts . has ( contextValue ) ) {
165
173
return exe . apply ( this , arguments )
@@ -170,12 +178,10 @@ function wrapExecute (execute) {
170
178
wrapFields ( schema . _mutationType )
171
179
}
172
180
173
- const context = { source, fields : { } , abortController : new AbortController ( ) , ...ctx }
174
-
175
- contexts . set ( contextValue , context )
181
+ contexts . set ( contextValue , ctx )
176
182
177
- return callInAsyncScope ( exe , this , arguments , context . abortController , ( err , res ) => {
178
- if ( finishResolveCh . hasSubscribers ) finishResolvers ( context )
183
+ return callInAsyncScope ( exe , this , arguments , ctx . abortController , ( err , res ) => {
184
+ if ( finishResolveCh . hasSubscribers ) finishResolvers ( ctx )
179
185
180
186
const error = err || ( res && res . errors && res . errors [ 0 ] )
181
187
@@ -198,14 +204,17 @@ function wrapResolve (resolve) {
198
204
function resolveAsync ( source , args , contextValue , info ) {
199
205
if ( ! startResolveCh . hasSubscribers ) return resolve . apply ( this , arguments )
200
206
201
- const context = contexts . get ( contextValue )
207
+ const ctx = contexts . get ( contextValue )
202
208
203
- if ( ! context ) return resolve . apply ( this , arguments )
209
+ if ( ! ctx ) return resolve . apply ( this , arguments )
204
210
205
- const field = assertField ( context , info , args )
211
+ const field = assertField ( ctx , info , args )
206
212
207
- return callInAsyncScope ( resolve , this , arguments , context . abortController , ( err ) => {
208
- updateFieldCh . publish ( { field, info, err, ...field . ctx } )
213
+ return callInAsyncScope ( resolve , this , arguments , ctx . abortController , ( err ) => {
214
+ field . ctx . error = err
215
+ field . ctx . info = info
216
+ field . ctx . field = field
217
+ updateFieldCh . publish ( field . ctx )
209
218
} )
210
219
}
211
220
@@ -250,49 +259,28 @@ function pathToArray (path) {
250
259
return flattened . reverse ( )
251
260
}
252
261
253
- function assertField ( context , info , args ) {
262
+ function assertField ( parentCtx , info , args ) {
254
263
const pathInfo = info && info . path
255
264
256
265
const path = pathToArray ( pathInfo )
257
266
258
267
const pathString = path . join ( '.' )
259
- const fields = context . fields
268
+ const fields = parentCtx . fields
260
269
261
270
let field = fields [ pathString ]
262
271
263
272
if ( ! field ) {
264
- const parent = getParentField ( context , path )
265
- // we need to pass the parent span to the field if it exists for correct span parenting
266
- // of nested fields
267
- const ctx = { info, context, args, childOf : parent ?. ctx ?. currentStore ?. span }
268
- startResolveCh . publish ( ctx )
273
+ const fieldCtx = { info, ctx : parentCtx , args }
274
+ startResolveCh . publish ( fieldCtx )
269
275
field = fields [ pathString ] = {
270
- parent,
271
276
error : null ,
272
- ctx
277
+ ctx : fieldCtx
273
278
}
274
279
}
275
280
276
281
return field
277
282
}
278
283
279
- function getParentField ( context , path ) {
280
- for ( let i = path . length - 1 ; i > 0 ; i -- ) {
281
- const field = getField ( context , path . slice ( 0 , i ) )
282
- if ( field ) {
283
- return field
284
- }
285
- }
286
-
287
- return {
288
- asyncResource : context . asyncResource
289
- }
290
- }
291
-
292
- function getField ( context , path ) {
293
- return context . fields [ path . join ( '.' ) ]
294
- }
295
-
296
284
function wrapFields ( type ) {
297
285
if ( ! type || ! type . _fields || patchedTypes . has ( type ) ) {
298
286
return
@@ -328,12 +316,14 @@ function wrapFieldType (field) {
328
316
function finishResolvers ( { fields } ) {
329
317
Object . keys ( fields ) . reverse ( ) . forEach ( key => {
330
318
const field = fields [ key ]
331
- const ctx = { field, finishTime : field . finishTime , ...field . ctx }
319
+ field . ctx . finishTime = field . finishTime
320
+ field . ctx . field = field
332
321
if ( field . error ) {
333
- ctx . error = field . error
334
- resolveErrorCh . publish ( ctx )
322
+ field . ctx . error = field . error
323
+ resolveErrorCh . publish ( field . ctx )
335
324
}
336
- finishResolveCh . publish ( ctx )
325
+ console . log ( 'finishResolvers' , field )
326
+ finishResolveCh . publish ( field . ctx )
337
327
} )
338
328
}
339
329
0 commit comments