Skip to content

Commit

Permalink
GraphQLError: enumarate only spec prescribed properties
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Oct 21, 2021
1 parent b936411 commit a69b8b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/error/GraphQLError.ts
Expand Up @@ -117,6 +117,17 @@ export class GraphQLError extends Error {
: undefined;
this.extensions = extensions ?? originalExtensions ?? Object.create(null);

// Only properties prescribed by the spec should be enumeratable.
// Keep the rest as non-enumeratable.
Object.defineProperties(this, {
message: { enumerable: true },
name: { enumerable: false },
nodes: { enumerable: false },
source: { enumerable: false },
positions: { enumerable: false },
originalError: { enumerable: false },
});

// Include (non-enumerable) stack trace.
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
if (originalError?.stack) {
Expand Down
14 changes: 14 additions & 0 deletions src/error/__tests__/GraphQLError-test.ts
Expand Up @@ -39,6 +39,20 @@ describe('GraphQLError', () => {
expect(e.stack).to.be.a('string');
});

it('enumerate only properties prescribed by the spec', () => {
const e = new GraphQLError(
'msg' /* message */,
[fieldNode] /* nodes */,
source /* source */,
[1, 2, 3] /* positions */,
['a', 'b', 'c'] /* path */,
new Error('test') /* originalError */,
{ foo: 'bar' } /* extensions */,
);

expect(Object.keys(e)).to.deep.equal(['message', 'path', 'locations', 'extensions']);
});

it('uses the stack of an original error', () => {
const original = new Error('original');
const e = new GraphQLError(
Expand Down

0 comments on commit a69b8b4

Please sign in to comment.