Skip to content

Commit c3e94a3

Browse files
committed
Make GraphQL properties non-editable and non-enumerable
This will make writing tests using deep.equal easier and not require using containsSubset which can lead to false positives.
1 parent 0e0f445 commit c3e94a3

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/error/GraphQLError.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,52 @@ export class GraphQLError extends Error {
1616
message: string;
1717
stack: string;
1818
nodes: ?Array<Node>;
19+
source: any;
1920
positions: any;
2021
locations: any;
21-
source: any;
2222

2323
constructor(
2424
message: string,
2525
// A flow bug keeps us from declaring nodes as an array of Node
2626
nodes?: Array<any/* Node */>,
27-
stack?: any
27+
stack?: ?string
2828
) {
2929
super(message);
3030
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;
3250
if (nodes) {
33-
this.nodes = nodes;
3451
var positions = nodes.map(node => node.loc && node.loc.start);
3552
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;
4354
}
4455
}
4556
}
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

Comments
 (0)