printer.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { visit } from "./visitor.mjs";
  2. import { printBlockString } from "./blockString.mjs";
  3. /**
  4. * Converts an AST into a string, using one set of reasonable
  5. * formatting rules.
  6. */
  7. export function print(ast) {
  8. return visit(ast, {
  9. leave: printDocASTReducer
  10. });
  11. }
  12. var MAX_LINE_LENGTH = 80; // TODO: provide better type coverage in future
  13. var printDocASTReducer = {
  14. Name: function Name(node) {
  15. return node.value;
  16. },
  17. Variable: function Variable(node) {
  18. return '$' + node.name;
  19. },
  20. // Document
  21. Document: function Document(node) {
  22. return join(node.definitions, '\n\n') + '\n';
  23. },
  24. OperationDefinition: function OperationDefinition(node) {
  25. var op = node.operation;
  26. var name = node.name;
  27. var varDefs = wrap('(', join(node.variableDefinitions, ', '), ')');
  28. var directives = join(node.directives, ' ');
  29. var selectionSet = node.selectionSet; // Anonymous queries with no directives or variable definitions can use
  30. // the query short form.
  31. return !name && !directives && !varDefs && op === 'query' ? selectionSet : join([op, join([name, varDefs]), directives, selectionSet], ' ');
  32. },
  33. VariableDefinition: function VariableDefinition(_ref) {
  34. var variable = _ref.variable,
  35. type = _ref.type,
  36. defaultValue = _ref.defaultValue,
  37. directives = _ref.directives;
  38. return variable + ': ' + type + wrap(' = ', defaultValue) + wrap(' ', join(directives, ' '));
  39. },
  40. SelectionSet: function SelectionSet(_ref2) {
  41. var selections = _ref2.selections;
  42. return block(selections);
  43. },
  44. Field: function Field(_ref3) {
  45. var alias = _ref3.alias,
  46. name = _ref3.name,
  47. args = _ref3.arguments,
  48. directives = _ref3.directives,
  49. selectionSet = _ref3.selectionSet;
  50. var prefix = wrap('', alias, ': ') + name;
  51. var argsLine = prefix + wrap('(', join(args, ', '), ')');
  52. if (argsLine.length > MAX_LINE_LENGTH) {
  53. argsLine = prefix + wrap('(\n', indent(join(args, '\n')), '\n)');
  54. }
  55. return join([argsLine, join(directives, ' '), selectionSet], ' ');
  56. },
  57. Argument: function Argument(_ref4) {
  58. var name = _ref4.name,
  59. value = _ref4.value;
  60. return name + ': ' + value;
  61. },
  62. // Fragments
  63. FragmentSpread: function FragmentSpread(_ref5) {
  64. var name = _ref5.name,
  65. directives = _ref5.directives;
  66. return '...' + name + wrap(' ', join(directives, ' '));
  67. },
  68. InlineFragment: function InlineFragment(_ref6) {
  69. var typeCondition = _ref6.typeCondition,
  70. directives = _ref6.directives,
  71. selectionSet = _ref6.selectionSet;
  72. return join(['...', wrap('on ', typeCondition), join(directives, ' '), selectionSet], ' ');
  73. },
  74. FragmentDefinition: function FragmentDefinition(_ref7) {
  75. var name = _ref7.name,
  76. typeCondition = _ref7.typeCondition,
  77. variableDefinitions = _ref7.variableDefinitions,
  78. directives = _ref7.directives,
  79. selectionSet = _ref7.selectionSet;
  80. return (// Note: fragment variable definitions are experimental and may be changed
  81. // or removed in the future.
  82. "fragment ".concat(name).concat(wrap('(', join(variableDefinitions, ', '), ')'), " ") + "on ".concat(typeCondition, " ").concat(wrap('', join(directives, ' '), ' ')) + selectionSet
  83. );
  84. },
  85. // Value
  86. IntValue: function IntValue(_ref8) {
  87. var value = _ref8.value;
  88. return value;
  89. },
  90. FloatValue: function FloatValue(_ref9) {
  91. var value = _ref9.value;
  92. return value;
  93. },
  94. StringValue: function StringValue(_ref10, key) {
  95. var value = _ref10.value,
  96. isBlockString = _ref10.block;
  97. return isBlockString ? printBlockString(value, key === 'description' ? '' : ' ') : JSON.stringify(value);
  98. },
  99. BooleanValue: function BooleanValue(_ref11) {
  100. var value = _ref11.value;
  101. return value ? 'true' : 'false';
  102. },
  103. NullValue: function NullValue() {
  104. return 'null';
  105. },
  106. EnumValue: function EnumValue(_ref12) {
  107. var value = _ref12.value;
  108. return value;
  109. },
  110. ListValue: function ListValue(_ref13) {
  111. var values = _ref13.values;
  112. return '[' + join(values, ', ') + ']';
  113. },
  114. ObjectValue: function ObjectValue(_ref14) {
  115. var fields = _ref14.fields;
  116. return '{' + join(fields, ', ') + '}';
  117. },
  118. ObjectField: function ObjectField(_ref15) {
  119. var name = _ref15.name,
  120. value = _ref15.value;
  121. return name + ': ' + value;
  122. },
  123. // Directive
  124. Directive: function Directive(_ref16) {
  125. var name = _ref16.name,
  126. args = _ref16.arguments;
  127. return '@' + name + wrap('(', join(args, ', '), ')');
  128. },
  129. // Type
  130. NamedType: function NamedType(_ref17) {
  131. var name = _ref17.name;
  132. return name;
  133. },
  134. ListType: function ListType(_ref18) {
  135. var type = _ref18.type;
  136. return '[' + type + ']';
  137. },
  138. NonNullType: function NonNullType(_ref19) {
  139. var type = _ref19.type;
  140. return type + '!';
  141. },
  142. // Type System Definitions
  143. SchemaDefinition: addDescription(function (_ref20) {
  144. var directives = _ref20.directives,
  145. operationTypes = _ref20.operationTypes;
  146. return join(['schema', join(directives, ' '), block(operationTypes)], ' ');
  147. }),
  148. OperationTypeDefinition: function OperationTypeDefinition(_ref21) {
  149. var operation = _ref21.operation,
  150. type = _ref21.type;
  151. return operation + ': ' + type;
  152. },
  153. ScalarTypeDefinition: addDescription(function (_ref22) {
  154. var name = _ref22.name,
  155. directives = _ref22.directives;
  156. return join(['scalar', name, join(directives, ' ')], ' ');
  157. }),
  158. ObjectTypeDefinition: addDescription(function (_ref23) {
  159. var name = _ref23.name,
  160. interfaces = _ref23.interfaces,
  161. directives = _ref23.directives,
  162. fields = _ref23.fields;
  163. return join(['type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' ');
  164. }),
  165. FieldDefinition: addDescription(function (_ref24) {
  166. var name = _ref24.name,
  167. args = _ref24.arguments,
  168. type = _ref24.type,
  169. directives = _ref24.directives;
  170. return name + (hasMultilineItems(args) ? wrap('(\n', indent(join(args, '\n')), '\n)') : wrap('(', join(args, ', '), ')')) + ': ' + type + wrap(' ', join(directives, ' '));
  171. }),
  172. InputValueDefinition: addDescription(function (_ref25) {
  173. var name = _ref25.name,
  174. type = _ref25.type,
  175. defaultValue = _ref25.defaultValue,
  176. directives = _ref25.directives;
  177. return join([name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')], ' ');
  178. }),
  179. InterfaceTypeDefinition: addDescription(function (_ref26) {
  180. var name = _ref26.name,
  181. interfaces = _ref26.interfaces,
  182. directives = _ref26.directives,
  183. fields = _ref26.fields;
  184. return join(['interface', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' ');
  185. }),
  186. UnionTypeDefinition: addDescription(function (_ref27) {
  187. var name = _ref27.name,
  188. directives = _ref27.directives,
  189. types = _ref27.types;
  190. return join(['union', name, join(directives, ' '), types && types.length !== 0 ? '= ' + join(types, ' | ') : ''], ' ');
  191. }),
  192. EnumTypeDefinition: addDescription(function (_ref28) {
  193. var name = _ref28.name,
  194. directives = _ref28.directives,
  195. values = _ref28.values;
  196. return join(['enum', name, join(directives, ' '), block(values)], ' ');
  197. }),
  198. EnumValueDefinition: addDescription(function (_ref29) {
  199. var name = _ref29.name,
  200. directives = _ref29.directives;
  201. return join([name, join(directives, ' ')], ' ');
  202. }),
  203. InputObjectTypeDefinition: addDescription(function (_ref30) {
  204. var name = _ref30.name,
  205. directives = _ref30.directives,
  206. fields = _ref30.fields;
  207. return join(['input', name, join(directives, ' '), block(fields)], ' ');
  208. }),
  209. DirectiveDefinition: addDescription(function (_ref31) {
  210. var name = _ref31.name,
  211. args = _ref31.arguments,
  212. repeatable = _ref31.repeatable,
  213. locations = _ref31.locations;
  214. return 'directive @' + name + (hasMultilineItems(args) ? wrap('(\n', indent(join(args, '\n')), '\n)') : wrap('(', join(args, ', '), ')')) + (repeatable ? ' repeatable' : '') + ' on ' + join(locations, ' | ');
  215. }),
  216. SchemaExtension: function SchemaExtension(_ref32) {
  217. var directives = _ref32.directives,
  218. operationTypes = _ref32.operationTypes;
  219. return join(['extend schema', join(directives, ' '), block(operationTypes)], ' ');
  220. },
  221. ScalarTypeExtension: function ScalarTypeExtension(_ref33) {
  222. var name = _ref33.name,
  223. directives = _ref33.directives;
  224. return join(['extend scalar', name, join(directives, ' ')], ' ');
  225. },
  226. ObjectTypeExtension: function ObjectTypeExtension(_ref34) {
  227. var name = _ref34.name,
  228. interfaces = _ref34.interfaces,
  229. directives = _ref34.directives,
  230. fields = _ref34.fields;
  231. return join(['extend type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' ');
  232. },
  233. InterfaceTypeExtension: function InterfaceTypeExtension(_ref35) {
  234. var name = _ref35.name,
  235. interfaces = _ref35.interfaces,
  236. directives = _ref35.directives,
  237. fields = _ref35.fields;
  238. return join(['extend interface', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' ');
  239. },
  240. UnionTypeExtension: function UnionTypeExtension(_ref36) {
  241. var name = _ref36.name,
  242. directives = _ref36.directives,
  243. types = _ref36.types;
  244. return join(['extend union', name, join(directives, ' '), types && types.length !== 0 ? '= ' + join(types, ' | ') : ''], ' ');
  245. },
  246. EnumTypeExtension: function EnumTypeExtension(_ref37) {
  247. var name = _ref37.name,
  248. directives = _ref37.directives,
  249. values = _ref37.values;
  250. return join(['extend enum', name, join(directives, ' '), block(values)], ' ');
  251. },
  252. InputObjectTypeExtension: function InputObjectTypeExtension(_ref38) {
  253. var name = _ref38.name,
  254. directives = _ref38.directives,
  255. fields = _ref38.fields;
  256. return join(['extend input', name, join(directives, ' '), block(fields)], ' ');
  257. }
  258. };
  259. function addDescription(cb) {
  260. return function (node) {
  261. return join([node.description, cb(node)], '\n');
  262. };
  263. }
  264. /**
  265. * Given maybeArray, print an empty string if it is null or empty, otherwise
  266. * print all items together separated by separator if provided
  267. */
  268. function join(maybeArray) {
  269. var _maybeArray$filter$jo;
  270. var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  271. return (_maybeArray$filter$jo = maybeArray === null || maybeArray === void 0 ? void 0 : maybeArray.filter(function (x) {
  272. return x;
  273. }).join(separator)) !== null && _maybeArray$filter$jo !== void 0 ? _maybeArray$filter$jo : '';
  274. }
  275. /**
  276. * Given array, print each item on its own line, wrapped in an
  277. * indented "{ }" block.
  278. */
  279. function block(array) {
  280. return wrap('{\n', indent(join(array, '\n')), '\n}');
  281. }
  282. /**
  283. * If maybeString is not null or empty, then wrap with start and end, otherwise print an empty string.
  284. */
  285. function wrap(start, maybeString) {
  286. var end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  287. return maybeString != null && maybeString !== '' ? start + maybeString + end : '';
  288. }
  289. function indent(str) {
  290. return wrap(' ', str.replace(/\n/g, '\n '));
  291. }
  292. function isMultiline(str) {
  293. return str.indexOf('\n') !== -1;
  294. }
  295. function hasMultilineItems(maybeArray) {
  296. return maybeArray != null && maybeArray.some(isMultiline);
  297. }