-
-
Notifications
You must be signed in to change notification settings - Fork 821
/
Copy pathhandleNull.ts
36 lines (28 loc) · 1.07 KB
/
handleNull.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { GraphQLError } from 'graphql';
import { getErrorsByPathSegment, CombinedError, 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);
return combinedError;
}
const error = errors[0];
return error.originalError || relocatedError(error, null);
} else if (errors.some(error => typeof error.path[1] === 'string')) {
const childErrors = getErrorsByPathSegment(errors);
const result = {};
Object.keys(childErrors).forEach(pathSegment => {
result[pathSegment] = handleNull(childErrors[pathSegment]);
});
return result;
}
const childErrors = getErrorsByPathSegment(errors);
const result: Array<any> = [];
Object.keys(childErrors).forEach(pathSegment => {
result.push(handleNull(childErrors[pathSegment]));
});
return result;
}
return null;
}