1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.printSchema = printSchema;
|
7 | exports.printIntrospectionSchema = printIntrospectionSchema;
|
8 | exports.printType = printType;
|
9 |
|
10 | var _flatMap = _interopRequireDefault(require("../polyfills/flatMap"));
|
11 |
|
12 | var _objectValues = _interopRequireDefault(require("../polyfills/objectValues"));
|
13 |
|
14 | var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
|
15 |
|
16 | var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
|
17 |
|
18 | var _printer = require("../language/printer");
|
19 |
|
20 | var _blockString = require("../language/blockString");
|
21 |
|
22 | var _introspection = require("../type/introspection");
|
23 |
|
24 | var _scalars = require("../type/scalars");
|
25 |
|
26 | var _directives = require("../type/directives");
|
27 |
|
28 | var _definition = require("../type/definition");
|
29 |
|
30 | var _astFromValue = require("../utilities/astFromValue");
|
31 |
|
32 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function printSchema(schema, options) {
|
42 | return printFilteredSchema(schema, function (n) {
|
43 | return !(0, _directives.isSpecifiedDirective)(n);
|
44 | }, isDefinedType, options);
|
45 | }
|
46 |
|
47 | function printIntrospectionSchema(schema, options) {
|
48 | return printFilteredSchema(schema, _directives.isSpecifiedDirective, _introspection.isIntrospectionType, options);
|
49 | }
|
50 |
|
51 | function isDefinedType(type) {
|
52 | return !(0, _scalars.isSpecifiedScalarType)(type) && !(0, _introspection.isIntrospectionType)(type);
|
53 | }
|
54 |
|
55 | function printFilteredSchema(schema, directiveFilter, typeFilter, options) {
|
56 | var directives = schema.getDirectives().filter(directiveFilter);
|
57 | var typeMap = schema.getTypeMap();
|
58 | var types = (0, _objectValues.default)(typeMap).sort(function (type1, type2) {
|
59 | return type1.name.localeCompare(type2.name);
|
60 | }).filter(typeFilter);
|
61 | return [printSchemaDefinition(schema)].concat(directives.map(function (directive) {
|
62 | return printDirective(directive, options);
|
63 | }), types.map(function (type) {
|
64 | return printType(type, options);
|
65 | })).filter(Boolean).join('\n\n') + '\n';
|
66 | }
|
67 |
|
68 | function printSchemaDefinition(schema) {
|
69 | if (isSchemaOfCommonNames(schema)) {
|
70 | return;
|
71 | }
|
72 |
|
73 | var operationTypes = [];
|
74 | var queryType = schema.getQueryType();
|
75 |
|
76 | if (queryType) {
|
77 | operationTypes.push(" query: ".concat(queryType.name));
|
78 | }
|
79 |
|
80 | var mutationType = schema.getMutationType();
|
81 |
|
82 | if (mutationType) {
|
83 | operationTypes.push(" mutation: ".concat(mutationType.name));
|
84 | }
|
85 |
|
86 | var subscriptionType = schema.getSubscriptionType();
|
87 |
|
88 | if (subscriptionType) {
|
89 | operationTypes.push(" subscription: ".concat(subscriptionType.name));
|
90 | }
|
91 |
|
92 | return "schema {\n".concat(operationTypes.join('\n'), "\n}");
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function isSchemaOfCommonNames(schema) {
|
109 | var queryType = schema.getQueryType();
|
110 |
|
111 | if (queryType && queryType.name !== 'Query') {
|
112 | return false;
|
113 | }
|
114 |
|
115 | var mutationType = schema.getMutationType();
|
116 |
|
117 | if (mutationType && mutationType.name !== 'Mutation') {
|
118 | return false;
|
119 | }
|
120 |
|
121 | var subscriptionType = schema.getSubscriptionType();
|
122 |
|
123 | if (subscriptionType && subscriptionType.name !== 'Subscription') {
|
124 | return false;
|
125 | }
|
126 |
|
127 | return true;
|
128 | }
|
129 |
|
130 | function printType(type, options) {
|
131 | if ((0, _definition.isScalarType)(type)) {
|
132 | return printScalar(type, options);
|
133 | } else if ((0, _definition.isObjectType)(type)) {
|
134 | return printObject(type, options);
|
135 | } else if ((0, _definition.isInterfaceType)(type)) {
|
136 | return printInterface(type, options);
|
137 | } else if ((0, _definition.isUnionType)(type)) {
|
138 | return printUnion(type, options);
|
139 | } else if ((0, _definition.isEnumType)(type)) {
|
140 | return printEnum(type, options);
|
141 | } else if ((0, _definition.isInputObjectType)(type)) {
|
142 | return printInputObject(type, options);
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 | (0, _invariant.default)(false, 'Unexpected type: ' + (0, _inspect.default)(type));
|
148 | }
|
149 |
|
150 | function printScalar(type, options) {
|
151 | return printDescription(options, type) + "scalar ".concat(type.name);
|
152 | }
|
153 |
|
154 | function printObject(type, options) {
|
155 | var interfaces = type.getInterfaces();
|
156 | var implementedInterfaces = interfaces.length ? ' implements ' + interfaces.map(function (i) {
|
157 | return i.name;
|
158 | }).join(' & ') : '';
|
159 | return printDescription(options, type) + "type ".concat(type.name).concat(implementedInterfaces) + printFields(options, type);
|
160 | }
|
161 |
|
162 | function printInterface(type, options) {
|
163 | return printDescription(options, type) + "interface ".concat(type.name) + printFields(options, type);
|
164 | }
|
165 |
|
166 | function printUnion(type, options) {
|
167 | var types = type.getTypes();
|
168 | var possibleTypes = types.length ? ' = ' + types.join(' | ') : '';
|
169 | return printDescription(options, type) + 'union ' + type.name + possibleTypes;
|
170 | }
|
171 |
|
172 | function printEnum(type, options) {
|
173 | var values = type.getValues().map(function (value, i) {
|
174 | return printDescription(options, value, ' ', !i) + ' ' + value.name + printDeprecated(value);
|
175 | });
|
176 | return printDescription(options, type) + "enum ".concat(type.name) + printBlock(values);
|
177 | }
|
178 |
|
179 | function printInputObject(type, options) {
|
180 | var fields = (0, _objectValues.default)(type.getFields()).map(function (f, i) {
|
181 | return printDescription(options, f, ' ', !i) + ' ' + printInputValue(f);
|
182 | });
|
183 | return printDescription(options, type) + "input ".concat(type.name) + printBlock(fields);
|
184 | }
|
185 |
|
186 | function printFields(options, type) {
|
187 | var fields = (0, _objectValues.default)(type.getFields()).map(function (f, i) {
|
188 | return printDescription(options, f, ' ', !i) + ' ' + f.name + printArgs(options, f.args, ' ') + ': ' + String(f.type) + printDeprecated(f);
|
189 | });
|
190 | return printBlock(fields);
|
191 | }
|
192 |
|
193 | function printBlock(items) {
|
194 | return items.length !== 0 ? ' {\n' + items.join('\n') + '\n}' : '';
|
195 | }
|
196 |
|
197 | function printArgs(options, args) {
|
198 | var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
199 |
|
200 | if (args.length === 0) {
|
201 | return '';
|
202 | }
|
203 |
|
204 |
|
205 | if (args.every(function (arg) {
|
206 | return !arg.description;
|
207 | })) {
|
208 | return '(' + args.map(printInputValue).join(', ') + ')';
|
209 | }
|
210 |
|
211 | return '(\n' + args.map(function (arg, i) {
|
212 | return printDescription(options, arg, ' ' + indentation, !i) + ' ' + indentation + printInputValue(arg);
|
213 | }).join('\n') + '\n' + indentation + ')';
|
214 | }
|
215 |
|
216 | function printInputValue(arg) {
|
217 | var defaultAST = (0, _astFromValue.astFromValue)(arg.defaultValue, arg.type);
|
218 | var argDecl = arg.name + ': ' + String(arg.type);
|
219 |
|
220 | if (defaultAST) {
|
221 | argDecl += " = ".concat((0, _printer.print)(defaultAST));
|
222 | }
|
223 |
|
224 | return argDecl;
|
225 | }
|
226 |
|
227 | function printDirective(directive, options) {
|
228 | return printDescription(options, directive) + 'directive @' + directive.name + printArgs(options, directive.args) + (directive.isRepeatable ? ' repeatable' : '') + ' on ' + directive.locations.join(' | ');
|
229 | }
|
230 |
|
231 | function printDeprecated(fieldOrEnumVal) {
|
232 | if (!fieldOrEnumVal.isDeprecated) {
|
233 | return '';
|
234 | }
|
235 |
|
236 | var reason = fieldOrEnumVal.deprecationReason;
|
237 | var reasonAST = (0, _astFromValue.astFromValue)(reason, _scalars.GraphQLString);
|
238 |
|
239 | if (reasonAST && reason !== '' && reason !== _directives.DEFAULT_DEPRECATION_REASON) {
|
240 | return ' @deprecated(reason: ' + (0, _printer.print)(reasonAST) + ')';
|
241 | }
|
242 |
|
243 | return ' @deprecated';
|
244 | }
|
245 |
|
246 | function printDescription(options, def) {
|
247 | var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
248 | var firstInBlock = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
|
249 |
|
250 | if (!def.description) {
|
251 | return '';
|
252 | }
|
253 |
|
254 | var lines = descriptionLines(def.description, 120 - indentation.length);
|
255 |
|
256 | if (options && options.commentDescriptions) {
|
257 | return printDescriptionWithComments(lines, indentation, firstInBlock);
|
258 | }
|
259 |
|
260 | var text = lines.join('\n');
|
261 | var preferMultipleLines = text.length > 70;
|
262 | var blockString = (0, _blockString.printBlockString)(text, '', preferMultipleLines);
|
263 | var prefix = indentation && !firstInBlock ? '\n' + indentation : indentation;
|
264 | return prefix + blockString.replace(/\n/g, '\n' + indentation) + '\n';
|
265 | }
|
266 |
|
267 | function printDescriptionWithComments(lines, indentation, firstInBlock) {
|
268 | var description = indentation && !firstInBlock ? '\n' : '';
|
269 |
|
270 | for (var _i2 = 0; _i2 < lines.length; _i2++) {
|
271 | var line = lines[_i2];
|
272 |
|
273 | if (line === '') {
|
274 | description += indentation + '#\n';
|
275 | } else {
|
276 | description += indentation + '# ' + line + '\n';
|
277 | }
|
278 | }
|
279 |
|
280 | return description;
|
281 | }
|
282 |
|
283 | function descriptionLines(description, maxLen) {
|
284 | var rawLines = description.split('\n');
|
285 | return (0, _flatMap.default)(rawLines, function (line) {
|
286 | if (line.length < maxLen + 5) {
|
287 | return line;
|
288 | }
|
289 |
|
290 |
|
291 |
|
292 | return breakLine(line, maxLen);
|
293 | });
|
294 | }
|
295 |
|
296 | function breakLine(line, maxLen) {
|
297 | var parts = line.split(new RegExp("((?: |^).{15,".concat(maxLen - 40, "}(?= |$))")));
|
298 |
|
299 | if (parts.length < 4) {
|
300 | return [line];
|
301 | }
|
302 |
|
303 | var sublines = [parts[0] + parts[1] + parts[2]];
|
304 |
|
305 | for (var i = 3; i < parts.length; i += 2) {
|
306 | sublines.push(parts[i].slice(1) + parts[i + 1]);
|
307 | }
|
308 |
|
309 | return sublines;
|
310 | }
|