Skip to content

Commit a91fdc6

Browse files
authored
feat: allow providing an object to the GraphQLError constructor (#3454)
1 parent 5caff03 commit a91fdc6

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/error/GraphQLError.ts

+43-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,41 @@ export interface GraphQLErrorExtensions {
2020
[attributeName: string]: unknown;
2121
}
2222

23+
export interface GraphQLErrorArgs {
24+
nodes?: ReadonlyArray<ASTNode> | ASTNode | null;
25+
source?: Maybe<Source>;
26+
positions?: Maybe<ReadonlyArray<number>>;
27+
path?: Maybe<ReadonlyArray<string | number>>;
28+
originalError?: Maybe<Error & { readonly extensions?: unknown }>;
29+
extensions?: Maybe<GraphQLErrorExtensions>;
30+
}
31+
32+
type BackwardsCompatibleArgs =
33+
| [args?: GraphQLErrorArgs]
34+
| [
35+
nodes?: GraphQLErrorArgs['nodes'],
36+
source?: GraphQLErrorArgs['source'],
37+
positions?: GraphQLErrorArgs['positions'],
38+
path?: GraphQLErrorArgs['path'],
39+
originalError?: GraphQLErrorArgs['originalError'],
40+
extensions?: GraphQLErrorArgs['extensions'],
41+
];
42+
43+
function toNormalizedArgs(args: BackwardsCompatibleArgs): GraphQLErrorArgs {
44+
const firstArg = args[0];
45+
if (firstArg == null || 'kind' in firstArg || 'length' in firstArg) {
46+
return {
47+
nodes: firstArg,
48+
source: args[1],
49+
positions: args[2],
50+
path: args[3],
51+
originalError: args[4],
52+
extensions: args[5],
53+
};
54+
}
55+
return firstArg;
56+
}
57+
2358
/**
2459
* A GraphQLError describes an Error found during the parse, validate, or
2560
* execute phases of performing a GraphQL operation. In addition to a message
@@ -76,6 +111,9 @@ export class GraphQLError extends Error {
76111
*/
77112
readonly extensions: GraphQLErrorExtensions;
78113

114+
/**
115+
* @deprecated Please use the `GraphQLErrorArgs` constructor overload instead.
116+
*/
79117
constructor(
80118
message: string,
81119
nodes?: ReadonlyArray<ASTNode> | ASTNode | null,
@@ -84,7 +122,11 @@ export class GraphQLError extends Error {
84122
path?: Maybe<ReadonlyArray<string | number>>,
85123
originalError?: Maybe<Error & { readonly extensions?: unknown }>,
86124
extensions?: Maybe<GraphQLErrorExtensions>,
87-
) {
125+
);
126+
constructor(message: string, args?: GraphQLErrorArgs);
127+
constructor(message: string, ...rawArgs: BackwardsCompatibleArgs) {
128+
const { nodes, source, positions, path, originalError, extensions } =
129+
toNormalizedArgs(rawArgs);
88130
super(message);
89131

90132
this.name = 'GraphQLError';

src/error/__tests__/GraphQLError-test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,22 @@ describe('toJSON', () => {
353353
extensions: { foo: 'bar' },
354354
});
355355
});
356+
357+
it('can be created with the alternative object argument', () => {
358+
const error = new GraphQLError('msg', {
359+
nodes: [operationNode],
360+
source,
361+
positions: [6],
362+
path: ['path', 2, 'a'],
363+
originalError: new Error('I like turtles'),
364+
extensions: { hee: 'I like turtles' },
365+
});
366+
367+
expect(error.toJSON()).to.deep.equal({
368+
message: 'msg',
369+
locations: [{ column: 5, line: 2 }],
370+
path: ['path', 2, 'a'],
371+
extensions: { hee: 'I like turtles' },
372+
});
373+
});
356374
});

0 commit comments

Comments
 (0)