Skip to content

Commit

Permalink
Use aggregate-error to keep the content of errors (#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jun 15, 2020
1 parent b688dbc commit 17b8ef4
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 37 deletions.
3 changes: 2 additions & 1 deletion packages/delegate/package.json
Expand Up @@ -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"
}
}
}
13 changes: 5 additions & 8 deletions packages/delegate/src/delegateToSchema.ts
Expand Up @@ -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(
Expand Down Expand Up @@ -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];
Expand Down
5 changes: 3 additions & 2 deletions 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<GraphQLError>) {
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];
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/package.json
Expand Up @@ -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"
}
}
}
16 changes: 0 additions & 16 deletions packages/utils/src/errors.ts
Expand Up @@ -35,22 +35,6 @@ export function getErrorsByPathSegment(errors: ReadonlyArray<GraphQLError>): Rec
return record;
}

export class CombinedError extends GraphQLError {
public errors: ReadonlyArray<Error>;
constructor(errors: ReadonlyArray<Error>) {
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<GraphQLError>) {
result[ERROR_SYMBOL] = errors;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/validate-documents.ts
Expand Up @@ -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();
Expand Down Expand Up @@ -90,7 +90,7 @@ export function checkValidationErrors(loadDocumentErrors: ReadonlyArray<LoadDocu
}
}

throw new CombinedError(errors);
throw new AggregateError(errors);
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/utils/tests/validate-documents.spec.ts
@@ -1,5 +1,6 @@
import { checkValidationErrors, validateGraphQlDocuments, CombinedError } from '../src';
import { checkValidationErrors, validateGraphQlDocuments } from '../src';
import { buildSchema, parse, GraphQLError } from 'graphql';
import AggregateError from 'aggregate-error';

describe('validateGraphQlDocuments', () => {
it('Should throw an informative error when validation errors happens, also check for fragments validation even why they are duplicated', async () => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -116,7 +117,7 @@ describe('checkValidationErrors', () => {
errors = _errors;
}

expect(errors).toBeInstanceOf(CombinedError);
expect(errors).toBeInstanceOf(AggregateError);

let error;
const generator = errors[Symbol.iterator]();
Expand Down
3 changes: 2 additions & 1 deletion packages/wrap/package.json
Expand Up @@ -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"
}
}
}
5 changes: 3 additions & 2 deletions packages/wrap/src/introspect.ts
Expand Up @@ -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<IntrospectionQuery>): 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];
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -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==
Expand Down

0 comments on commit 17b8ef4

Please sign in to comment.