forked from ardatan/graphql-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultMergedResolver.ts
39 lines (31 loc) · 1.3 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
import { GraphQLFieldResolver, GraphQLNonNull, responsePathAsArray } from 'graphql';
import { locatedError } from 'graphql/error';
import { getErrorsFromParent, annotateWithChildrenErrors } from './errors';
import { getResponseKeyFromInfo } from './getResponseKeyFromInfo';
// 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) => {
if (!parent) {
return null;
}
const responseKey = getResponseKeyFromInfo(info);
const errorResult = getErrorsFromParent(parent, responseKey);
if (errorResult.kind === 'OWN') {
throw locatedError(new Error(errorResult.error.message), info.fieldNodes, responsePathAsArray(info.path));
}
let result = parent[responseKey];
// Only replace the aliased result with the parent result if the field is non-nullable
if (result == null && info.returnType instanceof GraphQLNonNull) {
result = parent[info.fieldName];
}
// subscription result mapping
if (!result && parent.data && parent.data[responseKey]) {
result = parent.data[responseKey];
}
if (errorResult.errors) {
result = annotateWithChildrenErrors(result, errorResult.errors);
}
return result;
};
export default defaultMergedResolver;