Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(retry) - Add operation argument to retryIf predicate #1117

Merged
merged 2 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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