printSchema.mjs 7.3 KB

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