Skip to content

Commit cbf9b7b

Browse files
authored
Undeprecate FetchPolicy.CacheAndNetwork (#253)
* Undeprecate FetchPolicy.CacheAndNetwork * Tweak warning
1 parent 9e978b5 commit cbf9b7b

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.apollographql.cache.normalized
22

3+
import com.apollographql.apollo.ApolloCall
4+
35
enum class FetchPolicy {
46
/**
57
* Emit the response from the cache first, and if there was a cache miss, emit the response(s) from the network.
@@ -25,7 +27,9 @@ enum class FetchPolicy {
2527

2628
/**
2729
* Emit the response from the cache first, and then emit the response(s) from the network.
30+
*
31+
* Warning: this can emit multiple successful responses, therefore [ApolloCall.execute] should not be used with this fetch policy.
32+
* Use only with [ApolloCall.toFlow] or [ApolloCall.watch].
2833
*/
29-
@Deprecated("This is equivalent of executing with CacheOnly and then with NetworkOnly")
3034
CacheAndNetwork,
3135
}

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.apollographql.cache.normalized
22

3+
import com.apollographql.apollo.ApolloCall
34
import com.apollographql.apollo.api.ApolloRequest
45
import com.apollographql.apollo.api.ApolloResponse
56
import com.apollographql.apollo.api.Operation
@@ -42,11 +43,12 @@ val DefaultFetchPolicyInterceptor = object : ApolloInterceptor {
4243
request = request
4344
.newBuilder()
4445
.fetchFromCache(true)
45-
.build()
46+
.build(),
4647
).single()
4748
.errorsAsException(throwOnCacheMiss = request.throwOnCacheMiss, serverErrorsAsCacheMisses = request.serverErrorsAsCacheMisses)
48-
emit(cacheResponse.newBuilder().isLast(request.onlyIfCached || cacheResponse.exception == null)
49-
.build()
49+
emit(
50+
cacheResponse.newBuilder().isLast(request.onlyIfCached || cacheResponse.exception == null)
51+
.build(),
5052
)
5153
if (cacheResponse.exception == null) {
5254
return@flow
@@ -70,7 +72,7 @@ val NetworkFirstInterceptor = object : ApolloInterceptor {
7072
var networkException: ApolloException? = null
7173

7274
val networkResponses = chain.proceed(
73-
request = request
75+
request = request,
7476
).onEach { response ->
7577
if (response.exception != null && networkException == null) {
7678
networkException = response.exception
@@ -94,7 +96,7 @@ val NetworkFirstInterceptor = object : ApolloInterceptor {
9496
request = request
9597
.newBuilder()
9698
.fetchFromCache(true)
97-
.build()
99+
.build(),
98100
).single()
99101
.errorsAsException(throwOnCacheMiss = request.throwOnCacheMiss, serverErrorsAsCacheMisses = request.serverErrorsAsCacheMisses)
100102
emit(cacheResponse)
@@ -104,16 +106,19 @@ val NetworkFirstInterceptor = object : ApolloInterceptor {
104106

105107
/**
106108
* An interceptor that emits the response from the cache first, and then emits the response(s) from the network.
109+
*
110+
* Warning: this can emit multiple successful responses, therefore [ApolloCall.execute] should not be used with this fetch policy.
111+
* Use only with [ApolloCall.toFlow] or [ApolloCall.watch].
112+
*
107113
*/
108-
@Deprecated("This is equivalent of executing with onlyIfCached(true) followed by noCache(true)")
109114
val CacheAndNetworkInterceptor = object : ApolloInterceptor {
110115
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
111116
return flow {
112117
val cacheResponse = chain.proceed(
113118
request = request
114119
.newBuilder()
115120
.fetchFromCache(true)
116-
.build()
121+
.build(),
117122
).single()
118123
.errorsAsException(throwOnCacheMiss = request.throwOnCacheMiss, serverErrorsAsCacheMisses = request.serverErrorsAsCacheMisses)
119124

@@ -171,11 +176,13 @@ private fun <D : Operation.Data> ApolloResponse<D>.errorsAsException(
171176
when {
172177
cacheMissException != null -> {
173178
newBuilder()
174-
.exception(cacheMissException.apply {
175-
if (cachedErrorException != null) {
176-
addSuppressed(cachedErrorException)
177-
}
178-
})
179+
.exception(
180+
cacheMissException.apply {
181+
if (cachedErrorException != null) {
182+
addSuppressed(cachedErrorException)
183+
}
184+
},
185+
)
179186
.data(null)
180187
.errors(null)
181188
.build()
@@ -224,9 +231,9 @@ internal object FetchPolicyRouterInterceptor : ApolloInterceptor {
224231
it.cacheInfo!!.newBuilder()
225232
.cacheMissException(exceptions.filterIsInstance<CacheMissException>().firstOrNull())
226233
.networkException(exceptions.firstOrNull { it !is CacheMissException })
227-
.build()
234+
.build(),
228235
)
229-
.build()
236+
.build(),
230237
)
231238
hasEmitted = true
232239
}
@@ -248,9 +255,9 @@ internal object FetchPolicyRouterInterceptor : ApolloInterceptor {
248255
emit(
249256
ApolloResponse.Builder(request.operation, request.requestUuid)
250257
.exception(exception)
251-
.build()
258+
.build(),
252259

253-
)
260+
)
254261
}
255262
}
256263
}

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/fetchPolicy.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal val <D : Operation.Data> ApolloRequest<D>.fetchPolicyInterceptor
2525
* This only has effects for queries. Mutations and subscriptions always use [FetchPolicy.NetworkOnly]
2626
*/
2727
fun <T> MutableExecutionOptions<T>.fetchPolicyInterceptor(interceptor: ApolloInterceptor) = addExecutionContext(
28-
FetchPolicyContext(interceptor)
28+
FetchPolicyContext(interceptor),
2929
)
3030

3131
/**
@@ -45,11 +45,9 @@ fun <T> MutableExecutionOptions<T>.fetchPolicy(fetchPolicy: FetchPolicy): T {
4545
FetchPolicy.CacheOnly -> onlyIfCached(true)
4646
FetchPolicy.NetworkOnly -> noCache(true)
4747
FetchPolicy.CacheFirst -> this as T
48-
@Suppress("DEPRECATION")
4948
FetchPolicy.CacheAndNetwork,
5049
-> {
5150
// CacheAndNetwork is deprecated but should still work
52-
@Suppress("DEPRECATION")
5351
fetchPolicyInterceptor(CacheAndNetworkInterceptor)
5452
}
5553
}

normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/refetchPolicy.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal val <T> MutableExecutionOptions<T>.refetchPolicyInterceptor
2222
* Sets the [FetchPolicy] used when watching queries and a cache change has been published
2323
*/
2424
fun <T> MutableExecutionOptions<T>.refetchPolicyInterceptor(interceptor: ApolloInterceptor) = addExecutionContext(
25-
RefetchPolicyContext(interceptor)
25+
RefetchPolicyContext(interceptor),
2626
)
2727

2828
/**
@@ -41,11 +41,8 @@ fun <T> MutableExecutionOptions<T>.refetchPolicy(fetchPolicy: FetchPolicy): T {
4141
FetchPolicy.CacheOnly -> refetchOnlyIfCached(true)
4242
FetchPolicy.NetworkOnly -> refetchNoCache(true)
4343
FetchPolicy.CacheFirst -> this as T
44-
@Suppress("DEPRECATION")
4544
FetchPolicy.CacheAndNetwork,
4645
-> {
47-
// CacheAndNetwork is deprecated but should still work
48-
@Suppress("DEPRECATION")
4946
refetchPolicyInterceptor(CacheAndNetworkInterceptor)
5047
}
5148
}

0 commit comments

Comments
 (0)