printSchema.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.printIntrospectionSchema = printIntrospectionSchema;
  6. exports.printSchema = printSchema;
  7. exports.printType = printType;
  8. var _inspect = require('../jsutils/inspect.js');
  9. var _invariant = require('../jsutils/invariant.js');
  10. var _blockString = require('../language/blockString.js');
  11. var _kinds = require('../language/kinds.js');
  12. var _printer = require('../language/printer.js');
  13. var _definition = require('../type/definition.js');
  14. var _directives = require('../type/directives.js');
  15. var _introspection = require('../type/introspection.js');
  16. var _scalars = require('../type/scalars.js');
  17. var _astFromValue = require('./astFromValue.js');
  18. function printSchema(schema) {
  19. return printFilteredSchema(
  20. schema,
  21. (n) => !(0, _directives.isSpecifiedDirective)(n),
  22. isDefinedType,
  23. );
  24. }
  25. function printIntrospectionSchema(schema) {
  26. return printFilteredSchema(
  27. schema,
  28. _directives.isSpecifiedDirective,
  29. _introspection.isIntrospectionType,
  30. );
  31. }
  32. function isDefinedType(type) {
  33. return (
  34. !(0, _scalars.isSpecifiedScalarType)(type) &&
  35. !(0, _introspection.isIntrospectionType)(type)
  36. );
  37. }
  38. function printFilteredSchema(schema, directiveFilter, typeFilter) {
  39. const directives = schema.getDirectives().filter(directiveFilter);
  40. const types = Object.values(schema.getTypeMap()).filter(typeFilter);
  41. return [
  42. printSchemaDefinition(schema),
  43. ...directives.map((directive) => printDirective(directive)),
  44. ...types.map((type) => printType(type)),
  45. ]
  46. .filter(Boolean)
  47. .join('\n\n');
  48. }
  49. function printSchemaDefinition(schema) {
  50. if (schema.description == null && isSchemaOfCommonNames(schema)) {
  51. return;
  52. }
  53. const operationTypes = [];
  54. const queryType = schema.getQueryType();
  55. if (queryType) {
  56. operationTypes.push(` query: ${queryType.name}`);
  57. }
  58. const mutationType = schema.getMutationType();
  59. if (mutationType) {
  60. operationTypes.push(` mutation: ${mutationType.name}`);
  61. }
  62. const subscriptionType = schema.getSubscriptionType();
  63. if (subscriptionType) {
  64. operationTypes.push(` subscription: ${subscriptionType.name}`);
  65. }
  66. return printDescription(schema) + `schema {\n${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. * ```graphql
  74. * schema {
  75. * query: Query
  76. * mutation: Mutation
  77. * subscription: Subscription
  78. * }
  79. * ```
  80. *
  81. * When using this naming convention, the schema description can be omitted.
  82. */
  83. function isSchemaOfCommonNames(schema) {
  84. const queryType = schema.getQueryType();
  85. if (queryType && queryType.name !== 'Query') {
  86. return false;
  87. }
  88. const mutationType = schema.getMutationType();
  89. if (mutationType && mutationType.name !== 'Mutation') {
  90. return false;
  91. }
  92. const subscriptionType = schema.getSubscriptionType();
  93. if (subscriptionType && subscriptionType.name !== 'Subscription') {
  94. return false;
  95. }
  96. return true;
  97. }
  98. function printType(type) {
  99. if ((0, _definition.isScalarType)(type)) {
  100. return printScalar(type);
  101. }
  102. if ((0, _definition.isObjectType)(type)) {
  103. return printObject(type);
  104. }
  105. if ((0, _definition.isInterfaceType)(type)) {
  106. return printInterface(type);
  107. }
  108. if ((0, _definition.isUnionType)(type)) {
  109. return printUnion(type);
  110. }
  111. if ((0, _definition.isEnumType)(type)) {
  112. return printEnum(type);
  113. }
  114. if ((0, _definition.isInputObjectType)(type)) {
  115. return printInputObject(type);
  116. }
  117. /* c8 ignore next 3 */
  118. // Not reachable, all possible types have been considered.
  119. false ||
  120. (0, _invariant.invariant)(
  121. false,
  122. 'Unexpected type: ' + (0, _inspect.inspect)(type),
  123. );
  124. }
  125. function printScalar(type) {
  126. return (
  127. printDescription(type) + `scalar ${type.name}` + printSpecifiedByURL(type)
  128. );
  129. }
  130. function printImplementedInterfaces(type) {
  131. const interfaces = type.getInterfaces();
  132. return interfaces.length
  133. ? ' implements ' + interfaces.map((i) => i.name).join(' & ')
  134. : '';
  135. }
  136. function printObject(type) {
  137. return (
  138. printDescription(type) +
  139. `type ${type.name}` +
  140. printImplementedInterfaces(type) +
  141. printFields(type)
  142. );
  143. }
  144. function printInterface(type) {
  145. return (
  146. printDescription(type) +
  147. `interface ${type.name}` +
  148. printImplementedInterfaces(type) +
  149. printFields(type)
  150. );
  151. }
  152. function printUnion(type) {
  153. const types = type.getTypes();
  154. const possibleTypes = types.length ? ' = ' + types.join(' | ') : '';
  155. return printDescription(type) + 'union ' + type.name + possibleTypes;
  156. }
  157. function printEnum(type) {
  158. const values = type
  159. .getValues()
  160. .map(
  161. (value, i) =>
  162. printDescription(value, ' ', !i) +
  163. ' ' +
  164. value.name +
  165. printDeprecated(value.deprecationReason),
  166. );
  167. return printDescription(type) + `enum ${type.name}` + printBlock(values);
  168. }
  169. function printInputObject(type) {
  170. const fields = Object.values(type.getFields()).map(
  171. (f, i) => printDescription(f, ' ', !i) + ' ' + printInputValue(f),
  172. );
  173. return printDescription(type) + `input ${type.name}` + printBlock(fields);
  174. }
  175. function printFields(type) {
  176. const fields = Object.values(type.getFields()).map(
  177. (f, i) =>
  178. printDescription(f, ' ', !i) +
  179. ' ' +
  180. f.name +
  181. printArgs(f.args, ' ') +
  182. ': ' +
  183. String(f.type) +
  184. printDeprecated(f.deprecationReason),
  185. );
  186. return printBlock(fields);
  187. }
  188. function printBlock(items) {
  189. return items.length !== 0 ? ' {\n' + items.join('\n') + '\n}' : '';
  190. }
  191. function printArgs(args, indentation = '') {
  192. if (args.length === 0) {
  193. return '';
  194. } // If every arg does not have a description, print them on one line.
  195. if (args.every((arg) => !arg.description)) {
  196. return '(' + args.map(printInputValue).join(', ') + ')';
  197. }
  198. return (
  199. '(\n' +
  200. args
  201. .map(
  202. (arg, i) =>
  203. printDescription(arg, ' ' + indentation, !i) +
  204. ' ' +
  205. indentation +
  206. printInputValue(arg),
  207. )
  208. .join('\n') +
  209. '\n' +
  210. indentation +
  211. ')'
  212. );
  213. }
  214. function printInputValue(arg) {
  215. const defaultAST = (0, _astFromValue.astFromValue)(
  216. arg.defaultValue,
  217. arg.type,
  218. );
  219. let argDecl = arg.name + ': ' + String(arg.type);
  220. if (defaultAST) {
  221. argDecl += ` = ${(0, _printer.print)(defaultAST)}`;
  222. }
  223. return argDecl + printDeprecated(arg.deprecationReason);
  224. }
  225. function printDirective(directive) {
  226. return (
  227. printDescription(directive) +
  228. 'directive @' +
  229. directive.name +
  230. printArgs(directive.args) +
  231. (directive.isRepeatable ? ' repeatable' : '') +
  232. ' on ' +
  233. directive.locations.join(' | ')
  234. );
  235. }
  236. function printDeprecated(reason) {
  237. if (reason == null) {
  238. return '';
  239. }
  240. if (reason !== _directives.DEFAULT_DEPRECATION_REASON) {
  241. const astValue = (0, _printer.print)({
  242. kind: _kinds.Kind.STRING,
  243. value: reason,
  244. });
  245. return ` @deprecated(reason: ${astValue})`;
  246. }
  247. return ' @deprecated';
  248. }
  249. function printSpecifiedByURL(scalar) {
  250. if (scalar.specifiedByURL == null) {
  251. return '';
  252. }
  253. const astValue = (0, _printer.print)({
  254. kind: _kinds.Kind.STRING,
  255. value: scalar.specifiedByURL,
  256. });
  257. return ` @specifiedBy(url: ${astValue})`;
  258. }
  259. function printDescription(def, indentation = '', firstInBlock = true) {
  260. const { description } = def;
  261. if (description == null) {
  262. return '';
  263. }
  264. const blockString = (0, _printer.print)({
  265. kind: _kinds.Kind.STRING,
  266. value: description,
  267. block: (0, _blockString.isPrintableAsBlockString)(description),
  268. });
  269. const prefix =
  270. indentation && !firstInBlock ? '\n' + indentation : indentation;
  271. return prefix + blockString.replace(/\n/g, '\n' + indentation) + '\n';
  272. }