1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.mergeRequests = void 0;
|
5 | const graphql_1 = require("graphql");
|
6 | const utils_1 = require("@graphql-tools/utils");
|
7 | const prefix_js_1 = require("./prefix.js");
|
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 | function mergeRequests(requests, extensionsReducer) {
|
43 | var _a, _b, _c, _d, _e;
|
44 | const mergedVariables = Object.create(null);
|
45 | const mergedVariableDefinitions = [];
|
46 | const mergedSelections = [];
|
47 | const mergedFragmentDefinitions = [];
|
48 | let mergedExtensions = Object.create(null);
|
49 | for (const index in requests) {
|
50 | const request = requests[index];
|
51 | const prefixedRequests = prefixRequest((0, prefix_js_1.createPrefix)(index), request);
|
52 | for (const def of prefixedRequests.document.definitions) {
|
53 | if (isOperationDefinition(def)) {
|
54 | mergedSelections.push(...def.selectionSet.selections);
|
55 | if (def.variableDefinitions) {
|
56 | mergedVariableDefinitions.push(...def.variableDefinitions);
|
57 | }
|
58 | }
|
59 | if (isFragmentDefinition(def)) {
|
60 | mergedFragmentDefinitions.push(def);
|
61 | }
|
62 | }
|
63 | Object.assign(mergedVariables, prefixedRequests.variables);
|
64 | mergedExtensions = extensionsReducer(mergedExtensions, request);
|
65 | }
|
66 | const firstRequest = requests[0];
|
67 | const operationType = (_a = firstRequest.operationType) !== null && _a !== void 0 ? _a : (0, utils_1.getOperationASTFromRequest)(firstRequest).operation;
|
68 | const mergedOperationDefinition = {
|
69 | kind: graphql_1.Kind.OPERATION_DEFINITION,
|
70 | operation: operationType,
|
71 | variableDefinitions: mergedVariableDefinitions,
|
72 | selectionSet: {
|
73 | kind: graphql_1.Kind.SELECTION_SET,
|
74 | selections: mergedSelections,
|
75 | },
|
76 | };
|
77 | const operationName = (_b = firstRequest.operationName) !== null && _b !== void 0 ? _b : (_e = (_d = (_c = firstRequest.info) === null || _c === void 0 ? void 0 : _c.operation) === null || _d === void 0 ? void 0 : _d.name) === null || _e === void 0 ? void 0 : _e.value;
|
78 | if (operationName) {
|
79 | mergedOperationDefinition.name = {
|
80 | kind: graphql_1.Kind.NAME,
|
81 | value: operationName,
|
82 | };
|
83 | }
|
84 | return {
|
85 | document: {
|
86 | kind: graphql_1.Kind.DOCUMENT,
|
87 | definitions: [mergedOperationDefinition, ...mergedFragmentDefinitions],
|
88 | },
|
89 | variables: mergedVariables,
|
90 | extensions: mergedExtensions,
|
91 | context: requests[0].context,
|
92 | info: requests[0].info,
|
93 | operationType,
|
94 | };
|
95 | }
|
96 | exports.mergeRequests = mergeRequests;
|
97 | function prefixRequest(prefix, request) {
|
98 | var _a;
|
99 | const executionVariables = (_a = request.variables) !== null && _a !== void 0 ? _a : {};
|
100 | function prefixNode(node) {
|
101 | return prefixNodeName(node, prefix);
|
102 | }
|
103 | let prefixedDocument = aliasTopLevelFields(prefix, request.document);
|
104 | const executionVariableNames = Object.keys(executionVariables);
|
105 | const hasFragmentDefinitions = request.document.definitions.some(def => isFragmentDefinition(def));
|
106 | const fragmentSpreadImpl = {};
|
107 | if (executionVariableNames.length > 0 || hasFragmentDefinitions) {
|
108 | prefixedDocument = (0, graphql_1.visit)(prefixedDocument, {
|
109 | [graphql_1.Kind.VARIABLE]: prefixNode,
|
110 | [graphql_1.Kind.FRAGMENT_DEFINITION]: prefixNode,
|
111 | [graphql_1.Kind.FRAGMENT_SPREAD]: node => {
|
112 | node = prefixNodeName(node, prefix);
|
113 | fragmentSpreadImpl[node.name.value] = true;
|
114 | return node;
|
115 | },
|
116 | });
|
117 | }
|
118 | const prefixedVariables = {};
|
119 | for (const variableName of executionVariableNames) {
|
120 | prefixedVariables[prefix + variableName] = executionVariables[variableName];
|
121 | }
|
122 | if (hasFragmentDefinitions) {
|
123 | prefixedDocument = {
|
124 | ...prefixedDocument,
|
125 | definitions: prefixedDocument.definitions.filter(def => {
|
126 | return !isFragmentDefinition(def) || fragmentSpreadImpl[def.name.value];
|
127 | }),
|
128 | };
|
129 | }
|
130 | return {
|
131 | document: prefixedDocument,
|
132 | variables: prefixedVariables,
|
133 | };
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | function aliasTopLevelFields(prefix, document) {
|
141 | const transformer = {
|
142 | [graphql_1.Kind.OPERATION_DEFINITION]: (def) => {
|
143 | const { selections } = def.selectionSet;
|
144 | return {
|
145 | ...def,
|
146 | selectionSet: {
|
147 | ...def.selectionSet,
|
148 | selections: aliasFieldsInSelection(prefix, selections, document),
|
149 | },
|
150 | };
|
151 | },
|
152 | };
|
153 | return (0, graphql_1.visit)(document, transformer, {
|
154 | [graphql_1.Kind.DOCUMENT]: [`definitions`],
|
155 | });
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | function aliasFieldsInSelection(prefix, selections, document) {
|
178 | return selections.map(selection => {
|
179 | switch (selection.kind) {
|
180 | case graphql_1.Kind.INLINE_FRAGMENT:
|
181 | return aliasFieldsInInlineFragment(prefix, selection, document);
|
182 | case graphql_1.Kind.FRAGMENT_SPREAD: {
|
183 | const inlineFragment = inlineFragmentSpread(selection, document);
|
184 | return aliasFieldsInInlineFragment(prefix, inlineFragment, document);
|
185 | }
|
186 | case graphql_1.Kind.FIELD:
|
187 | default:
|
188 | return aliasField(selection, prefix);
|
189 | }
|
190 | });
|
191 | }
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | function aliasFieldsInInlineFragment(prefix, fragment, document) {
|
202 | const { selections } = fragment.selectionSet;
|
203 | return {
|
204 | ...fragment,
|
205 | selectionSet: {
|
206 | ...fragment.selectionSet,
|
207 | selections: aliasFieldsInSelection(prefix, selections, document),
|
208 | },
|
209 | };
|
210 | }
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | function inlineFragmentSpread(spread, document) {
|
222 | const fragment = document.definitions.find(def => isFragmentDefinition(def) && def.name.value === spread.name.value);
|
223 | if (!fragment) {
|
224 | throw new Error(`Fragment ${spread.name.value} does not exist`);
|
225 | }
|
226 | const { typeCondition, selectionSet } = fragment;
|
227 | return {
|
228 | kind: graphql_1.Kind.INLINE_FRAGMENT,
|
229 | typeCondition,
|
230 | selectionSet,
|
231 | directives: spread.directives,
|
232 | };
|
233 | }
|
234 | function prefixNodeName(namedNode, prefix) {
|
235 | return {
|
236 | ...namedNode,
|
237 | name: {
|
238 | ...namedNode.name,
|
239 | value: prefix + namedNode.name.value,
|
240 | },
|
241 | };
|
242 | }
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | function aliasField(field, aliasPrefix) {
|
251 | const aliasNode = field.alias ? field.alias : field.name;
|
252 | return {
|
253 | ...field,
|
254 | alias: {
|
255 | ...aliasNode,
|
256 | value: aliasPrefix + aliasNode.value,
|
257 | },
|
258 | };
|
259 | }
|
260 | function isOperationDefinition(def) {
|
261 | return def.kind === graphql_1.Kind.OPERATION_DEFINITION;
|
262 | }
|
263 | function isFragmentDefinition(def) {
|
264 | return def.kind === graphql_1.Kind.FRAGMENT_DEFINITION;
|
265 | }
|