-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
CheckResultAndHandleErrors.ts
58 lines (52 loc) · 1.94 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
import { GraphQLResolveInfo, ExecutionResult, GraphQLOutputType, GraphQLSchema } from 'graphql';
import { Transform, getResponseKeyFromInfo, RelativeGraphQLError, toRelativeError } from '@graphql-tools/utils';
import { handleResult } from '../results/handleResult';
import { SubschemaConfig } 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(result: any): any {
return checkResultAndHandleErrors(
result,
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: Array<RelativeGraphQLError> =
result.errors != null ? result.errors.map(error => toRelativeError(error, info)) : [];
const data = result.data != null ? result.data[responseKey] : undefined;
return handleResult(data, errors, subschema, context, info, returnType, skipTypeMerging);
}