@@ -2,9 +2,11 @@ import type { Subscription } from "rxjs";
22import type { Observer } from "rxjs" ;
33import { Observable } from "rxjs" ;
44
5+ import type { ErrorLike } from "@apollo/client" ;
56import {
67 graphQLResultHasProtocolErrors ,
78 PROTOCOL_ERRORS_SYMBOL ,
9+ toErrorLike ,
810} from "@apollo/client/errors" ;
911import { ApolloLink } from "@apollo/client/link" ;
1012
@@ -15,7 +17,7 @@ export declare namespace RetryLink {
1517 export type DelayFunction = (
1618 count : number ,
1719 operation : ApolloLink . Operation ,
18- error : any
20+ error : ErrorLike
1921 ) => number ;
2022
2123 export interface DelayOptions {
@@ -54,7 +56,7 @@ export declare namespace RetryLink {
5456 export type AttemptsFunction = (
5557 count : number ,
5658 operation : ApolloLink . Operation ,
57- error : any
59+ error : ErrorLike
5860 ) => boolean | Promise < boolean > ;
5961
6062 export interface AttemptsOptions {
@@ -78,7 +80,7 @@ export declare namespace RetryLink {
7880 * @defaultValue `() => true`
7981 */
8082 retryIf ?: (
81- error : any ,
83+ error : ErrorLike ,
8284 operation : ApolloLink . Operation
8385 ) => boolean | Promise < boolean > ;
8486 }
@@ -102,7 +104,7 @@ export declare namespace RetryLink {
102104class RetryableOperation {
103105 private retryCount : number = 0 ;
104106 private currentSubscription : Subscription | null = null ;
105- private timerId : number | undefined ;
107+ private timerId : ReturnType < typeof setTimeout > | undefined ;
106108
107109 constructor (
108110 private observer : Observer < ApolloLink . Result > ,
@@ -130,7 +132,11 @@ class RetryableOperation {
130132 this . currentSubscription = this . forward ( this . operation ) . subscribe ( {
131133 next : ( result ) => {
132134 if ( graphQLResultHasProtocolErrors ( result ) ) {
133- this . onError ( result . extensions [ PROTOCOL_ERRORS_SYMBOL ] ) ;
135+ this . onError ( result . extensions [ PROTOCOL_ERRORS_SYMBOL ] , ( ) =>
136+ // Pretend like we never encountered this error and move the result
137+ // along for Apollo Client core to handle this error.
138+ this . observer . next ( result )
139+ ) ;
134140 // Unsubscribe from the current subscription to prevent the `complete`
135141 // handler to be called as a result of the stream closing.
136142 this . currentSubscription ?. unsubscribe ( ) ;
@@ -139,26 +145,28 @@ class RetryableOperation {
139145
140146 this . observer . next ( result ) ;
141147 } ,
142- error : this . onError ,
148+ error : ( error ) => this . onError ( error , ( ) => this . observer . error ( error ) ) ,
143149 complete : this . observer . complete . bind ( this . observer ) ,
144150 } ) ;
145151 }
146152
147- private onError = async ( error : any ) => {
153+ private onError = async ( error : unknown , onContinue : ( ) => void ) => {
148154 this . retryCount += 1 ;
155+ const errorLike = toErrorLike ( error ) ;
149156
150- // Should we retry?
151157 const shouldRetry = await this . retryIf (
152158 this . retryCount ,
153159 this . operation ,
154- error
160+ errorLike
155161 ) ;
156162 if ( shouldRetry ) {
157- this . scheduleRetry ( this . delayFor ( this . retryCount , this . operation , error ) ) ;
163+ this . scheduleRetry (
164+ this . delayFor ( this . retryCount , this . operation , errorLike )
165+ ) ;
158166 return ;
159167 }
160168
161- this . observer . error ( error ) ;
169+ onContinue ( ) ;
162170 } ;
163171
164172 private scheduleRetry ( delay : number ) {
@@ -169,7 +177,7 @@ class RetryableOperation {
169177 this . timerId = setTimeout ( ( ) => {
170178 this . timerId = undefined ;
171179 this . try ( ) ;
172- } , delay ) as any as number ;
180+ } , delay ) ;
173181 }
174182}
175183
0 commit comments