linkUtils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var zen_observable_ts_1 = tslib_1.__importDefault(require("zen-observable-ts"));
  5. var apollo_utilities_1 = require("apollo-utilities");
  6. exports.getOperationName = apollo_utilities_1.getOperationName;
  7. var ts_invariant_1 = require("ts-invariant");
  8. function validateOperation(operation) {
  9. var OPERATION_FIELDS = [
  10. 'query',
  11. 'operationName',
  12. 'variables',
  13. 'extensions',
  14. 'context',
  15. ];
  16. for (var _i = 0, _a = Object.keys(operation); _i < _a.length; _i++) {
  17. var key = _a[_i];
  18. if (OPERATION_FIELDS.indexOf(key) < 0) {
  19. throw new ts_invariant_1.InvariantError("illegal argument: " + key);
  20. }
  21. }
  22. return operation;
  23. }
  24. exports.validateOperation = validateOperation;
  25. var LinkError = (function (_super) {
  26. tslib_1.__extends(LinkError, _super);
  27. function LinkError(message, link) {
  28. var _this = _super.call(this, message) || this;
  29. _this.link = link;
  30. return _this;
  31. }
  32. return LinkError;
  33. }(Error));
  34. exports.LinkError = LinkError;
  35. function isTerminating(link) {
  36. return link.request.length <= 1;
  37. }
  38. exports.isTerminating = isTerminating;
  39. function toPromise(observable) {
  40. var completed = false;
  41. return new Promise(function (resolve, reject) {
  42. observable.subscribe({
  43. next: function (data) {
  44. if (completed) {
  45. ts_invariant_1.invariant.warn("Promise Wrapper does not support multiple results from Observable");
  46. }
  47. else {
  48. completed = true;
  49. resolve(data);
  50. }
  51. },
  52. error: reject,
  53. });
  54. });
  55. }
  56. exports.toPromise = toPromise;
  57. exports.makePromise = toPromise;
  58. function fromPromise(promise) {
  59. return new zen_observable_ts_1.default(function (observer) {
  60. promise
  61. .then(function (value) {
  62. observer.next(value);
  63. observer.complete();
  64. })
  65. .catch(observer.error.bind(observer));
  66. });
  67. }
  68. exports.fromPromise = fromPromise;
  69. function fromError(errorValue) {
  70. return new zen_observable_ts_1.default(function (observer) {
  71. observer.error(errorValue);
  72. });
  73. }
  74. exports.fromError = fromError;
  75. function transformOperation(operation) {
  76. var transformedOperation = {
  77. variables: operation.variables || {},
  78. extensions: operation.extensions || {},
  79. operationName: operation.operationName,
  80. query: operation.query,
  81. };
  82. if (!transformedOperation.operationName) {
  83. transformedOperation.operationName =
  84. typeof transformedOperation.query !== 'string'
  85. ? apollo_utilities_1.getOperationName(transformedOperation.query)
  86. : '';
  87. }
  88. return transformedOperation;
  89. }
  90. exports.transformOperation = transformOperation;
  91. function createOperation(starting, operation) {
  92. var context = tslib_1.__assign({}, starting);
  93. var setContext = function (next) {
  94. if (typeof next === 'function') {
  95. context = tslib_1.__assign({}, context, next(context));
  96. }
  97. else {
  98. context = tslib_1.__assign({}, context, next);
  99. }
  100. };
  101. var getContext = function () { return (tslib_1.__assign({}, context)); };
  102. Object.defineProperty(operation, 'setContext', {
  103. enumerable: false,
  104. value: setContext,
  105. });
  106. Object.defineProperty(operation, 'getContext', {
  107. enumerable: false,
  108. value: getContext,
  109. });
  110. Object.defineProperty(operation, 'toKey', {
  111. enumerable: false,
  112. value: function () { return getKey(operation); },
  113. });
  114. return operation;
  115. }
  116. exports.createOperation = createOperation;
  117. function getKey(operation) {
  118. var query = operation.query, variables = operation.variables, operationName = operation.operationName;
  119. return JSON.stringify([operationName, query, variables]);
  120. }
  121. exports.getKey = getKey;
  122. //# sourceMappingURL=linkUtils.js.map