@@ -16,31 +16,52 @@ export class GraphQLError extends Error {
16
16
message : string ;
17
17
stack : string ;
18
18
nodes : ?Array < Node > ;
19
+ source : any ;
19
20
positions : any ;
20
21
locations : any ;
21
- source : any ;
22
22
23
23
constructor (
24
24
message : string ,
25
25
// A flow bug keeps us from declaring nodes as an array of Node
26
26
nodes ?: Array < any /* Node */ > ,
27
- stack ?: any
27
+ stack ?: ? string
28
28
) {
29
29
super ( message ) ;
30
30
this . message = message ;
31
- this . stack = stack || message ;
31
+ Object . defineProperty ( this , 'stack' , { value : stack || message } ) ;
32
+ Object . defineProperty ( this , 'nodes' , { value : nodes } ) ;
33
+ }
34
+ }
35
+
36
+ // Note: flow does not yet know about Object.defineProperty with `get`.
37
+ Object . defineProperty ( GraphQLError . prototype , 'source' , ( {
38
+ get ( ) {
39
+ var nodes = this . nodes ;
40
+ if ( nodes && nodes . length > 0 ) {
41
+ var node = nodes [ 0 ] ;
42
+ return node && node . loc && node . loc . source ;
43
+ }
44
+ }
45
+ } : any ) ) ;
46
+
47
+ Object . defineProperty ( GraphQLError . prototype , 'positions' , ( {
48
+ get ( ) {
49
+ var nodes = this . nodes ;
32
50
if ( nodes ) {
33
- this . nodes = nodes ;
34
51
var positions = nodes . map ( node => node . loc && node . loc . start ) ;
35
52
if ( positions . some ( p => p ) ) {
36
- this . positions = positions ;
37
- var loc = nodes [ 0 ] . loc ;
38
- var source = loc && loc . source ;
39
- if ( source ) {
40
- this . locations = positions . map ( pos => getLocation ( source , pos ) ) ;
41
- this . source = source ;
42
- }
53
+ return positions ;
43
54
}
44
55
}
45
56
}
46
- }
57
+ } : any ) ) ;
58
+
59
+ Object . defineProperty ( GraphQLError . prototype , 'locations' , ( {
60
+ get ( ) {
61
+ var positions = this . positions ;
62
+ var source = this . source ;
63
+ if ( positions && source ) {
64
+ return positions . map ( pos => getLocation ( source , pos ) ) ;
65
+ }
66
+ }
67
+ } : any ) ) ;
0 commit comments