text: var __assign = (this && this.__assign) || function () {
      __assign = Object.assign || function(t) {
          for (var s, i = 1, n = arguments.length; i < n; i++) {
              s = arguments[i];
              for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                  t[p] = s[p];
          }
          return t;
      };
      return __assign.apply(this, arguments);
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  var graphql_1 = require("graphql");
  var TransformQuery = /** @class */ (function () {
      function TransformQuery(_a) {
          var path = _a.path, queryTransformer = _a.queryTransformer, _b = _a.resultTransformer, resultTransformer = _b === void 0 ? function (result) { return result; } : _b, _c = _a.errorPathTransformer, errorPathTransformer = _c === void 0 ? function (errorPath) { return [].concat(errorPath); } : _c, _d = _a.fragments, fragments = _d === void 0 ? {} : _d;
          this.path = path;
          this.queryTransformer = queryTransformer;
          this.resultTransformer = resultTransformer;
          this.errorPathTransformer = errorPathTransformer;
          this.fragments = fragments;
      }
      TransformQuery.prototype.transformRequest = function (originalRequest) {
          var _a;
          var _this = this;
          var document = originalRequest.document;
          var pathLength = this.path.length;
          var index = 0;
          var newDocument = graphql_1.visit(document, (_a = {},
              _a[graphql_1.Kind.FIELD] = {
                  enter: function (node) {
                      if (index === pathLength || node.name.value !== _this.path[index]) {
                          return false;
                      }
                      index++;
                      if (index === pathLength) {
                          var selectionSet = _this.queryTransformer(node.selectionSet, _this.fragments);
                          return __assign(__assign({}, node), { selectionSet: selectionSet });
                      }
                  },
                  leave: function () {
                      index--;
                  }
              },
              _a));
          return __assign(__assign({}, originalRequest), { document: newDocument });
      };
      TransformQuery.prototype.transformResult = function (originalResult) {
          var data = this.transformData(originalResult.data);
          var errors = originalResult.errors;
          return {
              data: data,
              errors: errors ? this.transformErrors(errors) : undefined
          };
      };
      TransformQuery.prototype.transformData = function (data) {
          var index = 0;
          var leafIndex = this.path.length - 1;
          if (data) {
              var next = this.path[index];
              while (index < leafIndex) {
                  if (data[next]) {
                      data = data[next];
                  }
                  else {
                      break;
                  }
                  index++;
                  next = this.path[index];
              }
              data[next] = this.resultTransformer(data[next]);
          }
          return data;
      };
      TransformQuery.prototype.transformErrors = function (errors) {
          var _this = this;
          return errors.map(function (error) {
              var path = error.path;
              var match = true;
              var index = 0;
              while (index < _this.path.length) {
                  if (path[index] !== _this.path[index]) {
                      match = false;
                      break;
                  }
                  index++;
              }
              var newPath = match ?
                  path.slice(0, index).concat(_this.errorPathTransformer(path.slice(index))) :
                  path;
              return new graphql_1.GraphQLError(error.message, error.nodes, error.source, error.positions, newPath, error.originalError, error.extensions);
          });
      };
      return TransformQuery;
  }());
  exports.default = TransformQuery;
  //# sourceMappingURL=TransformQuery.js.map