printer.js 11 KB

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