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