How to retry a request on GraphQLError in graphql_flutter

Viewed 770

I want to retry a graphql request when the graphql response has some particular errors. My current code:

QueryResult result = await client.query(
              QueryOptions(
                documentNode: gql(queries.getNextEpisode),
                variables: <String, dynamic>{
                  'id': id,
                  'keyType': 'POSTER',
                },
              ),
            );
if (result.hasException && 
    result.exception.graphqlErrors
      .any((element) => element.message.contains("401"))) {
// retry the request 
}

I tried the retry package but I think it only works if there is an exception thrown, which I don't think is the case here.

I'm using graphql_flutter: ^3.0.0.

1 Answers

For this you have to use fresh_graphql/fresh_graphql.dart package.

_initFreshLink() {
_freshLink = FreshLink<OAuth2Token>(
  tokenStorage: _easyInMemoryTokenStorage,
  refreshToken: _refreshToken,
  shouldRefresh: (FetchResult result) {
    var hasAuthError = result.errors != null &&
        result.errors
                .where((e) =>
                    e['extensions'] != null &&
                    e['extensions']['code'] != null &&
                    e['extensions']['code'] == 'UNAUTHENTICATED')
                .toList()
                .length !=
            0;
    return hasAuthError;
  },
)..authenticationStatus.listen(print);
 }
Related