diff --git a/.eslintrc.yml b/.eslintrc.yml index dd2b76224c..31ff63986f 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -627,6 +627,7 @@ overrides: rules: internal-rules/require-to-string-tag: off node/no-unpublished-import: [error, { allowModules: ['chai', 'mocha'] }] + import/no-deprecated: off import/no-restricted-paths: off import/no-extraneous-dependencies: [error, { devDependencies: true }] - files: 'integrationTests/*' diff --git a/resources/gen-changelog.js b/resources/gen-changelog.js index 2439b8d77b..02bb634050 100644 --- a/resources/gen-changelog.js +++ b/resources/gen-changelog.js @@ -12,6 +12,9 @@ const labelsConfig = { 'PR: breaking change 💥': { section: 'Breaking Change 💥', }, + 'PR: deprecation ⚠': { + section: 'Deprecation ⚠', + }, 'PR: feature 🚀': { section: 'New Feature 🚀', }, diff --git a/src/error/GraphQLError.ts b/src/error/GraphQLError.ts index 0e0c5dcb74..448d74593d 100644 --- a/src/error/GraphQLError.ts +++ b/src/error/GraphQLError.ts @@ -198,7 +198,21 @@ export class GraphQLError extends Error { } toString(): string { - return printError(this); + let output = this.message; + + if (this.nodes) { + for (const node of this.nodes) { + if (node.loc) { + output += '\n\n' + printLocation(node.loc); + } + } + } else if (this.source && this.locations) { + for (const location of this.locations) { + output += '\n\n' + printSourceLocation(this.source, location); + } + } + + return output; } // FIXME: workaround to not break chai comparisons, should be remove in v16 @@ -210,21 +224,9 @@ export class GraphQLError extends Error { /** * Prints a GraphQLError to a string, representing useful location information * about the error's position in the source. + * + * @deprecated Please use `error.toString` instead. Will be removed in v17 */ export function printError(error: GraphQLError): string { - let output = error.message; - - if (error.nodes) { - for (const node of error.nodes) { - if (node.loc) { - output += '\n\n' + printLocation(node.loc); - } - } - } else if (error.source && error.locations) { - for (const location of error.locations) { - output += '\n\n' + printSourceLocation(error.source, location); - } - } - - return output; + return error.toString(); } diff --git a/src/error/__tests__/GraphQLError-test.ts b/src/error/__tests__/GraphQLError-test.ts index be349ddaba..d4cdf92a82 100644 --- a/src/error/__tests__/GraphQLError-test.ts +++ b/src/error/__tests__/GraphQLError-test.ts @@ -132,10 +132,15 @@ describe('GraphQLError', () => { }); }); -describe('printError', () => { +describe('toString', () => { + it('Deprecated: prints an error using printError', () => { + const error = new GraphQLError('Error'); + expect(printError(error)).to.equal('Error'); + }); + it('prints an error without location', () => { const error = new GraphQLError('Error without location'); - expect(printError(error)).to.equal('Error without location'); + expect(error.toString()).to.equal('Error without location'); }); it('prints an error using node without location', () => { @@ -143,7 +148,7 @@ describe('printError', () => { 'Error attached to node without location', parse('{ foo }', { noLocation: true }), ); - expect(printError(error)).to.equal( + expect(error.toString()).to.equal( 'Error attached to node without location', ); }); @@ -182,7 +187,7 @@ describe('printError', () => { fieldB.type, ]); - expect(printError(error)).to.equal(dedent` + expect(error.toString()).to.equal(dedent` Example error with two nodes SourceA:2:10