@@ -27,7 +27,8 @@ export default class CheckResultAndHandleErrors implements Transform {
2727 delegationContext . fieldName ,
2828 delegationContext . subschema ,
2929 delegationContext . returnType ,
30- delegationContext . skipTypeMerging
30+ delegationContext . skipTypeMerging ,
31+ delegationContext . onLocatedError
3132 ) ;
3233 }
3334}
@@ -39,12 +40,14 @@ export function checkResultAndHandleErrors(
3940 responseKey : string = getResponseKeyFromInfo ( info ) ,
4041 subschema ?: GraphQLSchema | SubschemaConfig ,
4142 returnType : GraphQLOutputType = info . returnType ,
42- skipTypeMerging ?: boolean
43+ skipTypeMerging ?: boolean ,
44+ onLocatedError ?: ( originalError : GraphQLError ) => GraphQLError
4345) : any {
4446 const { data, unpathedErrors } = mergeDataAndErrors (
4547 result . data == null ? undefined : result . data [ responseKey ] ,
4648 result . errors == null ? [ ] : result . errors ,
47- info ? responsePathAsArray ( info . path ) : undefined
49+ info ? responsePathAsArray ( info . path ) : undefined ,
50+ onLocatedError
4851 ) ;
4952
5053 return resolveExternalValue ( data , unpathedErrors , subschema , context , info , returnType , skipTypeMerging ) ;
@@ -54,6 +57,7 @@ export function mergeDataAndErrors(
5457 data : any ,
5558 errors : ReadonlyArray < GraphQLError > ,
5659 path : Array < string | number > ,
60+ onLocatedError : ( originalError : GraphQLError ) => GraphQLError ,
5761 index = 1
5862) : { data : any ; unpathedErrors : Array < GraphQLError > } {
5963 if ( data == null ) {
@@ -62,13 +66,16 @@ export function mergeDataAndErrors(
6266 }
6367
6468 if ( errors . length === 1 ) {
65- const error = errors [ 0 ] ;
69+ const error = onLocatedError ? onLocatedError ( errors [ 0 ] ) : errors [ 0 ] ;
6670 const newPath =
6771 path === undefined ? error . path : error . path === undefined ? path : path . concat ( error . path . slice ( 1 ) ) ;
72+
6873 return { data : relocatedError ( errors [ 0 ] , newPath ) , unpathedErrors : [ ] } ;
6974 }
7075
71- return { data : locatedError ( new AggregateError ( errors ) , undefined , path ) , unpathedErrors : [ ] } ;
76+ const newError = locatedError ( new AggregateError ( errors ) , undefined , path ) ;
77+
78+ return { data : newError , unpathedErrors : [ ] } ;
7279 }
7380
7481 if ( ! errors . length ) {
@@ -98,6 +105,7 @@ export function mergeDataAndErrors(
98105 data [ pathSegment ] ,
99106 errorMap [ pathSegment ] ,
100107 path ,
108+ onLocatedError ,
101109 index + 1
102110 ) ;
103111 data [ pathSegment ] = newData ;
0 commit comments