/
VisitSelectionSets.ts
143 lines (125 loc) · 3.64 KB
/
VisitSelectionSets.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {
DocumentNode,
GraphQLSchema,
Kind,
SelectionSetNode,
TypeInfo,
visit,
visitWithTypeInfo,
GraphQLOutputType,
OperationDefinitionNode,
FragmentDefinitionNode,
SelectionNode,
DefinitionNode,
} from 'graphql';
import { Request, collectFields, GraphQLExecutionContext } from '@graphql-tools/utils';
import { Transform, DelegationContext } from '../types';
export default class VisitSelectionSets implements Transform {
private readonly schema: GraphQLSchema;
private readonly initialType: GraphQLOutputType;
private readonly visitor: (node: SelectionSetNode, typeInfo: TypeInfo) => SelectionSetNode;
constructor(
schema: GraphQLSchema,
initialType: GraphQLOutputType,
visitor: (node: SelectionSetNode, typeInfo: TypeInfo) => SelectionSetNode
) {
this.schema = schema;
this.initialType = initialType;
this.visitor = visitor;
}
public transformRequest(
originalRequest: Request,
_delegationContext: DelegationContext,
_transformationContext: Record<string, any>
): Request {
const document = visitSelectionSets(originalRequest, this.schema, this.initialType, this.visitor);
return {
...originalRequest,
document,
};
}
}
function visitSelectionSets(
request: Request,
schema: GraphQLSchema,
initialType: GraphQLOutputType,
visitor: (node: SelectionSetNode, typeInfo: TypeInfo) => SelectionSetNode
): DocumentNode {
const { document, variables } = request;
const operations: Array<OperationDefinitionNode> = [];
const fragments: Record<string, FragmentDefinitionNode> = Object.create(null);
document.definitions.forEach(def => {
if (def.kind === Kind.OPERATION_DEFINITION) {
operations.push(def);
} else if (def.kind === Kind.FRAGMENT_DEFINITION) {
fragments[def.name.value] = def;
}
});
const partialExecutionContext = {
schema,
variableValues: variables,
fragments,
} as GraphQLExecutionContext;
const typeInfo = new TypeInfo(schema, undefined, initialType);
const newDefinitions: Array<DefinitionNode> = operations.map(operation => {
const type =
operation.operation === 'query'
? schema.getQueryType()
: operation.operation === 'mutation'
? schema.getMutationType()
: schema.getSubscriptionType();
const fields = collectFields(
partialExecutionContext,
type,
operation.selectionSet,
Object.create(null),
Object.create(null)
);
const newSelections: Array<SelectionNode> = [];
Object.keys(fields).forEach(responseKey => {
const fieldNodes = fields[responseKey];
fieldNodes.forEach(fieldNode => {
const selectionSet = fieldNode.selectionSet;
if (selectionSet == null) {
newSelections.push(fieldNode);
return;
}
const newSelectionSet = visit(
selectionSet,
visitWithTypeInfo(typeInfo, {
[Kind.SELECTION_SET]: node => visitor(node, typeInfo),
})
);
if (newSelectionSet === selectionSet) {
newSelections.push(fieldNode);
return;
}
newSelections.push({
...fieldNode,
selectionSet: newSelectionSet,
});
});
});
return {
...operation,
selectionSet: {
kind: Kind.SELECTION_SET,
selections: newSelections,
},
};
});
Object.values(fragments).forEach(fragment => {
newDefinitions.push(
visit(
fragment,
visitWithTypeInfo(typeInfo, {
[Kind.SELECTION_SET]: node => visitor(node, typeInfo),
})
)
);
});
return {
...document,
definitions: newDefinitions,
};
}