UNPKG

9.33 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.printSchema = printSchema;
7exports.printIntrospectionSchema = printIntrospectionSchema;
8exports.printType = printType;
9
10var _objectValues = _interopRequireDefault(require("../polyfills/objectValues.js"));
11
12var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
13
14var _invariant = _interopRequireDefault(require("../jsutils/invariant.js"));
15
16var _printer = require("../language/printer.js");
17
18var _blockString = require("../language/blockString.js");
19
20var _introspection = require("../type/introspection.js");
21
22var _scalars = require("../type/scalars.js");
23
24var _directives = require("../type/directives.js");
25
26var _definition = require("../type/definition.js");
27
28var _astFromValue = require("./astFromValue.js");
29
30function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
32/**
33 * Accepts options as a second argument:
34 *
35 * - commentDescriptions:
36 * Provide true to use preceding comments as the description.
37 *
38 */
39function printSchema(schema, options) {
40 return printFilteredSchema(schema, function (n) {
41 return !(0, _directives.isSpecifiedDirective)(n);
42 }, isDefinedType, options);
43}
44
45function printIntrospectionSchema(schema, options) {
46 return printFilteredSchema(schema, _directives.isSpecifiedDirective, _introspection.isIntrospectionType, options);
47}
48
49function isDefinedType(type) {
50 return !(0, _scalars.isSpecifiedScalarType)(type) && !(0, _introspection.isIntrospectionType)(type);
51}
52
53function 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
63function 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 * GraphQL schema define root types for each type of operation. These types are
91 * the same as any other type and can be named in any manner, however there is
92 * a common naming convention:
93 *
94 * schema {
95 * query: Query
96 * mutation: Mutation
97 * }
98 *
99 * When using this naming convention, the schema description can be omitted.
100 */
101
102
103function 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
125function 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 } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
145
146
147 if ((0, _definition.isInputObjectType)(type)) {
148 return printInputObject(type, options);
149 } // istanbul ignore next (Not reachable. All possible types have been considered)
150
151
152 false || (0, _invariant.default)(0, 'Unexpected type: ' + (0, _inspect.default)(type));
153}
154
155function printScalar(type, options) {
156 return printDescription(options, type) + "scalar ".concat(type.name) + printSpecifiedByUrl(type);
157}
158
159function printImplementedInterfaces(type) {
160 var interfaces = type.getInterfaces();
161 return interfaces.length ? ' implements ' + interfaces.map(function (i) {
162 return i.name;
163 }).join(' & ') : '';
164}
165
166function printObject(type, options) {
167 return printDescription(options, type) + "type ".concat(type.name) + printImplementedInterfaces(type) + printFields(options, type);
168}
169
170function printInterface(type, options) {
171 return printDescription(options, type) + "interface ".concat(type.name) + printImplementedInterfaces(type) + printFields(options, type);
172}
173
174function 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
180function 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
187function 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
194function 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
201function printBlock(items) {
202 return items.length !== 0 ? ' {\n' + items.join('\n') + '\n}' : '';
203}
204
205function printArgs(options, args) {
206 var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
207
208 if (args.length === 0) {
209 return '';
210 } // If every arg does not have a description, print them on one line.
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
224function 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
235function printDirective(directive, options) {
236 return printDescription(options, directive) + 'directive @' + directive.name + printArgs(options, directive.args) + (directive.isRepeatable ? ' repeatable' : '') + ' on ' + directive.locations.join(' | ');
237}
238
239function 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
253function 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
264function 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
283function 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}