1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 | var INDENT = ' ';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function print(node) {
|
21 | switch (node.kind) {
|
22 | case 'Fragment':
|
23 | return "fragment ".concat(node.name, " on ").concat(String(node.type)) + printFragmentArgumentDefinitions(node.argumentDefinitions) + printDirectives(node.directives) + printSelections(node, '') + '\n';
|
24 |
|
25 | case 'Root':
|
26 | return "".concat(node.operation, " ").concat(node.name) + printArgumentDefinitions(node.argumentDefinitions) + printDirectives(node.directives) + printSelections(node, '') + '\n';
|
27 |
|
28 | case 'SplitOperation':
|
29 | return "SplitOperation ".concat(node.name, " on ").concat(String(node.type)) + printSelections(node, '') + '\n';
|
30 |
|
31 | default:
|
32 | node;
|
33 | !false ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRPrinter: Unsupported IR node `%s`.', node.kind) : require("fbjs/lib/invariant")(false) : void 0;
|
34 | }
|
35 | }
|
36 |
|
37 | function printSelections(node, indent, parentDirectives) {
|
38 | var selections = node.selections;
|
39 |
|
40 | if (selections == null) {
|
41 | return '';
|
42 | }
|
43 |
|
44 | var printed = selections.map(function (selection) {
|
45 | return printSelection(selection, indent, parentDirectives);
|
46 | });
|
47 | return printed.length ? " {\n".concat(indent + INDENT).concat(printed.join('\n' + indent + INDENT), "\n").concat(indent, "}") : '';
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function printField(field) {
|
55 | var parentDirectives = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
56 | return (field.alias != null ? field.alias + ': ' + field.name : field.name) + printArguments(field.args) + parentDirectives + printDirectives(field.directives) + printHandles(field);
|
57 | }
|
58 |
|
59 | function printSelection(selection, indent) {
|
60 | var parentDirectives = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
61 | var str;
|
62 |
|
63 | if (selection.kind === 'LinkedField') {
|
64 | str = printField(selection, parentDirectives);
|
65 | str += printSelections(selection, indent + INDENT);
|
66 | } else if (selection.kind === 'MatchField') {
|
67 | str = printField(selection, parentDirectives);
|
68 | str += printSelections(selection, indent + INDENT);
|
69 | } else if (selection.kind === 'MatchBranch') {
|
70 | str = selection.selections.map(function (matchSelection) {
|
71 | return printSelection(matchSelection, indent);
|
72 | }).join('\n' + indent + INDENT);
|
73 | } else if (selection.kind === 'ScalarField') {
|
74 | str = printField(selection, parentDirectives);
|
75 | } else if (selection.kind === 'InlineFragment') {
|
76 | str = '... on ' + selection.typeCondition.toString();
|
77 | str += parentDirectives;
|
78 | str += printDirectives(selection.directives);
|
79 | str += printSelections(selection, indent + INDENT);
|
80 | } else if (selection.kind === 'FragmentSpread') {
|
81 | str = '...' + selection.name;
|
82 | str += parentDirectives;
|
83 | str += printFragmentArguments(selection.args);
|
84 | str += printDirectives(selection.directives);
|
85 | } else if (selection.kind === 'Condition') {
|
86 | var value = printValue(selection.condition);
|
87 |
|
88 | !(value != null) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRPrinter: Expected a variable for condition, got a literal `null`.') : require("fbjs/lib/invariant")(false) : void 0;
|
89 | var condStr = selection.passingValue ? ' @include' : ' @skip';
|
90 | condStr += '(if: ' + value + ')';
|
91 | condStr += parentDirectives;
|
92 |
|
93 | var subSelections = selection.selections.map(function (sel) {
|
94 | return printSelection(sel, indent, condStr);
|
95 | });
|
96 | str = subSelections.join('\n' + INDENT);
|
97 | } else if (selection.kind === 'Stream') {
|
98 | var streamStr = " @stream(label: \"".concat(selection.label, "\"");
|
99 |
|
100 | if (selection["if"] !== null) {
|
101 | var _printValue;
|
102 |
|
103 | streamStr += ", if: ".concat((_printValue = printValue(selection["if"])) !== null && _printValue !== void 0 ? _printValue : '');
|
104 | }
|
105 |
|
106 | if (selection.initialCount !== null) {
|
107 | var _printValue2;
|
108 |
|
109 | streamStr += ", initial_count: ".concat((_printValue2 = printValue(selection.initialCount)) !== null && _printValue2 !== void 0 ? _printValue2 : '');
|
110 | }
|
111 |
|
112 | streamStr += ')';
|
113 | streamStr += parentDirectives;
|
114 |
|
115 | var _subSelections = selection.selections.map(function (sel) {
|
116 | return printSelection(sel, indent, streamStr);
|
117 | });
|
118 |
|
119 | str = _subSelections.join('\n' + INDENT);
|
120 | } else if (selection.kind === 'Defer') {
|
121 | var deferStr = " @defer(label: \"".concat(selection.label, "\"");
|
122 |
|
123 | if (selection["if"] !== null) {
|
124 | var _printValue3;
|
125 |
|
126 | deferStr += ", if: ".concat((_printValue3 = printValue(selection["if"])) !== null && _printValue3 !== void 0 ? _printValue3 : '');
|
127 | }
|
128 |
|
129 | deferStr += ')';
|
130 | deferStr += parentDirectives;
|
131 |
|
132 | var _subSelections2 = selection.selections.map(function (sel) {
|
133 | return printSelection(sel, indent, deferStr);
|
134 | });
|
135 |
|
136 | str = _subSelections2.join('\n' + INDENT);
|
137 | } else {
|
138 | selection;
|
139 | !false ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRPrinter: Unknown selection kind `%s`.', selection.kind) : require("fbjs/lib/invariant")(false) : void 0;
|
140 | }
|
141 |
|
142 | return str;
|
143 | }
|
144 |
|
145 | function printArgumentDefinitions(argumentDefinitions) {
|
146 | var printed = argumentDefinitions.map(function (def) {
|
147 | var str = "$".concat(def.name, ": ").concat(def.type.toString());
|
148 |
|
149 | if (def.defaultValue != null) {
|
150 | str += ' = ' + printLiteral(def.defaultValue, def.type);
|
151 | }
|
152 |
|
153 | return str;
|
154 | });
|
155 | return printed.length ? "(\n".concat(INDENT).concat(printed.join('\n' + INDENT), "\n)") : '';
|
156 | }
|
157 |
|
158 | function printFragmentArgumentDefinitions(argumentDefinitions) {
|
159 | var printed;
|
160 | argumentDefinitions.forEach(function (def) {
|
161 | if (def.kind !== 'LocalArgumentDefinition') {
|
162 | return;
|
163 | }
|
164 |
|
165 | printed = printed || [];
|
166 | var str = "".concat(def.name, ": {type: \"").concat(def.type.toString(), "\"");
|
167 |
|
168 | if (def.defaultValue != null) {
|
169 | str += ", defaultValue: ".concat(printLiteral(def.defaultValue, def.type));
|
170 | }
|
171 |
|
172 | str += '}';
|
173 | printed.push(str);
|
174 | });
|
175 | return printed && printed.length ? " @argumentDefinitions(\n".concat(INDENT).concat(printed.join('\n' + INDENT), "\n)") : '';
|
176 | }
|
177 |
|
178 | function printHandles(field) {
|
179 | if (!field.handles) {
|
180 | return '';
|
181 | }
|
182 |
|
183 | var printed = field.handles.map(function (handle) {
|
184 |
|
185 | var key = handle.key === require("./DefaultHandleKey").DEFAULT_HANDLE_KEY ? '' : ", key: \"".concat(handle.key, "\"");
|
186 | var filters = handle.filters == null ? '' : ", filters: ".concat(JSON.stringify(Array.from(handle.filters).sort()));
|
187 | return "@__clientField(handle: \"".concat(handle.name, "\"").concat(key).concat(filters, ")");
|
188 | });
|
189 | return printed.length ? ' ' + printed.join(' ') : '';
|
190 | }
|
191 |
|
192 | function printDirectives(directives) {
|
193 | var printed = directives.map(function (directive) {
|
194 | return '@' + directive.name + printArguments(directive.args);
|
195 | });
|
196 | return printed.length ? ' ' + printed.join(' ') : '';
|
197 | }
|
198 |
|
199 | function printFragmentArguments(args) {
|
200 | var printedArgs = printArguments(args);
|
201 |
|
202 | if (!printedArgs.length) {
|
203 | return '';
|
204 | }
|
205 |
|
206 | return " @arguments".concat(printedArgs);
|
207 | }
|
208 |
|
209 | function printArguments(args) {
|
210 | var printed = [];
|
211 | args.forEach(function (arg) {
|
212 | var printedValue = printValue(arg.value, arg.type);
|
213 |
|
214 | if (printedValue != null) {
|
215 | printed.push(arg.name + ': ' + printedValue);
|
216 | }
|
217 | });
|
218 | return printed.length ? '(' + printed.join(', ') + ')' : '';
|
219 | }
|
220 |
|
221 | function printValue(value, type) {
|
222 | if (type instanceof require("graphql").GraphQLNonNull) {
|
223 | type = type.ofType;
|
224 | }
|
225 |
|
226 | if (value.kind === 'Variable') {
|
227 | return '$' + value.variableName;
|
228 | } else if (value.kind === 'ObjectValue') {
|
229 | !(type instanceof require("graphql").GraphQLInputObjectType) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRPrinter: Need an InputObject type to print objects.') : require("fbjs/lib/invariant")(false) : void 0;
|
230 | var typeFields = type.getFields();
|
231 | var pairs = value.fields.map(function (field) {
|
232 | var innerValue = printValue(field.value, typeFields[field.name].type);
|
233 | return innerValue == null ? null : field.name + ': ' + innerValue;
|
234 | }).filter(Boolean);
|
235 | return '{' + pairs.join(', ') + '}';
|
236 | } else if (value.kind === 'ListValue') {
|
237 | !(type instanceof require("graphql").GraphQLList) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRPrinter: Need a type in order to print arrays.') : require("fbjs/lib/invariant")(false) : void 0;
|
238 | var innerType = type.ofType;
|
239 | return "[".concat(value.items.map(function (i) {
|
240 | return printValue(i, innerType);
|
241 | }).join(', '), "]");
|
242 | } else if (value.value != null) {
|
243 | return printLiteral(value.value, type);
|
244 | } else {
|
245 | return null;
|
246 | }
|
247 | }
|
248 |
|
249 | function printLiteral(value, type) {
|
250 | if (type instanceof require("graphql").GraphQLNonNull) {
|
251 | type = type.ofType;
|
252 | }
|
253 |
|
254 | if (type instanceof require("graphql").GraphQLEnumType) {
|
255 | !(typeof value === 'string') ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRPrinter: Expected value of type %s to be a string, got `%s`.', type.name, value) : require("fbjs/lib/invariant")(false) : void 0;
|
256 | return value;
|
257 | }
|
258 |
|
259 | if (Array.isArray(value)) {
|
260 | !(type instanceof require("graphql").GraphQLList) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRPrinter: Need a type in order to print arrays.') : require("fbjs/lib/invariant")(false) : void 0;
|
261 | var itemType = type.ofType;
|
262 | return '[' + value.map(function (item) {
|
263 | return printLiteral(item, itemType);
|
264 | }).join(', ') + ']';
|
265 | } else if (typeof value === 'object' && value != null) {
|
266 | var fields = [];
|
267 | !(type instanceof require("graphql").GraphQLInputObjectType) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLIRPrinter: Need an InputObject type to print objects.') : require("fbjs/lib/invariant")(false) : void 0;
|
268 | var typeFields = type.getFields();
|
269 |
|
270 | for (var key in value) {
|
271 | if (value.hasOwnProperty(key)) {
|
272 | fields.push(key + ': ' + printLiteral(value[key], typeFields[key].type));
|
273 | }
|
274 | }
|
275 |
|
276 | return '{' + fields.join(', ') + '}';
|
277 | } else if (type instanceof require("graphql").GraphQLList && value != null) {
|
278 |
|
279 |
|
280 | return printLiteral(value, type.ofType);
|
281 | } else {
|
282 | return JSON.stringify(value);
|
283 | }
|
284 | }
|
285 |
|
286 | module.exports = {
|
287 | print: print,
|
288 | printField: printField,
|
289 | printArguments: printArguments,
|
290 | printDirectives: printDirectives
|
291 | }; |
\ | No newline at end of file |