UNPKG

apollo-link-shopify-retry

Version:

Easy setup for retrying rate limited request in Shopify's (Admin) GraphQL API.

109 lines (96 loc) 4.6 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var error = require('@apollo/client/link/error'); var Observable = require('zen-observable'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var Observable__default = /*#__PURE__*/_interopDefaultLegacy(Observable); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(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); }; function calculateDelayCost(cost) { var requestedQueryCost = cost.requestedQueryCost, actualQueryCost = cost.actualQueryCost, _a = cost.throttleStatus, currentlyAvailable = _a.currentlyAvailable, restoreRate = _a.restoreRate; var requested = actualQueryCost || requestedQueryCost; var restoreAmount = Math.max(0, requested - currentlyAvailable); return Math.ceil(restoreAmount / restoreRate) * 1000; } function exceedsMaximumCost(cost) { var requestedQueryCost = cost.requestedQueryCost, actualQueryCost = cost.actualQueryCost, maximumAvailable = cost.throttleStatus.maximumAvailable; var requested = actualQueryCost || requestedQueryCost; return requested > maximumAvailable; } var defaultOptions = { debug: true, }; function isThrottledError(error) { var _a; return ((_a = error.extensions) === null || _a === void 0 ? void 0 : _a.code) === "THROTTLED"; } function delay(msToWait) { return new Observable__default['default'](function (observer) { var timer = setTimeout(function () { observer.complete(); }, msToWait); return function () { return clearTimeout(timer); }; }); } function createShopifyRetryLink(optionsOverride) { var options = __assign(__assign({}, defaultOptions), (optionsOverride || {})); return error.onError(function (_a) { var _b, _c; var graphQLErrors = _a.graphQLErrors, networkError = _a.networkError, forward = _a.forward, operation = _a.operation, response = _a.response; if (networkError) { // A non 429 connection error. // Fallback to ApolloClient's own error handler. return; } if (!graphQLErrors) { // An error we cannot respond to with rate limit handling. We require a specific error extension. // Fallback to ApolloClient's own error handler. return; } if (!graphQLErrors.some(isThrottledError)) { // There was no throttling for this request. // Fallback to ApolloClient's own error handler. return; } var cost = (_c = (_b = response) === null || _b === void 0 ? void 0 : _b.extensions) === null || _c === void 0 ? void 0 : _c.cost; if (!cost) { // We require the cost extension to calculate the delay. // Fallback to ApolloClient's own error handler. return; } if (exceedsMaximumCost(cost)) { // Your query costs more than the maximum allowed cost. // Fallback to ApolloClient's own error handler. return; } var msToWait = calculateDelayCost(cost); if (options.debug) { console.log("Throttled. Milliseconds to wait: " + msToWait); } operation.setContext({ retry: true }); return delay(msToWait).concat(forward(operation)); }); } exports.calculateDelayCost = calculateDelayCost; exports.createShopifyRetryLink = createShopifyRetryLink; exports.exceedsMaximumCost = exceedsMaximumCost;