# How the retry should be implemented?

**URL:** <https://community.apollographql.com/t/how-the-retry-should-be-implemented/6332>\
**Category:** Client SDKs\
**Tags:** client, kotlin\
**Created:** [June 5, 2023, 8:45am UTC](https://community.apollographql.com/t/how-the-retry-should-be-implemented/6332 "2023-06-05T08:45:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sherry\_Ummen](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/sherry_ummen/32/3981_2.png) [@Sherry\_Ummen](https://community.apollographql.com/u/Sherry_Ummen)\
**Post date:** [June 5, 2023, 8:45am UTC](https://community.apollographql.com/t/how-the-retry-should-be-implemented/6332/1 "2023-06-05T08:45:11Z")

</div>

Hi,

I am bit lost what are the ways to implement retry logic.

I have a simple code like this

```kotlin
 val response = runBlocking {
        val mutation = apolloClient.mutation(
          UpdateCollectionMainStageMutation(
            input = it.first,
            version = version,
            metadata = Optional.present(it.second)
          )
        )
        mutation.execute()
      }

```

I see the there were some suggestions from old github comments, but not sure if they are accurate.

I see in the doc for react [Retry Link - Apollo GraphQL Docs](https://www.apollographql.com/docs/react/api/link/apollo-link-retry/#overview) , is there something for Kotlin?

---

<div class="post-metadata">

**Author:** ![Benoit\_Lubek](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/benoit_lubek/32/2070_2.png) [@Benoit\_Lubek](https://community.apollographql.com/u/Benoit_Lubek)\
**Post date:** [June 6, 2023, 7:43am UTC](https://community.apollographql.com/t/how-the-retry-should-be-implemented/6332/2 "2023-06-06T07:43:43Z")

</div>

Hi!

If you only need to retry certain operations on a case-by-case basis, you can simply enclose the call to `execute` into a `try catch` block and treat the exception here. Also check `response.errors` for [GraphQL errors](https://www.apollographql.com/docs/kotlin/essentials/errors). It could be an extension on `ApolloClient` to be nicer to use, something like:

```kotlin
suspend fun <T : Query.Data> ApolloClient.tryQuery(query: Query<T>, retryCount: Int): T? {
    for (i in 0..retryCount) {
        val response = try {
            this.query(query).execute()
        } catch (e: Exception) {
            // Log the errors
            continue
        }
        if (response.hasErrors()) {
            // Log the errors
            continue
        }
        return response.data
    }
    return null
}

```

If you want to have this behavior for all operations, you can use an [ApolloInterceptor](https://www.apollographql.com/docs/kotlin/advanced/interceptors-http#graphql-interceptors) for this, which will be automatically be called for all operations. Something like this:

```kotlin
class RetryInterceptor(private val retryCount: Long) : ApolloInterceptor {
    override fun <D : Operation.Data> intercept(
        request: ApolloRequest<D>,
        chain: ApolloInterceptorChain
    ): Flow<ApolloResponse<D>> {
        return chain.proceed(request)
            .onEach { response ->
                if (response.hasErrors()) {
                    throw Exception("There were GraphQL errors")
                }
            }
            .retry(retryCount) { true }
    }
}

```

and use it on your client like this:

```kotlin
    val apolloClient = ApolloClient.Builder()
        .serverUrl("https://...")
        .addInterceptor(RetryInterceptor(3))
        .build()

```

---

<div class="post-metadata">

**Author:** ![vinodjayachandran](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/vinodjayachandran/32/4508_2.png) [@vinodjayachandran](https://community.apollographql.com/u/vinodjayachandran)\
**Post date:** [October 6, 2023, 7:01am UTC](https://community.apollographql.com/t/how-the-retry-should-be-implemented/6332/3 "2023-10-06T07:01:00Z")

</div>

Hi @Benoit_Lubek  
Using the RetryInterceptor example above, would it retry for Network Errors ? Ideally doesn’t it make sense to retry only **Network Errors**? Is there any scenario where retrying GraphQL Errors help ?

Reference Error Handling doc : [Error handling - Apollo GraphQL Docs](https://www.apollographql.com/docs/kotlin/essentials/errors)

---

<div class="post-metadata">

**Author:** ![Benoit\_Lubek](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/benoit_lubek/32/2070_2.png) [@Benoit\_Lubek](https://community.apollographql.com/u/Benoit_Lubek)\
**Post date:** [October 6, 2023, 7:17am UTC](https://community.apollographql.com/t/how-the-retry-should-be-implemented/6332/4 "2023-10-06T07:17:08Z")

</div>

Hi!

In the code above, both GraphQL and network errors would cause a retry, but it doesn’t necessarily makes sense to do that - in most cases you probably won’t want or need to do that, or at least do it only on certain classes of errors only (I can imagine some “Backend temporary failure, please retry” types of errors for instance). Ultimately that depends on your project’s specific needs.

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:48pm UTC](https://community.apollographql.com/t/how-the-retry-should-be-implemented/6332/5 "2025-02-06T20:48:09Z")

</div>


