ExtractField.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. Object.defineProperty(exports, "__esModule", { value: true });
  13. var graphql_1 = require("graphql");
  14. var ExtractField = /** @class */ (function () {
  15. function ExtractField(_a) {
  16. var from = _a.from, to = _a.to;
  17. this.from = from;
  18. this.to = to;
  19. }
  20. ExtractField.prototype.transformRequest = function (originalRequest) {
  21. var _a, _b;
  22. var fromSelection;
  23. var ourPathFrom = JSON.stringify(this.from);
  24. var ourPathTo = JSON.stringify(this.to);
  25. var fieldPath = [];
  26. graphql_1.visit(originalRequest.document, (_a = {},
  27. _a[graphql_1.Kind.FIELD] = {
  28. enter: function (node) {
  29. fieldPath.push(node.name.value);
  30. if (ourPathFrom === JSON.stringify(fieldPath)) {
  31. fromSelection = node.selectionSet;
  32. return graphql_1.BREAK;
  33. }
  34. },
  35. leave: function (node) {
  36. fieldPath.pop();
  37. },
  38. },
  39. _a));
  40. fieldPath = [];
  41. var newDocument = graphql_1.visit(originalRequest.document, (_b = {},
  42. _b[graphql_1.Kind.FIELD] = {
  43. enter: function (node) {
  44. fieldPath.push(node.name.value);
  45. if (ourPathTo === JSON.stringify(fieldPath) && fromSelection) {
  46. return __assign(__assign({}, node), { selectionSet: fromSelection });
  47. }
  48. },
  49. leave: function (node) {
  50. fieldPath.pop();
  51. },
  52. },
  53. _b));
  54. return __assign(__assign({}, originalRequest), { document: newDocument });
  55. };
  56. return ExtractField;
  57. }());
  58. exports.default = ExtractField;
  59. //# sourceMappingURL=ExtractField.js.map