Skip to content

Commit

Permalink
(retry) - Add operation argument to retryIf predicate (#1117)
Browse files Browse the repository at this point in the history
* (retry) - Add operation argument to retryIf predicate

* Update test expectations for retryIf mocks
  • Loading branch information
kitten committed Nov 4, 2020
1 parent da5a2eb commit 2997679
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-grapes-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-retry': minor
---

Add a second `Operation` input argument to the `retryIf` predicate, so that retrying can be actively avoided for specific types of operations, e.g. mutations or subscriptions, in certain user-defined cases.
6 changes: 3 additions & 3 deletions exchanges/retry/src/retryExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ it(`retries if it hits an error and works for multiple concurrent operations`, (
next(op);

expect(mockRetryIf).toHaveBeenCalledTimes(1);
expect(mockRetryIf).toHaveBeenCalledWith(queryOneError);
expect(mockRetryIf).toHaveBeenCalledWith(queryOneError, op);

jest.runAllTimers();

Expand All @@ -131,7 +131,7 @@ it(`retries if it hits an error and works for multiple concurrent operations`, (

jest.runAllTimers();

expect(mockRetryIf).toHaveBeenCalledWith(queryTwoError);
expect(mockRetryIf).toHaveBeenCalledWith(queryTwoError, opTwo);

// max number of retries for each op
expect(response).toHaveBeenCalledTimes(mockOptions.maxNumberAttempts * 2);
Expand Down Expand Up @@ -176,7 +176,7 @@ it('should retry x number of times and then return the successful result', () =>
jest.runAllTimers();

expect(mockRetryIf).toHaveBeenCalledTimes(numberRetriesBeforeSuccess);
expect(mockRetryIf).toHaveBeenCalledWith(queryOneError);
expect(mockRetryIf).toHaveBeenCalledWith(queryOneError, op);

// one for original source, one for retry
expect(response).toHaveBeenCalledTimes(1 + numberRetriesBeforeSuccess);
Expand Down
4 changes: 2 additions & 2 deletions exchanges/retry/src/retryExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface RetryExchangeOptions {
randomDelay?: boolean;
maxNumberAttempts?: number;
/** Conditionally determine whether an error should be retried */
retryIf?: (e: CombinedError) => boolean;
retryIf?: (error: CombinedError, operation: Operation) => boolean;
}

export const retryExchange = ({
Expand Down Expand Up @@ -106,7 +106,7 @@ export const retryExchange = ({
filter(res => {
// Only retry if the error passes the conditional retryIf function (if passed)
// or if the error contains a networkError
if (!res.error || !retryIf(res.error)) {
if (!res.error || !retryIf(res.error, res.operation)) {
return true;
}

Expand Down

0 comments on commit 2997679

Please sign in to comment.