/
checkResultAndHandleErrors.ts
82 lines (75 loc) · 1.9 KB
/
checkResultAndHandleErrors.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
import {
GraphQLResolveInfo,
responsePathAsArray,
getNullableType,
isCompositeType,
isLeafType,
isListType,
ExecutionResult,
GraphQLError,
GraphQLOutputType,
GraphQLSchema,
} from 'graphql';
import { SubschemaConfig, IGraphQLToolsResolveInfo } from '../Interfaces';
import { getResponseKeyFromInfo } from '../stitch/getResponseKeyFromInfo';
import { handleNull } from './results/handleNull';
import { handleObject } from './results/handleObject';
import { handleList } from './results/handleList';
export function checkResultAndHandleErrors(
result: ExecutionResult,
context: Record<string, any>,
info: GraphQLResolveInfo,
responseKey: string = getResponseKeyFromInfo(info),
subschema?: GraphQLSchema | SubschemaConfig,
returnType: GraphQLOutputType = info.returnType,
skipTypeMerging?: boolean,
): any {
const errors = result.errors != null ? result.errors : [];
const data = result.data != null ? result.data[responseKey] : undefined;
return handleResult(
data,
errors,
subschema,
context,
info,
returnType,
skipTypeMerging,
);
}
export function handleResult(
result: any,
errors: ReadonlyArray<GraphQLError>,
subschema: GraphQLSchema | SubschemaConfig,
context: Record<string, any>,
info: IGraphQLToolsResolveInfo,
returnType = info.returnType,
skipTypeMerging?: boolean,
): any {
const type = getNullableType(returnType);
if (result == null) {
return handleNull(info.fieldNodes, responsePathAsArray(info.path), errors);
}
if (isLeafType(type)) {
return type.parseValue(result);
} else if (isCompositeType(type)) {
return handleObject(
type,
result,
errors,
subschema,
context,
info,
skipTypeMerging,
);
} else if (isListType(type)) {
return handleList(
type,
result,
errors,
subschema,
context,
info,
skipTypeMerging,
);
}
}