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 | function inferRootArgumentDefinitions(context) {
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var transformed = new Map();
|
39 | var nextContext = new (require("./GraphQLCompilerContext"))(context.serverSchema, context.clientSchema);
|
40 | return nextContext.addAll(Array.from(context.documents(), function (node) {
|
41 | switch (node.kind) {
|
42 | case 'Fragment':
|
43 | {
|
44 | var argumentDefinitions = transformFragmentArguments(context, transformed, node);
|
45 | return (0, _objectSpread2["default"])({}, node, {
|
46 | argumentDefinitions: Array.from(argumentDefinitions.values())
|
47 | });
|
48 | }
|
49 |
|
50 | case 'Root':
|
51 | {
|
52 | return transformRoot(context, transformed, node);
|
53 | }
|
54 |
|
55 | case 'SplitOperation':
|
56 | {
|
57 | return node;
|
58 | }
|
59 |
|
60 | default:
|
61 | {
|
62 | node;
|
63 | throw require("./RelayCompilerError").createCompilerError("inferRootArgumentDefinitions: Unsupported kind '".concat(node.kind, "'."));
|
64 | }
|
65 | }
|
66 | }));
|
67 | }
|
68 |
|
69 | function transformRoot(context, transformed, root) {
|
70 |
|
71 |
|
72 | var argumentDefinitions = new Map();
|
73 | var localArgumentDefinitions = new Map();
|
74 | var _iteratorNormalCompletion = true;
|
75 | var _didIteratorError = false;
|
76 | var _iteratorError = undefined;
|
77 |
|
78 | try {
|
79 | for (var _iterator = root.argumentDefinitions.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
80 | var _step$value = _step.value,
|
81 | name = _step$value[0],
|
82 | argDef = _step$value[1];
|
83 |
|
84 | if (argDef.kind === 'LocalArgumentDefinition') {
|
85 | localArgumentDefinitions.set(name, argDef);
|
86 | }
|
87 | }
|
88 | } catch (err) {
|
89 | _didIteratorError = true;
|
90 | _iteratorError = err;
|
91 | } finally {
|
92 | try {
|
93 | if (!_iteratorNormalCompletion && _iterator["return"] != null) {
|
94 | _iterator["return"]();
|
95 | }
|
96 | } finally {
|
97 | if (_didIteratorError) {
|
98 | throw _iteratorError;
|
99 | }
|
100 | }
|
101 | }
|
102 |
|
103 | visit(context, transformed, argumentDefinitions, root);
|
104 | return (0, _objectSpread2["default"])({}, root, {
|
105 | argumentDefinitions: Array.from(argumentDefinitions.values(), function (argDef) {
|
106 | var _ref, _ref2;
|
107 |
|
108 | if (argDef.kind !== 'RootArgumentDefinition') {
|
109 | throw require("./RelayCompilerError").createCompilerError("inferRootArgumentDefinitions: Expected inferred variable '$".concat(argDef.name, "' to be a root variables."), [argDef.loc]);
|
110 | }
|
111 |
|
112 | var localDefinition = localArgumentDefinitions.get(argDef.name);
|
113 | return {
|
114 | defaultValue: (_ref = localDefinition === null || localDefinition === void 0 ? void 0 : localDefinition.defaultValue) !== null && _ref !== void 0 ? _ref : null,
|
115 | kind: 'LocalArgumentDefinition',
|
116 | loc: argDef.loc,
|
117 | metadata: null,
|
118 | name: argDef.name,
|
119 | type: (_ref2 = localDefinition === null || localDefinition === void 0 ? void 0 : localDefinition.type) !== null && _ref2 !== void 0 ? _ref2 : argDef.type
|
120 | };
|
121 | })
|
122 | });
|
123 | }
|
124 |
|
125 | function transformFragmentArguments(context, transformed, fragment) {
|
126 | var name = fragment.name;
|
127 | var transformedArguments = transformed.get(name);
|
128 |
|
129 | if (transformedArguments != null) {
|
130 | return transformedArguments;
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 | var argumentDefinitions = new Map();
|
136 | fragment.argumentDefinitions.forEach(function (argDef) {
|
137 | if (argDef.kind === 'LocalArgumentDefinition') {
|
138 | argumentDefinitions.set(argDef.name, argDef);
|
139 | }
|
140 | });
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | transformed.set(name, argumentDefinitions);
|
147 | visit(context, transformed, argumentDefinitions, fragment);
|
148 | transformed.set(name, argumentDefinitions);
|
149 | return argumentDefinitions;
|
150 | }
|
151 |
|
152 | function visit(context, transformed, argumentDefinitions, node) {
|
153 | require("./GraphQLIRVisitor").visit(node, {
|
154 | FragmentSpread: function FragmentSpread(fragmentSpread) {
|
155 | var fragment;
|
156 |
|
157 | try {
|
158 | fragment = context.getFragment(fragmentSpread.name);
|
159 | } catch (_unused) {
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | return false;
|
170 | }
|
171 |
|
172 | var referencedFragmentArguments = transformFragmentArguments(context, transformed, fragment);
|
173 |
|
174 |
|
175 | fragmentSpread.args.forEach(function (arg) {
|
176 | var argDef = referencedFragmentArguments.get(arg.name);
|
177 |
|
178 | if (argDef != null && arg.value.kind === 'Variable' && !argumentDefinitions.has(arg.value.variableName)) {
|
179 | argumentDefinitions.set(arg.value.variableName, {
|
180 | kind: 'RootArgumentDefinition',
|
181 | loc: {
|
182 | kind: 'Derived',
|
183 | source: arg.loc
|
184 | },
|
185 | metadata: null,
|
186 | name: arg.value.variableName,
|
187 | type: argDef.type
|
188 | });
|
189 | }
|
190 | });
|
191 |
|
192 |
|
193 | var _iteratorNormalCompletion2 = true;
|
194 | var _didIteratorError2 = false;
|
195 | var _iteratorError2 = undefined;
|
196 |
|
197 | try {
|
198 | for (var _iterator2 = referencedFragmentArguments.values()[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
199 | var argDef = _step2.value;
|
200 |
|
201 | if (argDef.kind === 'RootArgumentDefinition' && !argumentDefinitions.has(argDef.name)) {
|
202 | argumentDefinitions.set(argDef.name, argDef);
|
203 | }
|
204 | }
|
205 | } catch (err) {
|
206 | _didIteratorError2 = true;
|
207 | _iteratorError2 = err;
|
208 | } finally {
|
209 | try {
|
210 | if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
|
211 | _iterator2["return"]();
|
212 | }
|
213 | } finally {
|
214 | if (_didIteratorError2) {
|
215 | throw _iteratorError2;
|
216 | }
|
217 | }
|
218 | }
|
219 |
|
220 | return false;
|
221 | },
|
222 | Argument: function Argument(argument) {
|
223 | if (argument.value.kind !== 'Variable') {
|
224 | return false;
|
225 | }
|
226 |
|
227 | var variable = argument.value;
|
228 |
|
229 | if (argument.type == null && variable.type == null) {
|
230 | return;
|
231 | }
|
232 |
|
233 | if (!argumentDefinitions.has(variable.variableName)) {
|
234 |
|
235 | argumentDefinitions.set(variable.variableName, {
|
236 | kind: 'RootArgumentDefinition',
|
237 | loc: {
|
238 | kind: 'Derived',
|
239 | source: argument.loc
|
240 | },
|
241 | metadata: null,
|
242 | name: variable.variableName,
|
243 | type: variable.type || argument.type
|
244 | });
|
245 | }
|
246 |
|
247 | return false;
|
248 | },
|
249 | Condition: function Condition(condition) {
|
250 | var _variable$type;
|
251 |
|
252 | if (condition.condition.kind !== 'Variable') {
|
253 | return;
|
254 | }
|
255 |
|
256 | var variable = condition.condition;
|
257 | var type = (_variable$type = variable.type) !== null && _variable$type !== void 0 ? _variable$type : new (require("graphql").GraphQLNonNull)(require("graphql").GraphQLBoolean);
|
258 |
|
259 | if (!argumentDefinitions.has(variable.variableName)) {
|
260 |
|
261 | argumentDefinitions.set(variable.variableName, {
|
262 | kind: 'RootArgumentDefinition',
|
263 | loc: {
|
264 | kind: 'Derived',
|
265 | source: condition.loc
|
266 | },
|
267 | metadata: null,
|
268 | name: variable.variableName,
|
269 | type: type
|
270 | });
|
271 | }
|
272 | }
|
273 | });
|
274 | }
|
275 |
|
276 | module.exports = inferRootArgumentDefinitions; |
\ | No newline at end of file |