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 3 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
56 changes: 47 additions & 9 deletions src/error/GraphQLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,43 @@ export interface GraphQLErrorExtensions {
[attributeName: string]: unknown;
}

interface GraphQLErrorArgs {
message: string;
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 =
| [GraphQLErrorArgs]
| [
message: GraphQLErrorArgs['message'],
nodes?: GraphQLErrorArgs['nodes'],
source?: GraphQLErrorArgs['source'],
positions?: GraphQLErrorArgs['positions'],
path?: GraphQLErrorArgs['path'],
originalError?: GraphQLErrorArgs['originalError'],
extensions?: GraphQLErrorArgs['extensions'],
];

function toNormalizedArgs(args: BackwardsCompatibleArgs): GraphQLErrorArgs {
if (typeof args[0] === 'string') {
return {
message: args[0],
nodes: args[1],
source: args[2],
positions: args[3],
path: args[4],
originalError: args[5],
extensions: args[6],
};
}
return args[0];
}

/**
* 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,15 +113,16 @@ export class GraphQLError extends Error {
*/
readonly extensions: GraphQLErrorExtensions;

constructor(
message: string,
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>,
) {
constructor(...rawArgs: BackwardsCompatibleArgs) {
const {
message,
nodes,
source,
positions,
path,
originalError,
extensions,
} = toNormalizedArgs(rawArgs);
n1ru4l marked this conversation as resolved.
Show resolved Hide resolved
super(message);

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

it('can be created with the alternative object argument', () => {
const error = new GraphQLError({
message: '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({
extensions: {
n1ru4l marked this conversation as resolved.
Show resolved Hide resolved
hee: 'I like turtles',
},
locations: [
{
column: 5,
line: 2,
},
],
message: 'msg',
path: ['path', 2, 'a'],
});
});
});