printer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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: function SchemaDefinition(_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. directives = _ref26.directives,
  181. fields = _ref26.fields;
  182. return join(['interface', name, join(directives, ' '), block(fields)], ' ');
  183. }),
  184. UnionTypeDefinition: addDescription(function (_ref27) {
  185. var name = _ref27.name,
  186. directives = _ref27.directives,
  187. types = _ref27.types;
  188. return join(['union', name, join(directives, ' '), types && types.length !== 0 ? '= ' + join(types, ' | ') : ''], ' ');
  189. }),
  190. EnumTypeDefinition: addDescription(function (_ref28) {
  191. var name = _ref28.name,
  192. directives = _ref28.directives,
  193. values = _ref28.values;
  194. return join(['enum', name, join(directives, ' '), block(values)], ' ');
  195. }),
  196. EnumValueDefinition: addDescription(function (_ref29) {
  197. var name = _ref29.name,
  198. directives = _ref29.directives;
  199. return join([name, join(directives, ' ')], ' ');
  200. }),
  201. InputObjectTypeDefinition: addDescription(function (_ref30) {
  202. var name = _ref30.name,
  203. directives = _ref30.directives,
  204. fields = _ref30.fields;
  205. return join(['input', name, join(directives, ' '), block(fields)], ' ');
  206. }),
  207. DirectiveDefinition: addDescription(function (_ref31) {
  208. var name = _ref31.name,
  209. args = _ref31.arguments,
  210. repeatable = _ref31.repeatable,
  211. locations = _ref31.locations;
  212. return 'directive @' + name + (hasMultilineItems(args) ? wrap('(\n', indent(join(args, '\n')), '\n)') : wrap('(', join(args, ', '), ')')) + (repeatable ? ' repeatable' : '') + ' on ' + join(locations, ' | ');
  213. }),
  214. SchemaExtension: function SchemaExtension(_ref32) {
  215. var directives = _ref32.directives,
  216. operationTypes = _ref32.operationTypes;
  217. return join(['extend schema', join(directives, ' '), block(operationTypes)], ' ');
  218. },
  219. ScalarTypeExtension: function ScalarTypeExtension(_ref33) {
  220. var name = _ref33.name,
  221. directives = _ref33.directives;
  222. return join(['extend scalar', name, join(directives, ' ')], ' ');
  223. },
  224. ObjectTypeExtension: function ObjectTypeExtension(_ref34) {
  225. var name = _ref34.name,
  226. interfaces = _ref34.interfaces,
  227. directives = _ref34.directives,
  228. fields = _ref34.fields;
  229. return join(['extend type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' ');
  230. },
  231. InterfaceTypeExtension: function InterfaceTypeExtension(_ref35) {
  232. var name = _ref35.name,
  233. directives = _ref35.directives,
  234. fields = _ref35.fields;
  235. return join(['extend interface', name, join(directives, ' '), block(fields)], ' ');
  236. },
  237. UnionTypeExtension: function UnionTypeExtension(_ref36) {
  238. var name = _ref36.name,
  239. directives = _ref36.directives,
  240. types = _ref36.types;
  241. return join(['extend union', name, join(directives, ' '), types && types.length !== 0 ? '= ' + join(types, ' | ') : ''], ' ');
  242. },
  243. EnumTypeExtension: function EnumTypeExtension(_ref37) {
  244. var name = _ref37.name,
  245. directives = _ref37.directives,
  246. values = _ref37.values;
  247. return join(['extend enum', name, join(directives, ' '), block(values)], ' ');
  248. },
  249. InputObjectTypeExtension: function InputObjectTypeExtension(_ref38) {
  250. var name = _ref38.name,
  251. directives = _ref38.directives,
  252. fields = _ref38.fields;
  253. return join(['extend input', name, join(directives, ' '), block(fields)], ' ');
  254. }
  255. };
  256. function addDescription(cb) {
  257. return function (node) {
  258. return join([node.description, cb(node)], '\n');
  259. };
  260. }
  261. /**
  262. * Given maybeArray, print an empty string if it is null or empty, otherwise
  263. * print all items together separated by separator if provided
  264. */
  265. function join(maybeArray, separator) {
  266. return maybeArray ? maybeArray.filter(function (x) {
  267. return x;
  268. }).join(separator || '') : '';
  269. }
  270. /**
  271. * Given array, print each item on its own line, wrapped in an
  272. * indented "{ }" block.
  273. */
  274. function block(array) {
  275. return array && array.length !== 0 ? '{\n' + indent(join(array, '\n')) + '\n}' : '';
  276. }
  277. /**
  278. * If maybeString is not null or empty, then wrap with start and end, otherwise
  279. * print an empty string.
  280. */
  281. function wrap(start, maybeString, end) {
  282. return maybeString ? start + maybeString + (end || '') : '';
  283. }
  284. function indent(maybeString) {
  285. return maybeString && ' ' + maybeString.replace(/\n/g, '\n ');
  286. }
  287. function isMultiline(string) {
  288. return string.indexOf('\n') !== -1;
  289. }
  290. function hasMultilineItems(maybeArray) {
  291. return maybeArray && maybeArray.some(isMultiline);
  292. }