Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow providing an object to the GraphQLError constructor #3454

Merged
merged 6 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/error/GraphQLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,41 @@ export interface GraphQLErrorExtensions {
[attributeName: string]: unknown;
}

export interface GraphQLErrorArgs {
nodes?: ReadonlyArray<ASTNode> | ASTNode | null;
source?: Maybe<Source>;
positions?: Maybe<ReadonlyArray<number>>;
path?: Maybe<ReadonlyArray<string | number>>;
originalError?: Maybe<Error & { readonly extensions?: unknown }>;
extensions?: Maybe<GraphQLErrorExtensions>;
}

type BackwardsCompatibleArgs =
| [args?: GraphQLErrorArgs]
| [
nodes?: GraphQLErrorArgs['nodes'],
source?: GraphQLErrorArgs['source'],
positions?: GraphQLErrorArgs['positions'],
path?: GraphQLErrorArgs['path'],
originalError?: GraphQLErrorArgs['originalError'],
extensions?: GraphQLErrorArgs['extensions'],
];

function toNormalizedArgs(args: BackwardsCompatibleArgs): GraphQLErrorArgs {
const firstArg = args[0];
if (firstArg == null || 'kind' in firstArg || 'length' in firstArg) {
n1ru4l marked this conversation as resolved.
Show resolved Hide resolved
return {
nodes: firstArg,
source: args[1],
positions: args[2],
path: args[3],
originalError: args[4],
extensions: args[5],
};
}
return firstArg;
}

/**
* A GraphQLError describes an Error found during the parse, validate, or
* execute phases of performing a GraphQL operation. In addition to a message
Expand Down Expand Up @@ -76,6 +111,9 @@ export class GraphQLError extends Error {
*/
readonly extensions: GraphQLErrorExtensions;

/**
* @deprecated Please use the `GraphQLErrorArgs` constructor overload instead.
*/
constructor(
message: string,
nodes?: ReadonlyArray<ASTNode> | ASTNode | null,
Expand All @@ -84,7 +122,11 @@ export class GraphQLError extends Error {
path?: Maybe<ReadonlyArray<string | number>>,
originalError?: Maybe<Error & { readonly extensions?: unknown }>,
extensions?: Maybe<GraphQLErrorExtensions>,
) {
);
n1ru4l marked this conversation as resolved.
Show resolved Hide resolved
constructor(message: string, args?: GraphQLErrorArgs);
constructor(message: string, ...rawArgs: BackwardsCompatibleArgs) {
const { nodes, source, positions, path, originalError, extensions } =
toNormalizedArgs(rawArgs);
super(message);

this.name = 'GraphQLError';
Expand Down
18 changes: 18 additions & 0 deletions src/error/__tests__/GraphQLError-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,22 @@ describe('toJSON', () => {
extensions: { foo: 'bar' },
});
});

it('can be created with the alternative object argument', () => {
const error = new GraphQLError('msg', {
nodes: [operationNode],
source,
positions: [6],
path: ['path', 2, 'a'],
originalError: new Error('I like turtles'),
extensions: { hee: 'I like turtles' },
});

expect(error.toJSON()).to.deep.equal({
message: 'msg',
locations: [{ column: 5, line: 2 }],
path: ['path', 2, 'a'],
extensions: { hee: 'I like turtles' },
});
});
});