Skip to content

Commit e697829

Browse files
committed
refactor: ValidationError should not be a GraphQLError object. It may be rewrapped in addErrorCatcherField() if client does not request error field in payload. We are using GraphQLError only for providing extensions property on top-level.
1 parent ad6ed68 commit e697829

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/errors/ValidationError.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import { SchemaComposer, ObjectTypeComposer } from 'graphql-compose';
2-
import { GraphQLError } from 'graphql';
32
import type { ValidationErrorData, ValidationsWithMessage } from '../resolvers/helpers/validate';
43

5-
export class ValidationError extends GraphQLError {
4+
export class ValidationError extends Error {
65
public errors: ValidationErrorData[];
76

87
constructor(validation: ValidationsWithMessage) {
9-
super(validation.message, undefined, undefined, undefined, undefined, undefined, {
10-
validations: validation.errors,
11-
});
12-
8+
super(validation.message);
139
this.errors = validation.errors;
14-
1510
(this as any).__proto__ = ValidationError.prototype;
1611
}
1712
}
1813

19-
export function getValidationOTC(schemaComposer: SchemaComposer<any>): ObjectTypeComposer {
20-
return schemaComposer.getOrCreateOTC('Validation', (otc) => {
14+
export function getValidatorErrorOTC(schemaComposer: SchemaComposer<any>): ObjectTypeComposer {
15+
return schemaComposer.getOrCreateOTC('ValidatorError', (otc) => {
2116
otc.addFields({
2217
message: {
2318
description: 'Validation error message',
@@ -31,21 +26,26 @@ export function getValidationOTC(schemaComposer: SchemaComposer<any>): ObjectTyp
3126
description: 'Field value which occurs the validation error',
3227
type: 'JSON',
3328
},
29+
idx: {
30+
description:
31+
'Input record idx in array which occurs the validation error. This `idx` is useful for createMany operation. For singular operations it always be 0. For *Many operations `idx` represents record index in array received from user.',
32+
type: 'Int!',
33+
resolve: (s: any) => s.idx || 0,
34+
},
3435
});
3536
});
3637
}
3738

3839
export function getValidationErrorOTC(schemaComposer: SchemaComposer<any>): ObjectTypeComposer {
3940
return schemaComposer.getOrCreateOTC('ValidationError', (otc) => {
40-
const Validation = getValidationOTC(schemaComposer);
4141
otc.addFields({
4242
message: {
4343
description: 'Combined error message from all validators',
4444
type: 'String',
4545
},
4646
errors: {
4747
description: 'List of validator errors',
48-
type: Validation.NonNull.List,
48+
type: getValidatorErrorOTC(schemaComposer).NonNull.List,
4949
},
5050
});
5151
});

0 commit comments

Comments
 (0)