-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
errors.ts
84 lines (68 loc) · 2.46 KB
/
errors.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { GraphQLError, responsePathAsArray, GraphQLResolveInfo } from 'graphql';
export const ERROR_SYMBOL = Symbol('subschemaErrors');
export interface RelativeGraphQLError {
relativePath: Array<string | number>;
graphQLError: GraphQLError;
}
export function relocatedError(originalError: GraphQLError, path?: ReadonlyArray<string | number>): GraphQLError {
return new GraphQLError(
originalError.message,
originalError.nodes,
originalError.source,
originalError.positions,
path === null ? undefined : path === undefined ? originalError.path : path,
originalError.originalError,
originalError.extensions
);
}
export function slicedError(originalError: GraphQLError) {
return relocatedError(originalError, originalError.path != null ? originalError.path.slice(1) : undefined);
}
export function getErrorsByPathSegment(
errors: Array<RelativeGraphQLError>
): Record<string, Array<RelativeGraphQLError>> {
const record: Record<string, Array<RelativeGraphQLError>> = Object.create(null);
errors.forEach(error => {
if (!error.relativePath || error.relativePath.length < 2) {
return;
}
const pathSegment = error.relativePath[1];
const current: Array<RelativeGraphQLError> = pathSegment in record ? record[pathSegment] : [];
current.push({
relativePath: error.relativePath.slice(1),
graphQLError: error.graphQLError,
});
record[pathSegment] = current;
});
return record;
}
export function toRelativeError(error: Readonly<GraphQLError>, info: GraphQLResolveInfo): RelativeGraphQLError {
const relativePath = error.path?.slice() || [];
const sourcePath = info != null ? responsePathAsArray(info.path) : [];
return {
relativePath,
graphQLError: relocatedError(error, sourcePath.concat(relativePath.slice(1))),
};
}
export function sliceRelativeError(error: RelativeGraphQLError): RelativeGraphQLError {
return {
...error,
relativePath: error.relativePath.slice(1),
};
}
export function setErrors(result: any, errors: Array<RelativeGraphQLError>) {
result[ERROR_SYMBOL] = errors;
}
export function getErrors(result: any, pathSegment: string): Array<RelativeGraphQLError> {
const errors = result != null ? result[ERROR_SYMBOL] : result;
if (!Array.isArray(errors)) {
return null;
}
const fieldErrors = [];
for (const error of errors) {
if (!error.relativePath || error.relativePath[0] === pathSegment) {
fieldErrors.push(error);
}
}
return fieldErrors;
}