/
CheckResultAndHandleErrors.ts
61 lines (55 loc) · 1.98 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
import { GraphQLResolveInfo, GraphQLOutputType, GraphQLSchema } from 'graphql';
import { getResponseKeyFromInfo, ExecutionResult } from '@graphql-tools/utils';
import { handleResult } from '../results/handleResult';
import { SubschemaConfig, Transform, DelegationContext } from '../types';
export default class CheckResultAndHandleErrors implements Transform {
private readonly context?: Record<string, any>;
private readonly info: GraphQLResolveInfo;
private readonly fieldName?: string;
private readonly subschema?: GraphQLSchema | SubschemaConfig;
private readonly returnType?: GraphQLOutputType;
private readonly typeMerge?: boolean;
constructor(
info: GraphQLResolveInfo,
fieldName?: string,
subschema?: GraphQLSchema | SubschemaConfig,
context?: Record<string, any>,
returnType: GraphQLOutputType = info.returnType,
typeMerge?: boolean
) {
this.context = context;
this.info = info;
this.fieldName = fieldName;
this.subschema = subschema;
this.returnType = returnType;
this.typeMerge = typeMerge;
}
public transformResult(
originalResult: ExecutionResult,
_delegationContext: DelegationContext,
_transformationContext: Record<string, any>
): ExecutionResult {
return checkResultAndHandleErrors(
originalResult,
this.context != null ? this.context : {},
this.info,
this.fieldName,
this.subschema,
this.returnType,
this.typeMerge
);
}
}
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);
}