Skip to content

Commit ff06428

Browse files
authoredApr 7, 2022
refactor: use object for GraphQLError constructor (#3465)
* refactor: use object for GraphQLError constructor * apply #3465 (comment)
1 parent 5f247e0 commit ff06428

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+170
-216
lines changed
 

‎src/error/__tests__/GraphQLError-test.ts

+46-77
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ describe('GraphQLError', () => {
4040
});
4141

4242
it('enumerate only properties prescribed by the spec', () => {
43-
const e = new GraphQLError(
44-
'msg' /* message */,
45-
[fieldNode] /* nodes */,
46-
source /* source */,
47-
[1, 2, 3] /* positions */,
48-
['a', 'b', 'c'] /* path */,
49-
new Error('test') /* originalError */,
50-
{ foo: 'bar' } /* extensions */,
51-
);
43+
const e = new GraphQLError('msg' /* message */, {
44+
nodes: [fieldNode],
45+
source,
46+
positions: [1, 2, 3],
47+
path: ['a', 'b', 'c'],
48+
originalError: new Error('test'),
49+
extensions: { foo: 'bar' },
50+
});
5251

5352
expect(Object.keys(e)).to.deep.equal([
5453
'message',
@@ -60,14 +59,9 @@ describe('GraphQLError', () => {
6059

6160
it('uses the stack of an original error', () => {
6261
const original = new Error('original');
63-
const e = new GraphQLError(
64-
'msg',
65-
undefined,
66-
undefined,
67-
undefined,
68-
undefined,
69-
original,
70-
);
62+
const e = new GraphQLError('msg', {
63+
originalError: original,
64+
});
7165

7266
expect(e).to.include({
7367
name: 'GraphQLError',
@@ -79,7 +73,7 @@ describe('GraphQLError', () => {
7973

8074
it('creates new stack if original error has no stack', () => {
8175
const original = new Error('original');
82-
const e = new GraphQLError('msg', null, null, null, null, original);
76+
const e = new GraphQLError('msg', { originalError: original });
8377

8478
expect(e).to.include({
8579
name: 'GraphQLError',
@@ -90,7 +84,7 @@ describe('GraphQLError', () => {
9084
});
9185

9286
it('converts nodes to positions and locations', () => {
93-
const e = new GraphQLError('msg', [fieldNode]);
87+
const e = new GraphQLError('msg', { nodes: [fieldNode] });
9488
expect(e).to.deep.include({
9589
source,
9690
nodes: [fieldNode],
@@ -100,7 +94,7 @@ describe('GraphQLError', () => {
10094
});
10195

10296
it('converts single node to positions and locations', () => {
103-
const e = new GraphQLError('msg', fieldNode); // Non-array value.
97+
const e = new GraphQLError('msg', { nodes: fieldNode }); // Non-array value.
10498
expect(e).to.deep.include({
10599
source,
106100
nodes: [fieldNode],
@@ -110,7 +104,7 @@ describe('GraphQLError', () => {
110104
});
111105

112106
it('converts node with loc.start === 0 to positions and locations', () => {
113-
const e = new GraphQLError('msg', operationNode);
107+
const e = new GraphQLError('msg', { nodes: operationNode });
114108
expect(e).to.deep.include({
115109
source,
116110
nodes: [operationNode],
@@ -125,7 +119,7 @@ describe('GraphQLError', () => {
125119
loc: undefined,
126120
};
127121

128-
const e = new GraphQLError('msg', fieldNodeNoLocation);
122+
const e = new GraphQLError('msg', { nodes: fieldNodeNoLocation });
129123
expect(e).to.deep.include({
130124
nodes: [fieldNodeNoLocation],
131125
source: undefined,
@@ -135,7 +129,7 @@ describe('GraphQLError', () => {
135129
});
136130

137131
it('converts source and positions to locations', () => {
138-
const e = new GraphQLError('msg', null, source, [6]);
132+
const e = new GraphQLError('msg', { source, positions: [6] });
139133
expect(e).to.deep.include({
140134
source,
141135
nodes: undefined,
@@ -155,47 +149,31 @@ describe('GraphQLError', () => {
155149
}
156150

157151
const original = new ErrorWithExtensions('original');
158-
const inheritedExtensions = new GraphQLError(
159-
'InheritedExtensions',
160-
undefined,
161-
undefined,
162-
undefined,
163-
undefined,
164-
original,
165-
undefined,
166-
);
152+
const inheritedExtensions = new GraphQLError('InheritedExtensions', {
153+
originalError: original,
154+
});
167155

168156
expect(inheritedExtensions).to.deep.include({
169157
message: 'InheritedExtensions',
170158
originalError: original,
171159
extensions: { original: 'extensions' },
172160
});
173161

174-
const ownExtensions = new GraphQLError(
175-
'OwnExtensions',
176-
undefined,
177-
undefined,
178-
undefined,
179-
undefined,
180-
original,
181-
{ own: 'extensions' },
182-
);
162+
const ownExtensions = new GraphQLError('OwnExtensions', {
163+
originalError: original,
164+
extensions: { own: 'extensions' },
165+
});
183166

184167
expect(ownExtensions).to.deep.include({
185168
message: 'OwnExtensions',
186169
originalError: original,
187170
extensions: { own: 'extensions' },
188171
});
189172

190-
const ownEmptyExtensions = new GraphQLError(
191-
'OwnEmptyExtensions',
192-
undefined,
193-
undefined,
194-
undefined,
195-
undefined,
196-
original,
197-
{},
198-
);
173+
const ownEmptyExtensions = new GraphQLError('OwnEmptyExtensions', {
174+
originalError: original,
175+
extensions: {},
176+
});
199177

200178
expect(ownEmptyExtensions).to.deep.include({
201179
message: 'OwnEmptyExtensions',
@@ -214,15 +192,11 @@ describe('GraphQLError', () => {
214192

215193
const path = ['path', 2, 'field'];
216194
const extensions = { foo: 'bar' };
217-
const eFull = new GraphQLError(
218-
'msg',
219-
fieldNode,
220-
undefined,
221-
undefined,
195+
const eFull = new GraphQLError('msg', {
196+
nodes: fieldNode,
222197
path,
223-
undefined,
224198
extensions,
225-
);
199+
});
226200

227201
// We should try to keep order of fields stable
228202
// Changing it wouldn't be breaking change but will fail some tests in other libraries.
@@ -260,10 +234,9 @@ describe('toString', () => {
260234
});
261235

262236
it('prints an error using node without location', () => {
263-
const error = new GraphQLError(
264-
'Error attached to node without location',
265-
parse('{ foo }', { noLocation: true }),
266-
);
237+
const error = new GraphQLError('Error attached to node without location', {
238+
nodes: parse('{ foo }', { noLocation: true }),
239+
});
267240
expect(error.toString()).to.equal(
268241
'Error attached to node without location',
269242
);
@@ -330,12 +303,7 @@ describe('toJSON', () => {
330303
});
331304

332305
it('includes path', () => {
333-
const error = new GraphQLError('msg', null, null, null, [
334-
'path',
335-
3,
336-
'to',
337-
'field',
338-
]);
306+
const error = new GraphQLError('msg', { path: ['path', 3, 'to', 'field'] });
339307

340308
expect(error.toJSON()).to.deep.equal({
341309
message: 'msg',
@@ -344,8 +312,8 @@ describe('toJSON', () => {
344312
});
345313

346314
it('includes extension fields', () => {
347-
const error = new GraphQLError('msg', null, null, null, null, null, {
348-
foo: 'bar',
315+
const error = new GraphQLError('msg', {
316+
extensions: { foo: 'bar' },
349317
});
350318

351319
expect(error.toJSON()).to.deep.equal({
@@ -354,15 +322,16 @@ describe('toJSON', () => {
354322
});
355323
});
356324

357-
it('can be created with the alternative object argument', () => {
358-
const error = new GraphQLError('msg', {
359-
nodes: [operationNode],
325+
it('can be created with the legacy argument list', () => {
326+
const error = new GraphQLError(
327+
'msg',
328+
[operationNode],
360329
source,
361-
positions: [6],
362-
path: ['path', 2, 'a'],
363-
originalError: new Error('I like turtles'),
364-
extensions: { hee: 'I like turtles' },
365-
});
330+
[6],
331+
['path', 2, 'a'],
332+
new Error('I like turtles'),
333+
{ hee: 'I like turtles' },
334+
);
366335

367336
expect(error.toJSON()).to.deep.equal({
368337
message: 'msg',

‎src/error/__tests__/locatedError-test.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import { locatedError } from '../locatedError';
66

77
describe('locatedError', () => {
88
it('passes GraphQLError through', () => {
9-
const e = new GraphQLError('msg', null, null, null, [
10-
'path',
11-
3,
12-
'to',
13-
'field',
14-
]);
9+
const e = new GraphQLError('msg', { path: ['path', 3, 'to', 'field'] });
1510

1611
expect(locatedError(e, [], [])).to.deep.equal(e);
1712
});

0 commit comments

Comments
 (0)
Please sign in to comment.