printSchema.js 9.1 KB

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