From 4729e6cdd06ca3954220263647455f92ab5dcdb1 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Tue, 26 Oct 2021 19:42:40 +0300 Subject: [PATCH] GraphQLError: restore order of enumerable fields Was unintentionally changed in #3333 --- src/error/GraphQLError.js | 3 +- src/error/__tests__/GraphQLError-test.js | 58 ++++++++++++++++-------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/error/GraphQLError.js b/src/error/GraphQLError.js index fc462e5a48..7552bb473d 100644 --- a/src/error/GraphQLError.js +++ b/src/error/GraphQLError.js @@ -75,7 +75,6 @@ export class GraphQLError extends Error { super(message); this.name = 'GraphQLError'; - this.path = path ?? undefined; this.originalError = originalError ?? undefined; // Compute list of blame nodes. @@ -101,6 +100,8 @@ export class GraphQLError extends Error { ? positions.map((pos) => getLocation(source, pos)) : nodeLocations?.map((loc) => getLocation(loc.source, loc.start)); + this.path = path ?? undefined; + this.extensions = extensions ?? {}; const originalExtensions = originalError?.extensions; diff --git a/src/error/__tests__/GraphQLError-test.js b/src/error/__tests__/GraphQLError-test.js index 890525a4d9..44fb505e12 100644 --- a/src/error/__tests__/GraphQLError-test.js +++ b/src/error/__tests__/GraphQLError-test.js @@ -122,29 +122,47 @@ describe('GraphQLError', () => { }); }); - it('serializes to include message', () => { - const e = new GraphQLError('msg'); - expect(JSON.stringify(e)).to.equal('{"message":"msg"}'); - }); + it('serializes to include all standard fields', () => { + const eShort = new GraphQLError('msg'); + expect(JSON.stringify(eShort, null, 2) + '\n').to.equal(dedent` + { + "message": "msg" + } + `); - it('serializes to include message and locations', () => { - const e = new GraphQLError('msg', fieldNode); - expect(JSON.stringify(e)).to.equal( - '{"message":"msg","locations":[{"line":2,"column":3}]}', + const path = ['path', 2, 'field']; + const extensions = { foo: 'bar' }; + const eFull = new GraphQLError( + 'msg', + fieldNode, + undefined, + undefined, + path, + undefined, + extensions, ); - }); - it('serializes to include path', () => { - const e = new GraphQLError('msg', null, null, null, [ - 'path', - 3, - 'to', - 'field', - ]); - expect(e).to.have.deep.property('path', ['path', 3, 'to', 'field']); - expect(JSON.stringify(e)).to.equal( - '{"message":"msg","path":["path",3,"to","field"]}', - ); + // We should try to keep order of fields stable + // Changing it wouldn't be breaking change but will fail some tests in other libraries. + expect(JSON.stringify(eFull, null, 2) + '\n').to.equal(dedent` + { + "message": "msg", + "locations": [ + { + "line": 2, + "column": 3 + } + ], + "path": [ + "path", + 2, + "field" + ], + "extensions": { + "foo": "bar" + } + } + `); }); });