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