1
- import { mergeDeep , ERROR_SYMBOL } from '@graphql-tools/utils' ;
1
+ import { GraphQLError , GraphQLResolveInfo , responsePathAsArray , SelectionSetNode , GraphQLObjectType } from 'graphql' ;
2
+
3
+ import {
4
+ mergeDeep ,
5
+ ERROR_SYMBOL ,
6
+ extendedError ,
7
+ collectFields ,
8
+ GraphQLExecutionContext ,
9
+ relocatedError ,
10
+ } from '@graphql-tools/utils' ;
2
11
3
12
import { SubschemaConfig } from '../types' ;
4
13
import { OBJECT_SUBSCHEMA_SYMBOL , FIELD_SUBSCHEMA_MAP_SYMBOL } from '../symbols' ;
5
14
6
- export function mergeProxiedResults ( target : any , ...sources : Array < any > ) : any {
7
- const results = sources . filter ( source => ! ( source instanceof Error ) ) ;
15
+ export function mergeProxiedResults (
16
+ info : GraphQLResolveInfo ,
17
+ target : any ,
18
+ sources : Array < any > ,
19
+ selectionSets : Array < SelectionSetNode >
20
+ ) : any {
21
+ const results : Array < any > = [ ] ;
22
+ let errors : Array < GraphQLError > = [ ] ;
23
+
24
+ const path = responsePathAsArray ( info . path ) ;
25
+
26
+ sources . forEach ( ( source , index ) => {
27
+ if ( source instanceof GraphQLError ) {
28
+ const selectionSet = selectionSets [ index ] ;
29
+ const fieldNodes = collectFields (
30
+ {
31
+ schema : info . schema ,
32
+ variableValues : { } ,
33
+ fragments : { } ,
34
+ } as GraphQLExecutionContext ,
35
+ info . schema . getType ( target . __typename ) as GraphQLObjectType ,
36
+ selectionSet ,
37
+ Object . create ( null ) ,
38
+ Object . create ( null )
39
+ ) ;
40
+ const nullResult = { } ;
41
+ Object . keys ( fieldNodes ) . forEach ( responseKey => {
42
+ errors . push ( relocatedError ( source , [ responseKey ] ) ) ;
43
+ nullResult [ responseKey ] = null ;
44
+ } ) ;
45
+ results . push ( nullResult ) ;
46
+ } else {
47
+ errors = errors . concat ( source [ ERROR_SYMBOL ] ) ;
48
+ results . push ( source ) ;
49
+ }
50
+ } ) ;
51
+
8
52
const fieldSubschemaMap = results . reduce ( ( acc : Record < any , SubschemaConfig > , source : any ) => {
9
53
const subschema = source [ OBJECT_SUBSCHEMA_SYMBOL ] ;
10
54
Object . keys ( source ) . forEach ( key => {
@@ -18,8 +62,14 @@ export function mergeProxiedResults(target: any, ...sources: Array<any>): any {
18
62
? Object . assign ( { } , target [ FIELD_SUBSCHEMA_MAP_SYMBOL ] , fieldSubschemaMap )
19
63
: fieldSubschemaMap ;
20
64
21
- const errors = sources . map ( ( source : any ) => ( source instanceof Error ? source : source [ ERROR_SYMBOL ] ) ) ;
22
- result [ ERROR_SYMBOL ] = target [ ERROR_SYMBOL ] . concat ( ...errors ) ;
65
+ const annotatedErrors = errors . map ( error => {
66
+ return extendedError ( error , {
67
+ ...error . extensions ,
68
+ graphQLToolsMergedPath : error . path != null ? [ ...path , ...error . path ] : responsePathAsArray ( info . path ) ,
69
+ } ) ;
70
+ } ) ;
71
+
72
+ result [ ERROR_SYMBOL ] = target [ ERROR_SYMBOL ] . concat ( annotatedErrors ) ;
23
73
24
74
return result ;
25
75
}
0 commit comments