Skip to content

Commit 6281c06

Browse files
GraphQLError: restore order of enumerable fields (#3335)
1 parent f49cf34 commit 6281c06

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

src/error/GraphQLError.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ export class GraphQLError extends Error {
7575
super(message);
7676

7777
this.name = 'GraphQLError';
78-
this.path = path ?? undefined;
7978
this.originalError = originalError ?? undefined;
8079

8180
// Compute list of blame nodes.
@@ -101,6 +100,8 @@ export class GraphQLError extends Error {
101100
? positions.map((pos) => getLocation(source, pos))
102101
: nodeLocations?.map((loc) => getLocation(loc.source, loc.start));
103102

103+
this.path = path ?? undefined;
104+
104105
this.extensions = extensions ?? {};
105106

106107
const originalExtensions = originalError?.extensions;

src/error/__tests__/GraphQLError-test.js

+38-20
Original file line numberDiff line numberDiff line change
@@ -122,29 +122,47 @@ describe('GraphQLError', () => {
122122
});
123123
});
124124

125-
it('serializes to include message', () => {
126-
const e = new GraphQLError('msg');
127-
expect(JSON.stringify(e)).to.equal('{"message":"msg"}');
128-
});
125+
it('serializes to include all standard fields', () => {
126+
const eShort = new GraphQLError('msg');
127+
expect(JSON.stringify(eShort, null, 2) + '\n').to.equal(dedent`
128+
{
129+
"message": "msg"
130+
}
131+
`);
129132

130-
it('serializes to include message and locations', () => {
131-
const e = new GraphQLError('msg', fieldNode);
132-
expect(JSON.stringify(e)).to.equal(
133-
'{"message":"msg","locations":[{"line":2,"column":3}]}',
133+
const path = ['path', 2, 'field'];
134+
const extensions = { foo: 'bar' };
135+
const eFull = new GraphQLError(
136+
'msg',
137+
fieldNode,
138+
undefined,
139+
undefined,
140+
path,
141+
undefined,
142+
extensions,
134143
);
135-
});
136144

137-
it('serializes to include path', () => {
138-
const e = new GraphQLError('msg', null, null, null, [
139-
'path',
140-
3,
141-
'to',
142-
'field',
143-
]);
144-
expect(e).to.have.deep.property('path', ['path', 3, 'to', 'field']);
145-
expect(JSON.stringify(e)).to.equal(
146-
'{"message":"msg","path":["path",3,"to","field"]}',
147-
);
145+
// We should try to keep order of fields stable
146+
// Changing it wouldn't be breaking change but will fail some tests in other libraries.
147+
expect(JSON.stringify(eFull, null, 2) + '\n').to.equal(dedent`
148+
{
149+
"message": "msg",
150+
"locations": [
151+
{
152+
"line": 2,
153+
"column": 3
154+
}
155+
],
156+
"path": [
157+
"path",
158+
2,
159+
"field"
160+
],
161+
"extensions": {
162+
"foo": "bar"
163+
}
164+
}
165+
`);
148166
});
149167
});
150168

0 commit comments

Comments
 (0)