@@ -43,6 +43,9 @@ import GraphQL.Client.Types (class GqlQuery, class QueryClient, Client(..), GqlR
4343import GraphQL.Client.Variables (class VarsTypeChecked , getVarsJson , getVarsTypeNames )
4444import Type.Proxy (Proxy (..))
4545
46+ foreign import isConnectionError :: Error -> Boolean
47+ foreign import isNoConnectionError :: Error -> Boolean
48+
4649-- | Run a graphQL query with a custom decoder and custom options
4750queryOptsWithDecoder
4851 :: forall client directives schema query returns queryOpts mutationOpts sr
@@ -199,8 +202,12 @@ runQuery
199202 -> Aff returns
200203runQuery decodeFn opts client _ queryNameUnsafe q =
201204 addErrorInfo (Proxy @schema) queryName q do
202- json <- clientQuery opts client queryName (getVarsTypeNames (Proxy :: _ schema ) q <> toGqlQueryString q)
203- (getVarsJson (Proxy :: _ schema ) q)
205+ let doQuery = clientQuery opts client queryName (getVarsTypeNames (Proxy :: _ schema ) q <> toGqlQueryString q)
206+ (getVarsJson (Proxy :: _ schema ) q)
207+ json <- doQuery `catchError` \err ->
208+ -- Retry once on connection-level errors (ETIMEDOUT, ECONNREFUSED, etc.)
209+ if isConnectionError err then doQuery
210+ else throwError err
204211 decodeJsonData decodeFn json
205212 where
206213 queryName = safeQueryName queryNameUnsafe
@@ -218,8 +225,12 @@ runMutation
218225 -> Aff returns
219226runMutation decodeFn opts client _ queryNameUnsafe q =
220227 addErrorInfo (Proxy @schema) queryName q do
221- json <- clientMutation opts client queryName (getVarsTypeNames (Proxy :: _ schema ) q <> toGqlQueryString q)
222- (getVarsJson (Proxy :: _ schema ) q)
228+ let doMutation = clientMutation opts client queryName (getVarsTypeNames (Proxy :: _ schema ) q <> toGqlQueryString q)
229+ (getVarsJson (Proxy :: _ schema ) q)
230+ json <- doMutation `catchError` \err ->
231+ -- Only retry on ECONNREFUSED/ENETUNREACH where the request never reached the server
232+ if isNoConnectionError err then doMutation
233+ else throwError err
223234 decodeJsonData decodeFn json
224235 where
225236 queryName = safeQueryName queryNameUnsafe
0 commit comments