From 317172ee757b4fa2f5fd9ad59c24db358cc6b465 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Tue, 11 Jan 2022 14:25:28 +0100 Subject: [PATCH 1/6] feat: allow providing an object to GraphQLError --- src/error/GraphQLError.ts | 56 ++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/src/error/GraphQLError.ts b/src/error/GraphQLError.ts index 6d6677fa16..dcec7edd8d 100644 --- a/src/error/GraphQLError.ts +++ b/src/error/GraphQLError.ts @@ -20,6 +20,43 @@ export interface GraphQLErrorExtensions { [attributeName: string]: unknown; } +interface GraphQLErrorArgs { + message: string; + nodes?: ReadonlyArray | ASTNode | null; + source?: Maybe; + positions?: Maybe>; + path?: Maybe>; + originalError?: Maybe; + extensions?: Maybe; +} + +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 @@ -76,15 +113,16 @@ export class GraphQLError extends Error { */ readonly extensions: GraphQLErrorExtensions; - constructor( - message: string, - nodes?: ReadonlyArray | ASTNode | null, - source?: Maybe, - positions?: Maybe>, - path?: Maybe>, - originalError?: Maybe, - extensions?: Maybe, - ) { + constructor(...rawArgs: BackwardsCompatibleArgs) { + const { + message, + nodes, + source, + positions, + path, + originalError, + extensions, + } = toNormalizedArgs(rawArgs); super(message); this.name = 'GraphQLError'; From e3c59b5831b434e079f8bbacd4b8059a23c7195f Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Tue, 11 Jan 2022 14:42:26 +0100 Subject: [PATCH 2/6] cover stuff with tests --- src/error/__tests__/GraphQLError-test.ts | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/error/__tests__/GraphQLError-test.ts b/src/error/__tests__/GraphQLError-test.ts index 21e1ab1f5b..d1d6f4e61d 100644 --- a/src/error/__tests__/GraphQLError-test.ts +++ b/src/error/__tests__/GraphQLError-test.ts @@ -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('Hee-HOOOO'), + extensions: { hee: 'hoooo' }, + }); + + expect(error.toJSON()).to.deep.equal({ + extensions: { + hee: 'hoooo', + }, + locations: [ + { + column: 5, + line: 2, + }, + ], + message: 'msg', + path: ['path', 2, 'a'], + }); + }); }); From fa5d1b9b583b658d69a026834bb78e75d1cde6f1 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Tue, 11 Jan 2022 14:46:45 +0100 Subject: [PATCH 3/6] use funny quote for pleasing cspell --- src/error/__tests__/GraphQLError-test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/error/__tests__/GraphQLError-test.ts b/src/error/__tests__/GraphQLError-test.ts index d1d6f4e61d..24a7b22287 100644 --- a/src/error/__tests__/GraphQLError-test.ts +++ b/src/error/__tests__/GraphQLError-test.ts @@ -361,13 +361,13 @@ describe('toJSON', () => { source, positions: [6], path: ['path', 2, 'a'], - originalError: new Error('Hee-HOOOO'), - extensions: { hee: 'hoooo' }, + originalError: new Error('I like turtles'), + extensions: { hee: 'I like turtles' }, }); expect(error.toJSON()).to.deep.equal({ extensions: { - hee: 'hoooo', + hee: 'I like turtles', }, locations: [ { From 0adf5f2a51e8bbf617cf9820b605d1befa4e64c4 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Mon, 17 Jan 2022 12:32:32 +0100 Subject: [PATCH 4/6] error message is always the first argument --- src/error/GraphQLError.ts | 45 ++++++++++++------------ src/error/__tests__/GraphQLError-test.ts | 3 +- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/error/GraphQLError.ts b/src/error/GraphQLError.ts index dcec7edd8d..b7e7ee8e1f 100644 --- a/src/error/GraphQLError.ts +++ b/src/error/GraphQLError.ts @@ -21,7 +21,6 @@ export interface GraphQLErrorExtensions { } interface GraphQLErrorArgs { - message: string; nodes?: ReadonlyArray | ASTNode | null; source?: Maybe; positions?: Maybe>; @@ -31,9 +30,8 @@ interface GraphQLErrorArgs { } type BackwardsCompatibleArgs = - | [GraphQLErrorArgs] + | [args?: GraphQLErrorArgs] | [ - message: GraphQLErrorArgs['message'], nodes?: GraphQLErrorArgs['nodes'], source?: GraphQLErrorArgs['source'], positions?: GraphQLErrorArgs['positions'], @@ -43,18 +41,18 @@ type BackwardsCompatibleArgs = ]; function toNormalizedArgs(args: BackwardsCompatibleArgs): GraphQLErrorArgs { - if (typeof args[0] === 'string') { + const firstArg = args[0]; + if (firstArg == null || 'kind' in firstArg || 'length' in firstArg) { return { - message: args[0], - nodes: args[1], - source: args[2], - positions: args[3], - path: args[4], - originalError: args[5], - extensions: args[6], + nodes: firstArg, + source: args[1], + positions: args[2], + path: args[3], + originalError: args[4], + extensions: args[5], }; } - return args[0]; + return firstArg; } /** @@ -113,16 +111,19 @@ export class GraphQLError extends Error { */ readonly extensions: GraphQLErrorExtensions; - constructor(...rawArgs: BackwardsCompatibleArgs) { - const { - message, - nodes, - source, - positions, - path, - originalError, - extensions, - } = toNormalizedArgs(rawArgs); + constructor( + message: string, + nodes?: GraphQLErrorArgs['nodes'], + source?: GraphQLErrorArgs['source'], + positions?: GraphQLErrorArgs['positions'], + path?: GraphQLErrorArgs['path'], + originalError?: GraphQLErrorArgs['originalError'], + extensions?: GraphQLErrorArgs['extensions'], + ); + constructor(message: string, args?: GraphQLErrorArgs); + constructor(message: string, ...rawArgs: BackwardsCompatibleArgs) { + const { nodes, source, positions, path, originalError, extensions } = + toNormalizedArgs(rawArgs); super(message); this.name = 'GraphQLError'; diff --git a/src/error/__tests__/GraphQLError-test.ts b/src/error/__tests__/GraphQLError-test.ts index 24a7b22287..76bce409d1 100644 --- a/src/error/__tests__/GraphQLError-test.ts +++ b/src/error/__tests__/GraphQLError-test.ts @@ -355,8 +355,7 @@ describe('toJSON', () => { }); it('can be created with the alternative object argument', () => { - const error = new GraphQLError({ - message: 'msg', + const error = new GraphQLError('msg', { nodes: [operationNode], source, positions: [6], From 953b0c39678fda7b751ee7fb6966c992203726b2 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Mon, 17 Jan 2022 13:27:37 +0100 Subject: [PATCH 5/6] add deprecation notice --- src/error/GraphQLError.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/error/GraphQLError.ts b/src/error/GraphQLError.ts index b7e7ee8e1f..c859f59e46 100644 --- a/src/error/GraphQLError.ts +++ b/src/error/GraphQLError.ts @@ -20,7 +20,7 @@ export interface GraphQLErrorExtensions { [attributeName: string]: unknown; } -interface GraphQLErrorArgs { +export interface GraphQLErrorArgs { nodes?: ReadonlyArray | ASTNode | null; source?: Maybe; positions?: Maybe>; @@ -111,14 +111,17 @@ export class GraphQLError extends Error { */ readonly extensions: GraphQLErrorExtensions; + /** + * @deprecated Please use the `GraphQLErrorArgs` constructor overload instead. + */ constructor( message: string, - nodes?: GraphQLErrorArgs['nodes'], - source?: GraphQLErrorArgs['source'], - positions?: GraphQLErrorArgs['positions'], - path?: GraphQLErrorArgs['path'], - originalError?: GraphQLErrorArgs['originalError'], - extensions?: GraphQLErrorArgs['extensions'], + nodes?: ReadonlyArray | ASTNode | null, + source?: Maybe, + positions?: Maybe>, + path?: Maybe>, + originalError?: Maybe, + extensions?: Maybe, ); constructor(message: string, args?: GraphQLErrorArgs); constructor(message: string, ...rawArgs: BackwardsCompatibleArgs) { From 6beec17cac592cf096e1223616cef5c04627c197 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Mon, 17 Jan 2022 13:30:53 +0100 Subject: [PATCH 6/6] format and order --- src/error/__tests__/GraphQLError-test.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/error/__tests__/GraphQLError-test.ts b/src/error/__tests__/GraphQLError-test.ts index 76bce409d1..60f35e9b6c 100644 --- a/src/error/__tests__/GraphQLError-test.ts +++ b/src/error/__tests__/GraphQLError-test.ts @@ -365,17 +365,10 @@ describe('toJSON', () => { }); expect(error.toJSON()).to.deep.equal({ - extensions: { - hee: 'I like turtles', - }, - locations: [ - { - column: 5, - line: 2, - }, - ], message: 'msg', + locations: [{ column: 5, line: 2 }], path: ['path', 2, 'a'], + extensions: { hee: 'I like turtles' }, }); }); });