Skip to content

Commit 22797c0

Browse files
committed
GraphQLError: enumarate only spec prescribed properties
1 parent b936411 commit 22797c0

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/error/GraphQLError.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ export class GraphQLError extends Error {
117117
: undefined;
118118
this.extensions = extensions ?? originalExtensions ?? Object.create(null);
119119

120+
// Only properties prescribed by the spec should be enumerable.
121+
// Keep the rest as non-enumerable.
122+
Object.defineProperties(this, {
123+
message: {
124+
writable: true,
125+
enumerable: true,
126+
},
127+
name: { enumerable: false },
128+
nodes: { enumerable: false },
129+
source: { enumerable: false },
130+
positions: { enumerable: false },
131+
originalError: { enumerable: false },
132+
});
133+
120134
// Include (non-enumerable) stack trace.
121135
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
122136
if (originalError?.stack) {

src/error/__tests__/GraphQLError-test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ describe('GraphQLError', () => {
3939
expect(e.stack).to.be.a('string');
4040
});
4141

42+
it('enumerate only properties prescribed by the spec', () => {
43+
const e = new GraphQLError(
44+
'msg' /* message */,
45+
[fieldNode] /* nodes */,
46+
source /* source */,
47+
[1, 2, 3] /* positions */,
48+
['a', 'b', 'c'] /* path */,
49+
new Error('test') /* originalError */,
50+
{ foo: 'bar' } /* extensions */,
51+
);
52+
53+
expect(Object.keys(e)).to.deep.equal([
54+
'message',
55+
'path',
56+
'locations',
57+
'extensions',
58+
]);
59+
});
60+
4261
it('uses the stack of an original error', () => {
4362
const original = new Error('original');
4463
const e = new GraphQLError(

0 commit comments

Comments
 (0)