-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
handleResult.ts
56 lines (50 loc) · 1.52 KB
/
handleResult.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
import {
GraphQLResolveInfo,
getNullableType,
isCompositeType,
isLeafType,
isListType,
GraphQLError,
GraphQLSchema,
responsePathAsArray,
} from 'graphql';
import { SubschemaConfig } from '../types';
import { handleNull } from './handleNull';
import { handleObject } from './handleObject';
import { handleList } from './handleList';
import { extendedError } from '@graphql-tools/utils';
export function handleResult(
result: any,
errors: ReadonlyArray<GraphQLError>,
subschema: GraphQLSchema | SubschemaConfig,
context: Record<string, any>,
info: GraphQLResolveInfo,
returnType = info.returnType,
skipTypeMerging?: boolean
): any {
const annotatedErrors = errors.map(error => {
if (error.extensions?.graphQLToolsMergedPath == null) {
return extendedError(error, {
...error.extensions,
graphQLToolsMergedPath:
info == null
? error.path
: error.path != null
? [...responsePathAsArray(info.path), ...error.path.slice(1)]
: responsePathAsArray(info.path),
});
}
return error;
});
const type = getNullableType(returnType);
if (result == null) {
return handleNull(annotatedErrors);
}
if (isLeafType(type)) {
return type.parseValue(result);
} else if (isCompositeType(type)) {
return handleObject(type, result, annotatedErrors, subschema, context, info, skipTypeMerging);
} else if (isListType(type)) {
return handleList(type, result, annotatedErrors, subschema, context, info, skipTypeMerging);
}
}