Skip to content

Commit 6492256

Browse files
authored
remove ensureUuid() (#6075)
1 parent d513599 commit 6492256

File tree

3 files changed

+30
-50
lines changed

3 files changed

+30
-50
lines changed

docs/source/advanced/experimental-websockets.mdx

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,22 @@ class MyRetryOnErrorInterceptor : ApolloInterceptor {
123123

124124
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
125125
var attempt = 0
126-
return request.ensureUniqueUuid()
127-
.flatMapConcat { chain.proceed(it) }.onEach {
128-
if (request.retryOnError == true && it.exception != null && it.exception is ApolloNetworkException) {
129-
throw RetryException
130-
} else {
131-
attempt = 0
132-
}
133-
}.retryWhen { cause, _ ->
134-
if (cause is RetryException) {
135-
attempt++
136-
delay(2.0.pow(attempt).seconds)
137-
true
138-
} else {
139-
// Not a RetryException, probably a programming error, pass it through
140-
false
141-
}
142-
}
126+
return chain.proceed(request).onEach {
127+
if (request.retryOnError == true && it.exception != null && it.exception is ApolloNetworkException) {
128+
throw RetryException
129+
} else {
130+
attempt = 0
131+
}
132+
}.retryWhen { cause, _ ->
133+
if (cause is RetryException) {
134+
attempt++
135+
delay(2.0.pow(attempt).seconds)
136+
true
137+
} else {
138+
// Not a RetryException, probably a programming error, pass it through
139+
false
140+
}
141+
}
143142
}
144143
}
145144
```

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo/interceptor/RetryOnErrorInterceptor.kt

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ private class DefaultRetryOnErrorInterceptorImpl(private val networkMonitor: Net
5858
}
5959

6060
var attempt = 0
61-
return request.ensureUniqueUuid()
62-
.flatMapConcatPolyfill {
61+
val downStream = chain.proceed(request)
62+
63+
return flow {
6364
if (failFastIfOffline && networkMonitor?.isOnline() == false) {
64-
flowOf(ApolloResponse.Builder(request.operation, request.requestUuid).exception(OfflineException).build())
65+
emit((ApolloResponse.Builder(request.operation, request.requestUuid).exception(OfflineException).build()))
6566
} else {
66-
chain.proceed(it)
67+
emitAll(downStream)
6768
}
6869
}.onEach {
6970
if (retryOnError && it.exception != null && it.exception!!.isRecoverable()) {
@@ -88,19 +89,6 @@ private class DefaultRetryOnErrorInterceptorImpl(private val networkMonitor: Net
8889
}
8990
}
9091

91-
@ApolloExperimental
92-
fun <D : Operation.Data> ApolloRequest<D>.ensureUniqueUuid(): Flow<ApolloRequest<D>> {
93-
var first = true
94-
return flow {
95-
if (first) {
96-
first = false
97-
emit(this@ensureUniqueUuid)
98-
} else {
99-
emit(newBuilder().requestUuid(uuid4()).build())
100-
}
101-
}
102-
}
103-
10492
private fun ApolloException.isRecoverable(): Boolean {
10593
/**
10694
* TODO: refine this. Some networks errors are probably not recoverable (SSL errors probably, maybe others?)

libraries/apollo-runtime/src/jvmTest/kotlin/RetryWebSocketsTest.kt

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
11

22
import app.cash.turbine.test
33
import com.apollographql.apollo.ApolloClient
4-
import com.apollographql.apollo.annotations.ApolloExperimental
5-
import com.apollographql.apollo.api.ApolloRequest
6-
import com.apollographql.apollo.api.ApolloResponse
7-
import com.apollographql.apollo.api.Operation
84
import com.apollographql.apollo.api.Subscription
9-
import com.apollographql.apollo.exception.ApolloException
105
import com.apollographql.apollo.exception.ApolloHttpException
116
import com.apollographql.apollo.exception.ApolloNetworkException
12-
import com.apollographql.apollo.interceptor.ApolloInterceptor
13-
import com.apollographql.apollo.interceptor.ApolloInterceptorChain
7+
import com.apollographql.apollo.network.websocket.WebSocketNetworkTransport
8+
import com.apollographql.apollo.testing.connectionAckMessage
9+
import com.apollographql.apollo.testing.internal.runTest
1410
import com.apollographql.mockserver.MockResponse
1511
import com.apollographql.mockserver.MockServer
1612
import com.apollographql.mockserver.awaitWebSocketRequest
1713
import com.apollographql.mockserver.enqueueWebSocket
18-
import com.apollographql.apollo.network.websocket.WebSocketNetworkTransport
19-
import test.FooQuery
20-
import test.FooSubscription
21-
import test.FooSubscription.Companion.completeMessage
22-
import test.FooSubscription.Companion.nextMessage
23-
import com.apollographql.apollo.testing.connectionAckMessage
24-
import com.apollographql.apollo.testing.internal.runTest
2514
import kotlinx.coroutines.CoroutineScope
2615
import kotlinx.coroutines.delay
2716
import kotlinx.coroutines.flow.collect
2817
import kotlinx.coroutines.flow.take
2918
import kotlinx.coroutines.launch
3019
import kotlinx.coroutines.withTimeout
3120
import okio.use
21+
import test.FooQuery
22+
import test.FooSubscription
23+
import test.FooSubscription.Companion.completeMessage
24+
import test.FooSubscription.Companion.nextMessage
25+
import test.network.awaitSubscribe
3226
import test.network.enqueueMessage
3327
import test.network.mockServerTest
28+
import test.network.retryWhen
3429
import kotlin.test.Test
3530
import kotlin.test.assertEquals
3631
import kotlin.test.assertIs
3732
import kotlin.test.assertNotEquals
3833
import kotlin.time.Duration.Companion.seconds
39-
import test.network.awaitSubscribe
40-
import test.network.retryWhen
4134

4235
class RetryWebSocketsTest {
4336
@Test

0 commit comments

Comments
 (0)