UNPKG

3.62 kBJavaScriptView Raw
1import { responsePathAsArray, locatedError } from 'graphql';
2import { AggregateError, getResponseKeyFromInfo, relocatedError } from '@graphql-tools/utils';
3import { resolveExternalValue } from './resolveExternalValue.js';
4export function checkResultAndHandleErrors(result, delegationContext) {
5 const { context, info, fieldName: responseKey = getResponseKey(info), subschema, returnType = getReturnType(info), skipTypeMerging, onLocatedError, } = delegationContext;
6 const { data, unpathedErrors } = mergeDataAndErrors(result.data == null ? undefined : result.data[responseKey], result.errors == null ? [] : result.errors, info != null && info.path ? responsePathAsArray(info.path) : undefined, onLocatedError);
7 return resolveExternalValue(data, unpathedErrors, subschema, context, info, returnType, skipTypeMerging);
8}
9export function mergeDataAndErrors(data, errors, path, onLocatedError, index = 1) {
10 var _a;
11 if (data == null) {
12 if (!errors.length) {
13 return { data: null, unpathedErrors: [] };
14 }
15 if (errors.length === 1) {
16 const error = onLocatedError ? onLocatedError(errors[0]) : errors[0];
17 const newPath = path === undefined ? error.path : !error.path ? path : path.concat(error.path.slice(1));
18 return { data: relocatedError(errors[0], newPath), unpathedErrors: [] };
19 }
20 // We cast path as any for GraphQL.js 14 compat
21 // locatedError path argument must be defined, but it is just forwarded to a constructor that allows a undefined value
22 // https://github.com/graphql/graphql-js/blob/b4bff0ba9c15c9d7245dd68556e754c41f263289/src/error/locatedError.js#L25
23 // https://github.com/graphql/graphql-js/blob/b4bff0ba9c15c9d7245dd68556e754c41f263289/src/error/GraphQLError.js#L19
24 const combinedError = new AggregateError(errors, errors.map(error => error.message).join(', \n'));
25 const newError = locatedError(combinedError, undefined, path);
26 return { data: newError, unpathedErrors: [] };
27 }
28 if (!errors.length) {
29 return { data, unpathedErrors: [] };
30 }
31 const unpathedErrors = [];
32 const errorMap = new Map();
33 for (const error of errors) {
34 const pathSegment = (_a = error.path) === null || _a === void 0 ? void 0 : _a[index];
35 if (pathSegment != null) {
36 let pathSegmentErrors = errorMap.get(pathSegment);
37 if (pathSegmentErrors === undefined) {
38 pathSegmentErrors = [error];
39 errorMap.set(pathSegment, pathSegmentErrors);
40 }
41 else {
42 pathSegmentErrors.push(error);
43 }
44 }
45 else {
46 unpathedErrors.push(error);
47 }
48 }
49 for (const [pathSegment, pathSegmentErrors] of errorMap) {
50 if (data[pathSegment] !== undefined) {
51 const { data: newData, unpathedErrors: newErrors } = mergeDataAndErrors(data[pathSegment], pathSegmentErrors, path, onLocatedError, index + 1);
52 data[pathSegment] = newData;
53 unpathedErrors.push(...newErrors);
54 }
55 else {
56 unpathedErrors.push(...pathSegmentErrors);
57 }
58 }
59 return { data, unpathedErrors };
60}
61function getResponseKey(info) {
62 if (info == null) {
63 throw new Error(`Data cannot be extracted from result without an explicit key or source schema.`);
64 }
65 return getResponseKeyFromInfo(info);
66}
67function getReturnType(info) {
68 if (info == null) {
69 throw new Error(`Return type cannot be inferred without a source schema.`);
70 }
71 return info.returnType;
72}