1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 | var _objectSpread2 = require("@babel/runtime/helpers/interopRequireDefault")(require("@babel/runtime/helpers/objectSpread"));
|
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 | function transform(context, visitor, stateInitializer) {
|
56 | var transformer = new Transformer(context, visitor);
|
57 | return context.withMutations(function (ctx) {
|
58 | var nextContext = ctx;
|
59 |
|
60 | var errors = require("./RelayCompilerError").eachWithErrors(context.documents(), function (prevNode) {
|
61 | var nextNode;
|
62 |
|
63 | if (stateInitializer === undefined) {
|
64 | nextNode = transformer.visit(prevNode, undefined);
|
65 | } else {
|
66 | var _state = stateInitializer(prevNode);
|
67 |
|
68 | if (_state != null) {
|
69 | nextNode = transformer.visit(prevNode, _state);
|
70 | }
|
71 | }
|
72 |
|
73 | if (!nextNode) {
|
74 | nextContext = nextContext.remove(prevNode.name);
|
75 | } else if (nextNode !== prevNode) {
|
76 | nextContext = nextContext.replace(nextNode);
|
77 | }
|
78 | });
|
79 |
|
80 | if (errors != null && errors.length !== 0) {
|
81 | throw require("./RelayCompilerError").createCombinedError(errors);
|
82 | }
|
83 |
|
84 | return nextContext;
|
85 | });
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | var Transformer =
|
93 |
|
94 | function () {
|
95 | function Transformer(context, visitor) {
|
96 | this._context = context;
|
97 | this._states = [];
|
98 | this._visitor = visitor;
|
99 | }
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | var _proto = Transformer.prototype;
|
109 |
|
110 | _proto.getContext = function getContext() {
|
111 | return this._context;
|
112 | };
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | _proto.visit = function visit(node, state) {
|
125 | this._states.push(state);
|
126 |
|
127 | var nextNode = this._visit(node);
|
128 |
|
129 | this._states.pop();
|
130 |
|
131 | return nextNode;
|
132 | };
|
133 | |
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | _proto.traverse = function traverse(node, state) {
|
146 | this._states.push(state);
|
147 |
|
148 | var nextNode = this._traverse(node);
|
149 |
|
150 | this._states.pop();
|
151 |
|
152 | return nextNode;
|
153 | };
|
154 |
|
155 | _proto._visit = function _visit(node) {
|
156 | var nodeVisitor = this._visitor[node.kind];
|
157 |
|
158 | if (nodeVisitor) {
|
159 |
|
160 |
|
161 | var _state2 = this._getState();
|
162 |
|
163 | var nextNode = nodeVisitor.call(this, node, _state2);
|
164 | return nextNode;
|
165 | }
|
166 |
|
167 |
|
168 | return this._traverse(node);
|
169 | };
|
170 |
|
171 | _proto._traverse = function _traverse(prevNode) {
|
172 | var nextNode;
|
173 |
|
174 | switch (prevNode.kind) {
|
175 | case 'Argument':
|
176 | nextNode = this._traverseChildren(prevNode, null, ['value']);
|
177 | break;
|
178 |
|
179 | case 'Literal':
|
180 | case 'LocalArgumentDefinition':
|
181 | case 'RootArgumentDefinition':
|
182 | case 'Variable':
|
183 | nextNode = prevNode;
|
184 | break;
|
185 |
|
186 | case 'Defer':
|
187 | nextNode = this._traverseChildren(prevNode, ['selections'], ['if']);
|
188 | break;
|
189 |
|
190 | case 'Stream':
|
191 | nextNode = this._traverseChildren(prevNode, ['selections'], ['if', 'initialCount']);
|
192 | break;
|
193 |
|
194 | case 'Directive':
|
195 | nextNode = this._traverseChildren(prevNode, ['args']);
|
196 | break;
|
197 |
|
198 | case 'MatchBranch':
|
199 | nextNode = this._traverseChildren(prevNode, ['selections']);
|
200 |
|
201 | if (!nextNode.selections.length) {
|
202 | nextNode = null;
|
203 | }
|
204 |
|
205 | break;
|
206 |
|
207 | case 'FragmentSpread':
|
208 | case 'ScalarField':
|
209 | nextNode = this._traverseChildren(prevNode, ['args', 'directives']);
|
210 | break;
|
211 |
|
212 | case 'LinkedField':
|
213 | nextNode = this._traverseChildren(prevNode, ['args', 'directives', 'selections']);
|
214 |
|
215 | if (!nextNode.selections.length) {
|
216 | nextNode = null;
|
217 | }
|
218 |
|
219 | break;
|
220 |
|
221 | case 'ListValue':
|
222 | nextNode = this._traverseChildren(prevNode, ['items']);
|
223 | break;
|
224 |
|
225 | case 'MatchField':
|
226 | nextNode = this._traverseChildren(prevNode, ['args', 'directives', 'selections']);
|
227 | break;
|
228 |
|
229 | case 'ObjectFieldValue':
|
230 | nextNode = this._traverseChildren(prevNode, null, ['value']);
|
231 | break;
|
232 |
|
233 | case 'ObjectValue':
|
234 | nextNode = this._traverseChildren(prevNode, ['fields']);
|
235 | break;
|
236 |
|
237 | case 'Condition':
|
238 | nextNode = this._traverseChildren(prevNode, ['directives', 'selections'], ['condition']);
|
239 |
|
240 | if (!nextNode.selections.length) {
|
241 | nextNode = null;
|
242 | }
|
243 |
|
244 | break;
|
245 |
|
246 | case 'InlineFragment':
|
247 | nextNode = this._traverseChildren(prevNode, ['directives', 'selections']);
|
248 |
|
249 | if (!nextNode.selections.length) {
|
250 | nextNode = null;
|
251 | }
|
252 |
|
253 | break;
|
254 |
|
255 | case 'Fragment':
|
256 | case 'Root':
|
257 | nextNode = this._traverseChildren(prevNode, ['argumentDefinitions', 'directives', 'selections']);
|
258 | break;
|
259 |
|
260 | case 'Request':
|
261 | nextNode = this._traverseChildren(prevNode, null, ['fragment', 'root']);
|
262 | break;
|
263 |
|
264 | case 'SplitOperation':
|
265 | nextNode = this._traverseChildren(prevNode, ['selections']);
|
266 | break;
|
267 |
|
268 | default:
|
269 | prevNode;
|
270 | !false ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRTransformer: Unknown kind `%s`.', prevNode.kind) : require("fbjs/lib/invariant")(false) : void 0;
|
271 | }
|
272 |
|
273 | return nextNode;
|
274 | };
|
275 |
|
276 | _proto._traverseChildren = function _traverseChildren(prevNode, pluralKeys, singularKeys) {
|
277 | var _this = this;
|
278 |
|
279 | var nextNode;
|
280 | pluralKeys && pluralKeys.forEach(function (key) {
|
281 | var prevItems = prevNode[key];
|
282 |
|
283 | if (!prevItems) {
|
284 | return;
|
285 | }
|
286 |
|
287 | !Array.isArray(prevItems) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRTransformer: Expected data for `%s` to be an array, got `%s`.', key, prevItems) : require("fbjs/lib/invariant")(false) : void 0;
|
288 |
|
289 | var nextItems = _this._map(prevItems);
|
290 |
|
291 | if (nextNode || nextItems !== prevItems) {
|
292 | nextNode = nextNode || (0, _objectSpread2["default"])({}, prevNode);
|
293 | nextNode[key] = nextItems;
|
294 | }
|
295 | });
|
296 | singularKeys && singularKeys.forEach(function (key) {
|
297 | var prevItem = prevNode[key];
|
298 |
|
299 | if (!prevItem) {
|
300 | return;
|
301 | }
|
302 |
|
303 | var nextItem = _this._visit(prevItem);
|
304 |
|
305 | if (nextNode || nextItem !== prevItem) {
|
306 | nextNode = nextNode || (0, _objectSpread2["default"])({}, prevNode);
|
307 | nextNode[key] = nextItem;
|
308 | }
|
309 | });
|
310 | return nextNode || prevNode;
|
311 | };
|
312 |
|
313 | _proto._map = function _map(prevItems) {
|
314 | var _this2 = this;
|
315 |
|
316 | var nextItems;
|
317 | prevItems.forEach(function (prevItem, index) {
|
318 | var nextItem = _this2._visit(prevItem);
|
319 |
|
320 | if (nextItems || nextItem !== prevItem) {
|
321 | nextItems = nextItems || prevItems.slice(0, index);
|
322 |
|
323 | if (nextItem) {
|
324 | nextItems.push(nextItem);
|
325 | }
|
326 | }
|
327 | });
|
328 | return nextItems || prevItems;
|
329 | };
|
330 |
|
331 | _proto._getState = function _getState() {
|
332 | !this._states.length ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRTransformer: Expected a current state to be set but found none. ' + 'This is usually the result of mismatched number of pushState()/popState() ' + 'calls.') : require("fbjs/lib/invariant")(false) : void 0;
|
333 | return this._states[this._states.length - 1];
|
334 | };
|
335 |
|
336 | return Transformer;
|
337 | }();
|
338 |
|
339 | module.exports = {
|
340 | transform: transform
|
341 | }; |
\ | No newline at end of file |