apollo-link-retry
Advanced tools
Comparing version 2.1.3 to 2.2.0
@@ -5,2 +5,9 @@ # Change log | ||
### 2.2.0 | ||
- handle Promises from `retryIf` and `attempts` [#436](https://github.com/apollographql/apollo-link/pull/436) | ||
- udate apollo link with zen-observable-ts [PR#515](https://github.com/apollographql/apollo-link/pull/515) | ||
### 2.1.0 | ||
- add `retryIf` [PR#324](https://github.com/apollographql/apollo-link/pull/324) | ||
### 2.0.2 | ||
@@ -7,0 +14,0 @@ - ApolloLink upgrade |
@@ -44,2 +44,37 @@ (function (global, factory) { | ||
})(); | ||
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (undefined && undefined.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var RetryableOperation = (function () { | ||
@@ -72,14 +107,24 @@ function RetryableOperation(operation, nextLink, delayFor, retryIf) { | ||
}; | ||
this.onError = function (error) { | ||
_this.retryCount += 1; | ||
if (_this.retryIf(_this.retryCount, _this.operation, error)) { | ||
_this.scheduleRetry(_this.delayFor(_this.retryCount, _this.operation, error)); | ||
return; | ||
} | ||
_this.error = error; | ||
for (var _i = 0, _a = _this.observers; _i < _a.length; _i++) { | ||
var observer = _a[_i]; | ||
observer.error(error); | ||
} | ||
}; | ||
this.onError = function (error) { return __awaiter(_this, void 0, void 0, function () { | ||
var shouldRetry, _i, _a, observer; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
this.retryCount += 1; | ||
return [4, this.retryIf(this.retryCount, this.operation, error)]; | ||
case 1: | ||
shouldRetry = _b.sent(); | ||
if (shouldRetry) { | ||
this.scheduleRetry(this.delayFor(this.retryCount, this.operation, error)); | ||
return [2]; | ||
} | ||
this.error = error; | ||
for (_i = 0, _a = this.observers; _i < _a.length; _i++) { | ||
observer = _a[_i]; | ||
observer.error(error); | ||
} | ||
return [2]; | ||
} | ||
}); | ||
}); }; | ||
} | ||
@@ -86,0 +131,0 @@ RetryableOperation.prototype.subscribe = function (observer) { |
import { Operation } from 'apollo-link'; | ||
export interface RetryFunction { | ||
(count: number, operation: Operation, error: any): boolean; | ||
(count: number, operation: Operation, error: any): boolean | Promise<boolean>; | ||
} | ||
export interface RetryFunctionOptions { | ||
max?: number; | ||
retryIf?: (error: any, operation: Operation) => boolean; | ||
retryIf?: (error: any, operation: Operation) => boolean | Promise<boolean>; | ||
} | ||
export declare function buildRetryFunction({max, retryIf}?: RetryFunctionOptions): RetryFunction; |
@@ -11,2 +11,37 @@ var __extends = (this && this.__extends) || (function () { | ||
})(); | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
import { ApolloLink, Observable, } from 'apollo-link'; | ||
@@ -42,14 +77,24 @@ import { buildDelayFunction, } from './delayFunction'; | ||
}; | ||
this.onError = function (error) { | ||
_this.retryCount += 1; | ||
if (_this.retryIf(_this.retryCount, _this.operation, error)) { | ||
_this.scheduleRetry(_this.delayFor(_this.retryCount, _this.operation, error)); | ||
return; | ||
} | ||
_this.error = error; | ||
for (var _i = 0, _a = _this.observers; _i < _a.length; _i++) { | ||
var observer = _a[_i]; | ||
observer.error(error); | ||
} | ||
}; | ||
this.onError = function (error) { return __awaiter(_this, void 0, void 0, function () { | ||
var shouldRetry, _i, _a, observer; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
this.retryCount += 1; | ||
return [4, this.retryIf(this.retryCount, this.operation, error)]; | ||
case 1: | ||
shouldRetry = _b.sent(); | ||
if (shouldRetry) { | ||
this.scheduleRetry(this.delayFor(this.retryCount, this.operation, error)); | ||
return [2]; | ||
} | ||
this.error = error; | ||
for (_i = 0, _a = this.observers; _i < _a.length; _i++) { | ||
observer = _a[_i]; | ||
observer.error(error); | ||
} | ||
return [2]; | ||
} | ||
}); | ||
}); }; | ||
} | ||
@@ -56,0 +101,0 @@ RetryableOperation.prototype.subscribe = function (observer) { |
{ | ||
"name": "apollo-link-retry", | ||
"version": "2.1.3", | ||
"version": "2.2.0", | ||
"description": "Retry Apollo Link for GraphQL Network Stack", | ||
@@ -42,3 +42,3 @@ "author": "Evans Hauser <evanshauser@gmail.com>", | ||
"@types/zen-observable": "0.5.3", | ||
"apollo-link": "^1.2.0" | ||
"apollo-link": "^1.2.1" | ||
}, | ||
@@ -45,0 +45,0 @@ "devDependencies": { |
@@ -172,2 +172,25 @@ import gql from 'graphql-tag'; | ||
}); | ||
}); | ||
it('supports custom attempt functions that return either Promises or booleans', async () => { | ||
const attemptStub = jest.fn(); | ||
attemptStub.mockReturnValueOnce(true); | ||
attemptStub.mockReturnValueOnce(Promise.resolve(true)); | ||
attemptStub.mockReturnValueOnce(Promise.resolve(false)); | ||
const retry = new RetryLink({ | ||
delay: { initial: 1 }, | ||
attempts: attemptStub, | ||
}); | ||
const linkStub = jest.fn(() => new Observable(o => o.error(standardError))); | ||
const link = ApolloLink.from([retry, linkStub]); | ||
const [{ error }] = await waitFor(execute(link, { query })); | ||
expect(error).toEqual(standardError); | ||
const operation = attemptStub.mock.calls[0][1]; | ||
expect(attemptStub.mock.calls).toEqual([ | ||
[1, operation, standardError], | ||
[2, operation, standardError], | ||
[3, operation, standardError], | ||
]); | ||
}); |
@@ -8,3 +8,3 @@ import { Operation } from 'apollo-link'; | ||
export interface RetryFunction { | ||
(count: number, operation: Operation, error: any): boolean; | ||
(count: number, operation: Operation, error: any): boolean | Promise<boolean>; | ||
} | ||
@@ -31,3 +31,3 @@ | ||
*/ | ||
retryIf?: (error: any, operation: Operation) => boolean; | ||
retryIf?: (error: any, operation: Operation) => boolean | Promise<boolean>; | ||
} | ||
@@ -34,0 +34,0 @@ |
@@ -147,8 +147,11 @@ import { | ||
private onError = error => { | ||
private onError = async error => { | ||
this.retryCount += 1; | ||
// Should we retry? | ||
if (this.retryIf(this.retryCount, this.operation, error)) { | ||
this.scheduleRetry(this.delayFor(this.retryCount, this.operation, error)); | ||
const shouldRetry = await this.retryIf(this.retryCount, this.operation, error) | ||
if (shouldRetry) { | ||
this.scheduleRetry( | ||
this.delayFor(this.retryCount, this.operation, error), | ||
); | ||
return; | ||
@@ -155,0 +158,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
80778
1218
Updatedapollo-link@^1.2.1