schemaPrinter.mjs 8.8 KB

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