/
delegationBindings.ts
49 lines (41 loc) · 1.9 KB
/
delegationBindings.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
import { Maybe } from '@graphql-tools/utils';
import { Transform, StitchingInfo, DelegationContext } from './types';
import AddSelectionSets from './transforms/AddSelectionSets';
import ExpandAbstractTypes from './transforms/ExpandAbstractTypes';
import WrapConcreteTypes from './transforms/WrapConcreteTypes';
import FilterToSchema from './transforms/FilterToSchema';
import AddTypenameToAbstract from './transforms/AddTypenameToAbstract';
import CheckResultAndHandleErrors from './transforms/CheckResultAndHandleErrors';
import AddArgumentsAsVariables from './transforms/AddArgumentsAsVariables';
export function defaultDelegationBinding<TContext>(
delegationContext: DelegationContext<TContext>
): Array<Transform<any, TContext>> {
let delegationTransforms: Array<Transform<any, TContext>> = [new CheckResultAndHandleErrors()];
const info = delegationContext.info;
const stitchingInfo: Maybe<StitchingInfo> = info?.schema.extensions?.['stitchingInfo'];
if (stitchingInfo != null) {
delegationTransforms = delegationTransforms.concat([
new ExpandAbstractTypes(),
new AddSelectionSets(
stitchingInfo.fieldNodesByType,
stitchingInfo.fieldNodesByField,
stitchingInfo.dynamicSelectionSetsByField
),
new WrapConcreteTypes(),
]);
} else if (info != null) {
delegationTransforms = delegationTransforms.concat([new WrapConcreteTypes(), new ExpandAbstractTypes()]);
} else {
delegationTransforms.push(new WrapConcreteTypes());
}
const transforms = delegationContext.transforms;
if (transforms != null) {
delegationTransforms = delegationTransforms.concat(transforms.slice().reverse());
}
const args = delegationContext.args;
if (args != null) {
delegationTransforms.push(new AddArgumentsAsVariables(args));
}
delegationTransforms = delegationTransforms.concat([new FilterToSchema(), new AddTypenameToAbstract()]);
return delegationTransforms;
}