1
- import { GraphQLError } from 'graphql' ;
1
+ import { GraphQLError , responsePathAsArray , GraphQLResolveInfo } from 'graphql' ;
2
2
3
3
export const ERROR_SYMBOL = Symbol ( 'subschemaErrors' ) ;
4
4
5
+ export interface RelativeGraphQLError {
6
+ relativePath : Array < string | number > ;
7
+ graphQLError : GraphQLError ;
8
+ }
9
+
5
10
export function relocatedError ( originalError : GraphQLError , path ?: ReadonlyArray < string | number > ) : GraphQLError {
6
11
return new GraphQLError (
7
12
originalError . message ,
@@ -14,33 +19,55 @@ export function relocatedError(originalError: GraphQLError, path?: ReadonlyArray
14
19
) ;
15
20
}
16
21
17
- export function slicedError ( originalError : GraphQLError ) {
18
- return relocatedError ( originalError , originalError . path != null ? originalError . path . slice ( 1 ) : undefined ) ;
22
+ export function sliceRelativeError ( error : RelativeGraphQLError ) : RelativeGraphQLError {
23
+ return {
24
+ ...error ,
25
+ relativePath : error . relativePath ?. slice ( 1 ) ,
26
+ } ;
27
+ }
28
+
29
+ export function toRelativeErrors (
30
+ errors : ReadonlyArray < GraphQLError > ,
31
+ info : GraphQLResolveInfo
32
+ ) : Array < RelativeGraphQLError > {
33
+ return errors . map ( error => {
34
+ const relativePath = error . path ?. slice ( ) || [ ] ;
35
+ const sourcePath = info != null ? responsePathAsArray ( info . path ) : [ ] ;
36
+ return {
37
+ relativePath,
38
+ graphQLError : relocatedError ( error , sourcePath . concat ( relativePath . slice ( 1 ) ) ) ,
39
+ } ;
40
+ } ) ;
19
41
}
20
42
21
- export function getErrorsByPathSegment ( errors : ReadonlyArray < GraphQLError > ) : Record < string , Array < GraphQLError > > {
22
- const record = Object . create ( null ) ;
43
+ export function setErrors ( result : any , errors : Array < RelativeGraphQLError > ) {
44
+ result [ ERROR_SYMBOL ] = errors ;
45
+ }
46
+
47
+ export function getErrorsByPathSegment (
48
+ errors : Array < RelativeGraphQLError >
49
+ ) : Record < string , Array < RelativeGraphQLError > > {
50
+ const record : Record < string , Array < RelativeGraphQLError > > = Object . create ( null ) ;
23
51
errors . forEach ( error => {
24
- if ( ! error . path || error . path . length < 2 ) {
52
+ if ( ! error . relativePath || error . relativePath . length < 2 ) {
25
53
return ;
26
54
}
27
55
28
- const pathSegment = error . path [ 1 ] ;
56
+ const pathSegment = error . relativePath [ 1 ] ;
29
57
30
- const current = pathSegment in record ? record [ pathSegment ] : [ ] ;
31
- current . push ( slicedError ( error ) ) ;
58
+ const current : Array < RelativeGraphQLError > = pathSegment in record ? record [ pathSegment ] : [ ] ;
59
+ current . push ( {
60
+ relativePath : error . relativePath . slice ( 1 ) ,
61
+ graphQLError : error . graphQLError ,
62
+ } ) ;
32
63
record [ pathSegment ] = current ;
33
64
} ) ;
34
65
35
66
return record ;
36
67
}
37
68
38
- export function setErrors ( result : any , errors : Array < GraphQLError > ) {
39
- result [ ERROR_SYMBOL ] = errors ;
40
- }
41
-
42
- export function getErrors ( result : any , pathSegment : string ) : Array < GraphQLError > {
43
- const errors = result != null ? result [ ERROR_SYMBOL ] : result ;
69
+ export function getErrors ( result : any , pathSegment : string | number ) : Array < RelativeGraphQLError > {
70
+ const errors : Array < RelativeGraphQLError > = result != null ? result [ ERROR_SYMBOL ] : result ;
44
71
45
72
if ( ! Array . isArray ( errors ) ) {
46
73
return null ;
@@ -49,8 +76,8 @@ export function getErrors(result: any, pathSegment: string): Array<GraphQLError>
49
76
const fieldErrors = [ ] ;
50
77
51
78
for ( const error of errors ) {
52
- if ( ! error . path || error . path [ 0 ] === pathSegment ) {
53
- fieldErrors . push ( error ) ;
79
+ if ( ! error . relativePath || error . relativePath . length < 2 || error . relativePath [ 1 ] === pathSegment ) {
80
+ fieldErrors . push ( sliceRelativeError ( error ) ) ;
54
81
}
55
82
}
56
83
0 commit comments