schemaPrinter.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.printSchema = printSchema;
  6. exports.printIntrospectionSchema = printIntrospectionSchema;
  7. exports.printType = printType;
  8. var _flatMap = _interopRequireDefault(require("../polyfills/flatMap"));
  9. var _objectValues = _interopRequireDefault(require("../polyfills/objectValues"));
  10. var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
  11. var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
  12. var _printer = require("../language/printer");
  13. var _blockString = require("../language/blockString");
  14. var _introspection = require("../type/introspection");
  15. var _scalars = require("../type/scalars");
  16. var _directives = require("../type/directives");
  17. var _definition = require("../type/definition");
  18. var _astFromValue = require("../utilities/astFromValue");
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20. /**
  21. * Accepts options as a second argument:
  22. *
  23. * - commentDescriptions:
  24. * Provide true to use preceding comments as the description.
  25. *
  26. */
  27. function printSchema(schema, options) {
  28. return printFilteredSchema(schema, function (n) {
  29. return !(0, _directives.isSpecifiedDirective)(n);
  30. }, isDefinedType, options);
  31. }
  32. function printIntrospectionSchema(schema, options) {
  33. return printFilteredSchema(schema, _directives.isSpecifiedDirective, _introspection.isIntrospectionType, options);
  34. }
  35. function isDefinedType(type) {
  36. return !(0, _scalars.isSpecifiedScalarType)(type) && !(0, _introspection.isIntrospectionType)(type);
  37. }
  38. function printFilteredSchema(schema, directiveFilter, typeFilter, options) {
  39. var directives = schema.getDirectives().filter(directiveFilter);
  40. var typeMap = schema.getTypeMap();
  41. var types = (0, _objectValues.default)(typeMap).sort(function (type1, type2) {
  42. return type1.name.localeCompare(type2.name);
  43. }).filter(typeFilter);
  44. return [printSchemaDefinition(schema)].concat(directives.map(function (directive) {
  45. return printDirective(directive, options);
  46. }), types.map(function (type) {
  47. return printType(type, options);
  48. })).filter(Boolean).join('\n\n') + '\n';
  49. }
  50. function printSchemaDefinition(schema) {
  51. if (isSchemaOfCommonNames(schema)) {
  52. return;
  53. }
  54. var operationTypes = [];
  55. var queryType = schema.getQueryType();
  56. if (queryType) {
  57. operationTypes.push(" query: ".concat(queryType.name));
  58. }
  59. var mutationType = schema.getMutationType();
  60. if (mutationType) {
  61. operationTypes.push(" mutation: ".concat(mutationType.name));
  62. }
  63. var subscriptionType = schema.getSubscriptionType();
  64. if (subscriptionType) {
  65. operationTypes.push(" subscription: ".concat(subscriptionType.name));
  66. }
  67. return "schema {\n".concat(operationTypes.join('\n'), "\n}");
  68. }
  69. /**
  70. * GraphQL schema define root types for each type of operation. These types are
  71. * the same as any other type and can be named in any manner, however there is
  72. * a common naming convention:
  73. *
  74. * schema {
  75. * query: Query
  76. * mutation: Mutation
  77. * }
  78. *
  79. * When using this naming convention, the schema description can be omitted.
  80. */
  81. function isSchemaOfCommonNames(schema) {
  82. var queryType = schema.getQueryType();
  83. if (queryType && queryType.name !== 'Query') {
  84. return false;
  85. }
  86. var mutationType = schema.getMutationType();
  87. if (mutationType && mutationType.name !== 'Mutation') {
  88. return false;
  89. }
  90. var subscriptionType = schema.getSubscriptionType();
  91. if (subscriptionType && subscriptionType.name !== 'Subscription') {
  92. return false;
  93. }
  94. return true;
  95. }
  96. function printType(type, options) {
  97. if ((0, _definition.isScalarType)(type)) {
  98. return printScalar(type, options);
  99. } else if ((0, _definition.isObjectType)(type)) {
  100. return printObject(type, options);
  101. } else if ((0, _definition.isInterfaceType)(type)) {
  102. return printInterface(type, options);
  103. } else if ((0, _definition.isUnionType)(type)) {
  104. return printUnion(type, options);
  105. } else if ((0, _definition.isEnumType)(type)) {
  106. return printEnum(type, options);
  107. } else if ((0, _definition.isInputObjectType)(type)) {
  108. return printInputObject(type, options);
  109. } // Not reachable. All possible types have been considered.
  110. /* istanbul ignore next */
  111. (0, _invariant.default)(false, 'Unexpected type: ' + (0, _inspect.default)(type));
  112. }
  113. function printScalar(type, options) {
  114. return printDescription(options, type) + "scalar ".concat(type.name);
  115. }
  116. function printObject(type, options) {
  117. var interfaces = type.getInterfaces();
  118. var implementedInterfaces = interfaces.length ? ' implements ' + interfaces.map(function (i) {
  119. return i.name;
  120. }).join(' & ') : '';
  121. return printDescription(options, type) + "type ".concat(type.name).concat(implementedInterfaces) + printFields(options, type);
  122. }
  123. function printInterface(type, options) {
  124. return printDescription(options, type) + "interface ".concat(type.name) + printFields(options, type);
  125. }
  126. function printUnion(type, options) {
  127. var types = type.getTypes();
  128. var possibleTypes = types.length ? ' = ' + types.join(' | ') : '';
  129. return printDescription(options, type) + 'union ' + type.name + possibleTypes;
  130. }
  131. function printEnum(type, options) {
  132. var values = type.getValues().map(function (value, i) {
  133. return printDescription(options, value, ' ', !i) + ' ' + value.name + printDeprecated(value);
  134. });
  135. return printDescription(options, type) + "enum ".concat(type.name) + printBlock(values);
  136. }
  137. function printInputObject(type, options) {
  138. var fields = (0, _objectValues.default)(type.getFields()).map(function (f, i) {
  139. return printDescription(options, f, ' ', !i) + ' ' + printInputValue(f);
  140. });
  141. return printDescription(options, type) + "input ".concat(type.name) + printBlock(fields);
  142. }
  143. function printFields(options, type) {
  144. var fields = (0, _objectValues.default)(type.getFields()).map(function (f, i) {
  145. return printDescription(options, f, ' ', !i) + ' ' + f.name + printArgs(options, f.args, ' ') + ': ' + String(f.type) + printDeprecated(f);
  146. });
  147. return printBlock(fields);
  148. }
  149. function printBlock(items) {
  150. return items.length !== 0 ? ' {\n' + items.join('\n') + '\n}' : '';
  151. }
  152. function printArgs(options, args) {
  153. var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  154. if (args.length === 0) {
  155. return '';
  156. } // If every arg does not have a description, print them on one line.
  157. if (args.every(function (arg) {
  158. return !arg.description;
  159. })) {
  160. return '(' + args.map(printInputValue).join(', ') + ')';
  161. }
  162. return '(\n' + args.map(function (arg, i) {
  163. return printDescription(options, arg, ' ' + indentation, !i) + ' ' + indentation + printInputValue(arg);
  164. }).join('\n') + '\n' + indentation + ')';
  165. }
  166. function printInputValue(arg) {
  167. var defaultAST = (0, _astFromValue.astFromValue)(arg.defaultValue, arg.type);
  168. var argDecl = arg.name + ': ' + String(arg.type);
  169. if (defaultAST) {
  170. argDecl += " = ".concat((0, _printer.print)(defaultAST));
  171. }
  172. return argDecl;
  173. }
  174. function printDirective(directive, options) {
  175. return printDescription(options, directive) + 'directive @' + directive.name + printArgs(options, directive.args) + (directive.isRepeatable ? ' repeatable' : '') + ' on ' + directive.locations.join(' | ');
  176. }
  177. function printDeprecated(fieldOrEnumVal) {
  178. if (!fieldOrEnumVal.isDeprecated) {
  179. return '';
  180. }
  181. var reason = fieldOrEnumVal.deprecationReason;
  182. var reasonAST = (0, _astFromValue.astFromValue)(reason, _scalars.GraphQLString);
  183. if (reasonAST && reason !== '' && reason !== _directives.DEFAULT_DEPRECATION_REASON) {
  184. return ' @deprecated(reason: ' + (0, _printer.print)(reasonAST) + ')';
  185. }
  186. return ' @deprecated';
  187. }
  188. function printDescription(options, def) {
  189. var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  190. var firstInBlock = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
  191. if (!def.description) {
  192. return '';
  193. }
  194. var lines = descriptionLines(def.description, 120 - indentation.length);
  195. if (options && options.commentDescriptions) {
  196. return printDescriptionWithComments(lines, indentation, firstInBlock);
  197. }
  198. var text = lines.join('\n');
  199. var preferMultipleLines = text.length > 70;
  200. var blockString = (0, _blockString.printBlockString)(text, '', preferMultipleLines);
  201. var prefix = indentation && !firstInBlock ? '\n' + indentation : indentation;
  202. return prefix + blockString.replace(/\n/g, '\n' + indentation) + '\n';
  203. }
  204. function printDescriptionWithComments(lines, indentation, firstInBlock) {
  205. var description = indentation && !firstInBlock ? '\n' : '';
  206. for (var _i2 = 0; _i2 < lines.length; _i2++) {
  207. var line = lines[_i2];
  208. if (line === '') {
  209. description += indentation + '#\n';
  210. } else {
  211. description += indentation + '# ' + line + '\n';
  212. }
  213. }
  214. return description;
  215. }
  216. function descriptionLines(description, maxLen) {
  217. var rawLines = description.split('\n');
  218. return (0, _flatMap.default)(rawLines, function (line) {
  219. if (line.length < maxLen + 5) {
  220. return line;
  221. } // For > 120 character long lines, cut at space boundaries into sublines
  222. // of ~80 chars.
  223. return breakLine(line, maxLen);
  224. });
  225. }
  226. function breakLine(line, maxLen) {
  227. var parts = line.split(new RegExp("((?: |^).{15,".concat(maxLen - 40, "}(?= |$))")));
  228. if (parts.length < 4) {
  229. return [line];
  230. }
  231. var sublines = [parts[0] + parts[1] + parts[2]];
  232. for (var i = 3; i < parts.length; i += 2) {
  233. sublines.push(parts[i].slice(1) + parts[i + 1]);
  234. }
  235. return sublines;
  236. }