This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
/
errors.ts
162 lines (142 loc) · 4 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
GraphQLResolveInfo,
responsePathAsArray,
getNullableType,
isObjectType,
isListType,
ExecutionResult,
GraphQLError,
ASTNode
} from 'graphql';
import { getResponseKeyFromInfo } from './getResponseKeyFromInfo';
export let ERROR_SYMBOL: any;
if (
(typeof global !== 'undefined' && 'Symbol' in global) ||
(typeof window !== 'undefined' && 'Symbol' in window)
) {
ERROR_SYMBOL = Symbol('subSchemaErrors');
} else {
ERROR_SYMBOL = '@@__subSchemaErrors';
}
export function relocatedError(
originalError: Error | GraphQLError,
nodes: ReadonlyArray<ASTNode>,
path: ReadonlyArray<string | number>
): GraphQLError {
if (Array.isArray((originalError as GraphQLError).path)) {
return new GraphQLError(
(originalError as GraphQLError).message,
(originalError as GraphQLError).nodes,
(originalError as GraphQLError).source,
(originalError as GraphQLError).positions,
path ? path : (originalError as GraphQLError).path,
(originalError as GraphQLError).originalError,
(originalError as GraphQLError).extensions
);
}
return new GraphQLError(
originalError && originalError.message,
(originalError && (originalError as any).nodes) || nodes,
originalError && (originalError as any).source,
originalError && (originalError as any).positions,
path,
originalError,
);
}
export function annotateWithChildrenErrors(object: any, childrenErrors: ReadonlyArray<GraphQLError>): any {
if (!Array.isArray(childrenErrors)) {
object[ERROR_SYMBOL] = [];
return object;
}
if (Array.isArray(object)) {
const byIndex = {};
childrenErrors.forEach(error => {
if (!error.path) {
return;
}
const index = error.path[1];
const current = byIndex[index] || [];
current.push(
relocatedError(
error,
error.nodes,
error.path ? error.path.slice(1) : undefined
)
);
byIndex[index] = current;
});
return object.map((item, index) => annotateWithChildrenErrors(item, byIndex[index]));
}
object[ERROR_SYMBOL] = childrenErrors.map(error => {
const newError = relocatedError(
error,
error.nodes,
error.path ? error.path.slice(1) : undefined
);
return newError;
});
return object;
}
export function getErrorsFromParent(
object: any,
fieldName: string
): Array<GraphQLError> {
const errors = object && object[ERROR_SYMBOL];
if (!Array.isArray(errors)) {
return null;
}
const childrenErrors = [];
for (const error of errors) {
if (!error.path || error.path[0] === fieldName) {
childrenErrors.push(error);
}
}
return childrenErrors;
}
class CombinedError extends Error {
public errors: ReadonlyArray<GraphQLError>;
constructor(message: string, errors: ReadonlyArray<GraphQLError>) {
super(message);
this.errors = errors;
}
}
export function checkResultAndHandleErrors(
result: ExecutionResult,
info: GraphQLResolveInfo,
responseKey?: string
): any {
if (!responseKey) {
responseKey = getResponseKeyFromInfo(info);
}
if (!result.data || !result.data[responseKey]) {
if (result.errors) {
throw relocatedError(
combineErrors(result.errors),
info.fieldNodes,
responsePathAsArray(info.path)
);
}
}
result.errors = result.errors || [];
let resultObject = result.data[responseKey];
const nullableType = getNullableType(info.returnType);
if (isObjectType(nullableType) || isListType(nullableType)) {
annotateWithChildrenErrors(resultObject, result.errors);
}
return resultObject;
}
export function combineErrors(errors: ReadonlyArray<GraphQLError>): GraphQLError | CombinedError {
if (errors.length === 1) {
return new GraphQLError(
errors[0].message,
errors[0].nodes,
errors[0].source,
errors[0].positions,
errors[0].path,
errors[0].originalError,
errors[0].extensions
);
} else {
return new CombinedError(errors.map(error => error.message).join('\n'), errors);
}
}