transforms.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.printWithReducedWhitespace = exports.removeAliases = exports.sortAST = exports.dropUnusedDefinitions = exports.hideStringAndNumericLiterals = exports.hideLiterals = void 0;
  7. const visitor_1 = require("graphql/language/visitor");
  8. const printer_1 = require("graphql/language/printer");
  9. const utilities_1 = require("graphql/utilities");
  10. const lodash_sortby_1 = __importDefault(require("lodash.sortby"));
  11. function hideLiterals(ast) {
  12. return (0, visitor_1.visit)(ast, {
  13. IntValue(node) {
  14. return Object.assign(Object.assign({}, node), { value: "0" });
  15. },
  16. FloatValue(node) {
  17. return Object.assign(Object.assign({}, node), { value: "0" });
  18. },
  19. StringValue(node) {
  20. return Object.assign(Object.assign({}, node), { value: "", block: false });
  21. },
  22. ListValue(node) {
  23. return Object.assign(Object.assign({}, node), { values: [] });
  24. },
  25. ObjectValue(node) {
  26. return Object.assign(Object.assign({}, node), { fields: [] });
  27. },
  28. });
  29. }
  30. exports.hideLiterals = hideLiterals;
  31. function hideStringAndNumericLiterals(ast) {
  32. return (0, visitor_1.visit)(ast, {
  33. IntValue(node) {
  34. return Object.assign(Object.assign({}, node), { value: "0" });
  35. },
  36. FloatValue(node) {
  37. return Object.assign(Object.assign({}, node), { value: "0" });
  38. },
  39. StringValue(node) {
  40. return Object.assign(Object.assign({}, node), { value: "", block: false });
  41. },
  42. });
  43. }
  44. exports.hideStringAndNumericLiterals = hideStringAndNumericLiterals;
  45. function dropUnusedDefinitions(ast, operationName) {
  46. const separated = (0, utilities_1.separateOperations)(ast)[operationName];
  47. if (!separated) {
  48. return ast;
  49. }
  50. return separated;
  51. }
  52. exports.dropUnusedDefinitions = dropUnusedDefinitions;
  53. function sorted(items, ...iteratees) {
  54. if (items) {
  55. return (0, lodash_sortby_1.default)(items, ...iteratees);
  56. }
  57. return undefined;
  58. }
  59. function sortAST(ast) {
  60. return (0, visitor_1.visit)(ast, {
  61. Document(node) {
  62. return Object.assign(Object.assign({}, node), { definitions: (0, lodash_sortby_1.default)(node.definitions, "kind", "name.value") });
  63. },
  64. OperationDefinition(node) {
  65. return Object.assign(Object.assign({}, node), { variableDefinitions: sorted(node.variableDefinitions, "variable.name.value") });
  66. },
  67. SelectionSet(node) {
  68. return Object.assign(Object.assign({}, node), { selections: (0, lodash_sortby_1.default)(node.selections, "kind", "name.value") });
  69. },
  70. Field(node) {
  71. return Object.assign(Object.assign({}, node), { arguments: sorted(node.arguments, "name.value") });
  72. },
  73. FragmentSpread(node) {
  74. return Object.assign(Object.assign({}, node), { directives: sorted(node.directives, "name.value") });
  75. },
  76. InlineFragment(node) {
  77. return Object.assign(Object.assign({}, node), { directives: sorted(node.directives, "name.value") });
  78. },
  79. FragmentDefinition(node) {
  80. return Object.assign(Object.assign({}, node), { directives: sorted(node.directives, "name.value"), variableDefinitions: sorted(node.variableDefinitions, "variable.name.value") });
  81. },
  82. Directive(node) {
  83. return Object.assign(Object.assign({}, node), { arguments: sorted(node.arguments, "name.value") });
  84. },
  85. });
  86. }
  87. exports.sortAST = sortAST;
  88. function removeAliases(ast) {
  89. return (0, visitor_1.visit)(ast, {
  90. Field(node) {
  91. return Object.assign(Object.assign({}, node), { alias: undefined });
  92. },
  93. });
  94. }
  95. exports.removeAliases = removeAliases;
  96. function printWithReducedWhitespace(ast) {
  97. const sanitizedAST = (0, visitor_1.visit)(ast, {
  98. StringValue(node) {
  99. return Object.assign(Object.assign({}, node), { value: Buffer.from(node.value, "utf8").toString("hex"), block: false });
  100. },
  101. });
  102. const withWhitespace = (0, printer_1.print)(sanitizedAST);
  103. const minimizedButStillHex = withWhitespace
  104. .replace(/\s+/g, " ")
  105. .replace(/([^_a-zA-Z0-9]) /g, (_, c) => c)
  106. .replace(/ ([^_a-zA-Z0-9])/g, (_, c) => c);
  107. return minimizedButStillHex.replace(/"([a-f0-9]+)"/g, (_, hex) => JSON.stringify(Buffer.from(hex, "hex").toString("utf8")));
  108. }
  109. exports.printWithReducedWhitespace = printWithReducedWhitespace;
  110. //# sourceMappingURL=transforms.js.map