Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQLError: enumerate only spec prescribed properties #3326

Merged
merged 1 commit into from Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/error/GraphQLError.ts
Expand Up @@ -117,6 +117,20 @@ export class GraphQLError extends Error {
: undefined;
this.extensions = extensions ?? originalExtensions ?? Object.create(null);

// Only properties prescribed by the spec should be enumerable.
// Keep the rest as non-enumerable.
Object.defineProperties(this, {
message: {
writable: true,
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
19 changes: 19 additions & 0 deletions src/error/__tests__/GraphQLError-test.ts
Expand Up @@ -39,6 +39,25 @@ 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