UNPKG

8.71 kBJavaScriptView Raw
1import objectValues from "../polyfills/objectValues.mjs";
2import inspect from "../jsutils/inspect.mjs";
3import invariant from "../jsutils/invariant.mjs";
4import { print } from "../language/printer.mjs";
5import { printBlockString } from "../language/blockString.mjs";
6import { isIntrospectionType } from "../type/introspection.mjs";
7import { GraphQLString, isSpecifiedScalarType } from "../type/scalars.mjs";
8import { DEFAULT_DEPRECATION_REASON, isSpecifiedDirective } from "../type/directives.mjs";
9import { isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType } from "../type/definition.mjs";
10import { astFromValue } from "./astFromValue.mjs";
11
12/**
13 * Accepts options as a second argument:
14 *
15 * - commentDescriptions:
16 * Provide true to use preceding comments as the description.
17 *
18 */
19export function printSchema(schema, options) {
20 return printFilteredSchema(schema, function (n) {
21 return !isSpecifiedDirective(n);
22 }, isDefinedType, options);
23}
24export function printIntrospectionSchema(schema, options) {
25 return printFilteredSchema(schema, isSpecifiedDirective, isIntrospectionType, options);
26}
27
28function isDefinedType(type) {
29 return !isSpecifiedScalarType(type) && !isIntrospectionType(type);
30}
31
32function printFilteredSchema(schema, directiveFilter, typeFilter, options) {
33 var directives = schema.getDirectives().filter(directiveFilter);
34 var types = objectValues(schema.getTypeMap()).filter(typeFilter);
35 return [printSchemaDefinition(schema)].concat(directives.map(function (directive) {
36 return printDirective(directive, options);
37 }), types.map(function (type) {
38 return printType(type, options);
39 })).filter(Boolean).join('\n\n') + '\n';
40}
41
42function printSchemaDefinition(schema) {
43 if (schema.description == null && isSchemaOfCommonNames(schema)) {
44 return;
45 }
46
47 var operationTypes = [];
48 var queryType = schema.getQueryType();
49
50 if (queryType) {
51 operationTypes.push(" query: ".concat(queryType.name));
52 }
53
54 var mutationType = schema.getMutationType();
55
56 if (mutationType) {
57 operationTypes.push(" mutation: ".concat(mutationType.name));
58 }
59
60 var subscriptionType = schema.getSubscriptionType();
61
62 if (subscriptionType) {
63 operationTypes.push(" subscription: ".concat(subscriptionType.name));
64 }
65
66 return printDescription({}, schema) + "schema {\n".concat(operationTypes.join('\n'), "\n}");
67}
68/**
69 * GraphQL schema define root types for each type of operation. These types are
70 * the same as any other type and can be named in any manner, however there is
71 * a common naming convention:
72 *
73 * schema {
74 * query: Query
75 * mutation: Mutation
76 * }
77 *
78 * When using this naming convention, the schema description can be omitted.
79 */
80
81
82function isSchemaOfCommonNames(schema) {
83 var queryType = schema.getQueryType();
84
85 if (queryType && queryType.name !== 'Query') {
86 return false;
87 }
88
89 var mutationType = schema.getMutationType();
90
91 if (mutationType && mutationType.name !== 'Mutation') {
92 return false;
93 }
94
95 var subscriptionType = schema.getSubscriptionType();
96
97 if (subscriptionType && subscriptionType.name !== 'Subscription') {
98 return false;
99 }
100
101 return true;
102}
103
104export function printType(type, options) {
105 if (isScalarType(type)) {
106 return printScalar(type, options);
107 }
108
109 if (isObjectType(type)) {
110 return printObject(type, options);
111 }
112
113 if (isInterfaceType(type)) {
114 return printInterface(type, options);
115 }
116
117 if (isUnionType(type)) {
118 return printUnion(type, options);
119 }
120
121 if (isEnumType(type)) {
122 return printEnum(type, options);
123 } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
124
125
126 if (isInputObjectType(type)) {
127 return printInputObject(type, options);
128 } // istanbul ignore next (Not reachable. All possible types have been considered)
129
130
131 false || invariant(0, 'Unexpected type: ' + inspect(type));
132}
133
134function printScalar(type, options) {
135 return printDescription(options, type) + "scalar ".concat(type.name) + printSpecifiedByUrl(type);
136}
137
138function printImplementedInterfaces(type) {
139 var interfaces = type.getInterfaces();
140 return interfaces.length ? ' implements ' + interfaces.map(function (i) {
141 return i.name;
142 }).join(' & ') : '';
143}
144
145function printObject(type, options) {
146 return printDescription(options, type) + "type ".concat(type.name) + printImplementedInterfaces(type) + printFields(options, type);
147}
148
149function printInterface(type, options) {
150 return printDescription(options, type) + "interface ".concat(type.name) + printImplementedInterfaces(type) + printFields(options, type);
151}
152
153function printUnion(type, options) {
154 var types = type.getTypes();
155 var possibleTypes = types.length ? ' = ' + types.join(' | ') : '';
156 return printDescription(options, type) + 'union ' + type.name + possibleTypes;
157}
158
159function printEnum(type, options) {
160 var values = type.getValues().map(function (value, i) {
161 return printDescription(options, value, ' ', !i) + ' ' + value.name + printDeprecated(value.deprecationReason);
162 });
163 return printDescription(options, type) + "enum ".concat(type.name) + printBlock(values);
164}
165
166function printInputObject(type, options) {
167 var fields = objectValues(type.getFields()).map(function (f, i) {
168 return printDescription(options, f, ' ', !i) + ' ' + printInputValue(f);
169 });
170 return printDescription(options, type) + "input ".concat(type.name) + printBlock(fields);
171}
172
173function printFields(options, type) {
174 var fields = objectValues(type.getFields()).map(function (f, i) {
175 return printDescription(options, f, ' ', !i) + ' ' + f.name + printArgs(options, f.args, ' ') + ': ' + String(f.type) + printDeprecated(f.deprecationReason);
176 });
177 return printBlock(fields);
178}
179
180function printBlock(items) {
181 return items.length !== 0 ? ' {\n' + items.join('\n') + '\n}' : '';
182}
183
184function printArgs(options, args) {
185 var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
186
187 if (args.length === 0) {
188 return '';
189 } // If every arg does not have a description, print them on one line.
190
191
192 if (args.every(function (arg) {
193 return !arg.description;
194 })) {
195 return '(' + args.map(printInputValue).join(', ') + ')';
196 }
197
198 return '(\n' + args.map(function (arg, i) {
199 return printDescription(options, arg, ' ' + indentation, !i) + ' ' + indentation + printInputValue(arg);
200 }).join('\n') + '\n' + indentation + ')';
201}
202
203function printInputValue(arg) {
204 var defaultAST = astFromValue(arg.defaultValue, arg.type);
205 var argDecl = arg.name + ': ' + String(arg.type);
206
207 if (defaultAST) {
208 argDecl += " = ".concat(print(defaultAST));
209 }
210
211 return argDecl + printDeprecated(arg.deprecationReason);
212}
213
214function printDirective(directive, options) {
215 return printDescription(options, directive) + 'directive @' + directive.name + printArgs(options, directive.args) + (directive.isRepeatable ? ' repeatable' : '') + ' on ' + directive.locations.join(' | ');
216}
217
218function printDeprecated(reason) {
219 if (reason == null) {
220 return '';
221 }
222
223 var reasonAST = astFromValue(reason, GraphQLString);
224
225 if (reasonAST && reason !== DEFAULT_DEPRECATION_REASON) {
226 return ' @deprecated(reason: ' + print(reasonAST) + ')';
227 }
228
229 return ' @deprecated';
230}
231
232function printSpecifiedByUrl(scalar) {
233 if (scalar.specifiedByUrl == null) {
234 return '';
235 }
236
237 var url = scalar.specifiedByUrl;
238 var urlAST = astFromValue(url, GraphQLString);
239 urlAST || invariant(0, 'Unexpected null value returned from `astFromValue` for specifiedByUrl');
240 return ' @specifiedBy(url: ' + print(urlAST) + ')';
241}
242
243function printDescription(options, def) {
244 var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
245 var firstInBlock = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
246 var description = def.description;
247
248 if (description == null) {
249 return '';
250 }
251
252 if ((options === null || options === void 0 ? void 0 : options.commentDescriptions) === true) {
253 return printDescriptionWithComments(description, indentation, firstInBlock);
254 }
255
256 var preferMultipleLines = description.length > 70;
257 var blockString = printBlockString(description, '', preferMultipleLines);
258 var prefix = indentation && !firstInBlock ? '\n' + indentation : indentation;
259 return prefix + blockString.replace(/\n/g, '\n' + indentation) + '\n';
260}
261
262function printDescriptionWithComments(description, indentation, firstInBlock) {
263 var prefix = indentation && !firstInBlock ? '\n' : '';
264 var comment = description.split('\n').map(function (line) {
265 return indentation + (line !== '' ? '# ' + line : '#');
266 }).join('\n');
267 return prefix + comment + '\n';
268}