printSchema.mjs 8.5 KB

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