-
-
Notifications
You must be signed in to change notification settings - Fork 816
/
defaultMergedResolver.ts
41 lines (37 loc) · 1.13 KB
/
defaultMergedResolver.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
import { GraphQLFieldResolver, responsePathAsArray } from 'graphql';
import { locatedError } from 'graphql/error';
import { getErrorsFromParent, annotateWithChildrenErrors } from './errors';
// Resolver that knows how to:
// a) handle aliases for proxied schemas
// b) handle errors from proxied schemas
const defaultMergedResolver: GraphQLFieldResolver<any, any> = (
parent,
args,
context,
info,
) => {
const responseKey = info.fieldNodes[0].alias
? info.fieldNodes[0].alias.value
: info.fieldName;
const errorResult = getErrorsFromParent(parent, responseKey);
if (errorResult.kind === 'OWN') {
throw locatedError(
new Error(errorResult.error.message),
info.fieldNodes,
responsePathAsArray(info.path),
);
} else if (parent) {
let result = parent[responseKey];
// subscription result mapping
if (!result && parent.data && parent.data[responseKey]) {
result = parent.data[responseKey];
}
if (errorResult.errors) {
result = annotateWithChildrenErrors(result, errorResult.errors);
}
return result;
} else {
return null;
}
};
export default defaultMergedResolver;