diff --git a/src/error/GraphQLError.ts b/src/error/GraphQLError.ts index eab15873a1..2a78545dbd 100644 --- a/src/error/GraphQLError.ts +++ b/src/error/GraphQLError.ts @@ -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) { diff --git a/src/error/__tests__/GraphQLError-test.ts b/src/error/__tests__/GraphQLError-test.ts index d2b551c205..7599c15c9f 100644 --- a/src/error/__tests__/GraphQLError-test.ts +++ b/src/error/__tests__/GraphQLError-test.ts @@ -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(