From 17b8ef487c7e5187ad5b13a23b9fde1889e85ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20Tanr=C4=B1kulu?= <20847995+ardatan@users.noreply.github.com> Date: Mon, 15 Jun 2020 15:53:14 +0300 Subject: [PATCH] Use aggregate-error to keep the content of errors (#1646) --- packages/delegate/package.json | 3 ++- packages/delegate/src/delegateToSchema.ts | 13 +++++-------- packages/delegate/src/results/handleNull.ts | 5 +++-- packages/utils/package.json | 3 ++- packages/utils/src/errors.ts | 16 ---------------- packages/utils/src/validate-documents.ts | 4 ++-- packages/utils/tests/validate-documents.spec.ts | 7 ++++--- packages/wrap/package.json | 3 ++- packages/wrap/src/introspect.ts | 5 +++-- yarn.lock | 2 +- 10 files changed, 24 insertions(+), 37 deletions(-) diff --git a/packages/delegate/package.json b/packages/delegate/package.json index d9cf4cb5454..74a1c1e9471 100644 --- a/packages/delegate/package.json +++ b/packages/delegate/package.json @@ -20,10 +20,11 @@ "dependencies": { "@graphql-tools/schema": "6.0.9", "@graphql-tools/utils": "6.0.9", + "aggregate-error": "3.0.1", "tslib": "~2.0.0" }, "publishConfig": { "access": "public", "directory": "dist" } -} \ No newline at end of file +} diff --git a/packages/delegate/src/delegateToSchema.ts b/packages/delegate/src/delegateToSchema.ts index 01a705cfd79..ce1dd717dfe 100644 --- a/packages/delegate/src/delegateToSchema.ts +++ b/packages/delegate/src/delegateToSchema.ts @@ -14,19 +14,16 @@ import { GraphQLObjectType, } from 'graphql'; -import { mapAsyncIterator, CombinedError, Transform } from '@graphql-tools/utils'; +import { mapAsyncIterator, Transform } from '@graphql-tools/utils'; -import { - IDelegateToSchemaOptions, - IDelegateRequestOptions, - SubschemaConfig, - ExecutionParams, -} from './types'; +import { IDelegateToSchemaOptions, IDelegateRequestOptions, SubschemaConfig, ExecutionParams } from './types'; import { isSubschemaConfig } from './Subschema'; import { createRequestFromInfo, getDelegatingOperation } from './createRequest'; import { Transformer } from './Transformer'; +import AggregateError from 'aggregate-error'; + export function delegateToSchema(options: IDelegateToSchemaOptions | GraphQLSchema): any { if (isSchema(options)) { throw new Error( @@ -197,7 +194,7 @@ function validateRequest(targetSchema: GraphQLSchema, document: DocumentNode) { const errors = validate(targetSchema, document); if (errors.length > 0) { if (errors.length > 1) { - const combinedError = new CombinedError(errors); + const combinedError = new AggregateError(errors); throw combinedError; } const error = errors[0]; diff --git a/packages/delegate/src/results/handleNull.ts b/packages/delegate/src/results/handleNull.ts index 791682fc674..b567ef7697a 100644 --- a/packages/delegate/src/results/handleNull.ts +++ b/packages/delegate/src/results/handleNull.ts @@ -1,12 +1,13 @@ import { GraphQLError } from 'graphql'; -import { getErrorsByPathSegment, CombinedError, relocatedError } from '@graphql-tools/utils'; +import AggregateError from 'aggregate-error'; +import { getErrorsByPathSegment, relocatedError } from '@graphql-tools/utils'; export function handleNull(errors: ReadonlyArray) { if (errors.length) { if (errors.some(error => !error.path || error.path.length < 2)) { if (errors.length > 1) { - const combinedError = new CombinedError(errors); + const combinedError = new AggregateError(errors); return combinedError; } const error = errors[0]; diff --git a/packages/utils/package.json b/packages/utils/package.json index aee2aca7b48..00a4da32bf9 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -21,10 +21,11 @@ "graphql-scalars": "1.1.5" }, "dependencies": { + "aggregate-error": "3.0.1", "camel-case": "4.1.1" }, "publishConfig": { "access": "public", "directory": "dist" } -} \ No newline at end of file +} diff --git a/packages/utils/src/errors.ts b/packages/utils/src/errors.ts index b3562d94b40..062993c8b53 100644 --- a/packages/utils/src/errors.ts +++ b/packages/utils/src/errors.ts @@ -35,22 +35,6 @@ export function getErrorsByPathSegment(errors: ReadonlyArray): Rec return record; } -export class CombinedError extends GraphQLError { - public errors: ReadonlyArray; - constructor(errors: ReadonlyArray) { - const message = errors.map(error => error.message).join('\n'); - super(message, undefined, undefined, undefined, undefined, undefined, undefined); - const actualErrors = errors.map((error: GraphQLError) => - error.originalError != null ? error.originalError : error - ); - this.errors = actualErrors; - } - - [Symbol.iterator]() { - return this.errors[Symbol.iterator](); - } -} - export function setErrors(result: any, errors: Array) { result[ERROR_SYMBOL] = errors; } diff --git a/packages/utils/src/validate-documents.ts b/packages/utils/src/validate-documents.ts index b4e5b55a80e..4730cbd7553 100644 --- a/packages/utils/src/validate-documents.ts +++ b/packages/utils/src/validate-documents.ts @@ -9,7 +9,7 @@ import { ASTVisitor, } from 'graphql'; import { Source } from './loaders'; -import { CombinedError } from './errors'; +import AggregateError from 'aggregate-error'; export type ValidationRule = (context: ValidationContext) => ASTVisitor; const DEFAULT_EFFECTIVE_RULES = createDefaultRules(); @@ -90,7 +90,7 @@ export function checkValidationErrors(loadDocumentErrors: ReadonlyArray { it('Should throw an informative error when validation errors happens, also check for fragments validation even why they are duplicated', async () => { @@ -54,7 +55,7 @@ describe('validateGraphQlDocuments', () => { checkValidationErrors(result); expect(true).toBeFalsy(); } catch (errors) { - expect(errors).toBeInstanceOf(CombinedError); + expect(errors).toBeInstanceOf(AggregateError); const generator = errors[Symbol.iterator](); const error = generator.next().value; @@ -116,7 +117,7 @@ describe('checkValidationErrors', () => { errors = _errors; } - expect(errors).toBeInstanceOf(CombinedError); + expect(errors).toBeInstanceOf(AggregateError); let error; const generator = errors[Symbol.iterator](); diff --git a/packages/wrap/package.json b/packages/wrap/package.json index ffeffe8b6e9..c955cb63ead 100644 --- a/packages/wrap/package.json +++ b/packages/wrap/package.json @@ -21,10 +21,11 @@ "@graphql-tools/delegate": "6.0.9", "@graphql-tools/schema": "6.0.9", "@graphql-tools/utils": "6.0.9", + "aggregate-error": "3.0.1", "tslib": "~2.0.0" }, "publishConfig": { "access": "public", "directory": "dist" } -} \ No newline at end of file +} diff --git a/packages/wrap/src/introspect.ts b/packages/wrap/src/introspect.ts index 82a1c0de686..3242483167b 100644 --- a/packages/wrap/src/introspect.ts +++ b/packages/wrap/src/introspect.ts @@ -7,15 +7,16 @@ import { IntrospectionQuery, } from 'graphql'; -import { ExecutionResult, CombinedError } from '@graphql-tools/utils'; +import { ExecutionResult } from '@graphql-tools/utils'; import { AsyncExecutor, SyncExecutor } from '@graphql-tools/delegate'; +import AggregateError from 'aggregate-error'; function getSchemaFromIntrospection(introspectionResult: ExecutionResult): GraphQLSchema { if (introspectionResult?.data?.__schema) { return buildClientSchema(introspectionResult.data); } else if (introspectionResult?.errors?.length) { if (introspectionResult.errors.length > 1) { - const combinedError = new CombinedError(introspectionResult.errors); + const combinedError = new AggregateError(introspectionResult.errors); throw combinedError; } const error = introspectionResult.errors[0]; diff --git a/yarn.lock b/yarn.lock index 49a925f70cf..fd56994e30f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2621,7 +2621,7 @@ agentkeepalive@^2.2.0: resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-2.2.0.tgz#c5d1bd4b129008f1163f236f86e5faea2026e2ef" integrity sha1-xdG9SxKQCPEWPyNvhuX66iAm4u8= -aggregate-error@^3.0.0: +aggregate-error@3.0.1, aggregate-error@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/aggregate-error/-/aggregate-error-3.0.1.tgz#db2fe7246e536f40d9b5442a39e117d7dd6a24e0" integrity sha512-quoaXsZ9/BLNae5yiNoUz+Nhkwz83GhWwtYFglcjEQB2NDHCIpApbqXxIFnm4Pq/Nvhrsq5sYJFyohrrxnTGAA==