From 6fa38724c50e117c96bb2bbc3b84467856b8cd16 Mon Sep 17 00:00:00 2001 From: BoD Date: Fri, 31 Oct 2025 19:01:51 +0100 Subject: [PATCH 1/2] Undeprecate FetchPolicy.CacheAndNetwork --- .../cache/normalized/FetchPolicy.kt | 6 ++- .../normalized/FetchPolicyInterceptors.kt | 39 +++++++++++-------- .../cache/normalized/options/fetchPolicy.kt | 4 +- .../cache/normalized/options/refetchPolicy.kt | 5 +-- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt index b88aa21f..8a3c62c1 100644 --- a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt +++ b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt @@ -1,5 +1,7 @@ package com.apollographql.cache.normalized +import com.apollographql.apollo.ApolloCall + enum class FetchPolicy { /** * 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 { /** * Emit the response from the cache first, and then emit the response(s) from the network. + * + * Warning: this can emit multiple successful responses, which is not allowed by [ApolloCall.execute] and will + * crash. Use only with [ApolloCall.toFlow] or [ApolloCall.watch]. */ - @Deprecated("This is equivalent of executing with CacheOnly and then with NetworkOnly") CacheAndNetwork, } diff --git a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt index 1c17af02..af934fcd 100644 --- a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt +++ b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt @@ -1,5 +1,6 @@ package com.apollographql.cache.normalized +import com.apollographql.apollo.ApolloCall import com.apollographql.apollo.api.ApolloRequest import com.apollographql.apollo.api.ApolloResponse import com.apollographql.apollo.api.Operation @@ -42,11 +43,12 @@ val DefaultFetchPolicyInterceptor = object : ApolloInterceptor { request = request .newBuilder() .fetchFromCache(true) - .build() + .build(), ).single() .errorsAsException(throwOnCacheMiss = request.throwOnCacheMiss, serverErrorsAsCacheMisses = request.serverErrorsAsCacheMisses) - emit(cacheResponse.newBuilder().isLast(request.onlyIfCached || cacheResponse.exception == null) - .build() + emit( + cacheResponse.newBuilder().isLast(request.onlyIfCached || cacheResponse.exception == null) + .build(), ) if (cacheResponse.exception == null) { return@flow @@ -70,7 +72,7 @@ val NetworkFirstInterceptor = object : ApolloInterceptor { var networkException: ApolloException? = null val networkResponses = chain.proceed( - request = request + request = request, ).onEach { response -> if (response.exception != null && networkException == null) { networkException = response.exception @@ -94,7 +96,7 @@ val NetworkFirstInterceptor = object : ApolloInterceptor { request = request .newBuilder() .fetchFromCache(true) - .build() + .build(), ).single() .errorsAsException(throwOnCacheMiss = request.throwOnCacheMiss, serverErrorsAsCacheMisses = request.serverErrorsAsCacheMisses) emit(cacheResponse) @@ -104,8 +106,11 @@ val NetworkFirstInterceptor = object : ApolloInterceptor { /** * An interceptor that emits the response from the cache first, and then emits the response(s) from the network. + * + * Warning: this can emit multiple successful responses, which is not allowed by [ApolloCall.execute] and will + * crash. Use only with [ApolloCall.toFlow] or [ApolloCall.watch]. + * */ -@Deprecated("This is equivalent of executing with onlyIfCached(true) followed by noCache(true)") val CacheAndNetworkInterceptor = object : ApolloInterceptor { override fun intercept(request: ApolloRequest, chain: ApolloInterceptorChain): Flow> { return flow { @@ -113,7 +118,7 @@ val CacheAndNetworkInterceptor = object : ApolloInterceptor { request = request .newBuilder() .fetchFromCache(true) - .build() + .build(), ).single() .errorsAsException(throwOnCacheMiss = request.throwOnCacheMiss, serverErrorsAsCacheMisses = request.serverErrorsAsCacheMisses) @@ -171,11 +176,13 @@ private fun ApolloResponse.errorsAsException( when { cacheMissException != null -> { newBuilder() - .exception(cacheMissException.apply { - if (cachedErrorException != null) { - addSuppressed(cachedErrorException) - } - }) + .exception( + cacheMissException.apply { + if (cachedErrorException != null) { + addSuppressed(cachedErrorException) + } + }, + ) .data(null) .errors(null) .build() @@ -224,9 +231,9 @@ internal object FetchPolicyRouterInterceptor : ApolloInterceptor { it.cacheInfo!!.newBuilder() .cacheMissException(exceptions.filterIsInstance().firstOrNull()) .networkException(exceptions.firstOrNull { it !is CacheMissException }) - .build() + .build(), ) - .build() + .build(), ) hasEmitted = true } @@ -248,9 +255,9 @@ internal object FetchPolicyRouterInterceptor : ApolloInterceptor { emit( ApolloResponse.Builder(request.operation, request.requestUuid) .exception(exception) - .build() + .build(), - ) + ) } } } diff --git a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/fetchPolicy.kt b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/fetchPolicy.kt index 31dd20e9..856bd152 100644 --- a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/fetchPolicy.kt +++ b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/fetchPolicy.kt @@ -25,7 +25,7 @@ internal val ApolloRequest.fetchPolicyInterceptor * This only has effects for queries. Mutations and subscriptions always use [FetchPolicy.NetworkOnly] */ fun MutableExecutionOptions.fetchPolicyInterceptor(interceptor: ApolloInterceptor) = addExecutionContext( - FetchPolicyContext(interceptor) + FetchPolicyContext(interceptor), ) /** @@ -45,11 +45,9 @@ fun MutableExecutionOptions.fetchPolicy(fetchPolicy: FetchPolicy): T { FetchPolicy.CacheOnly -> onlyIfCached(true) FetchPolicy.NetworkOnly -> noCache(true) FetchPolicy.CacheFirst -> this as T - @Suppress("DEPRECATION") FetchPolicy.CacheAndNetwork, -> { // CacheAndNetwork is deprecated but should still work - @Suppress("DEPRECATION") fetchPolicyInterceptor(CacheAndNetworkInterceptor) } } diff --git a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/refetchPolicy.kt b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/refetchPolicy.kt index 436bfb5b..607c2380 100644 --- a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/refetchPolicy.kt +++ b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/options/refetchPolicy.kt @@ -22,7 +22,7 @@ internal val MutableExecutionOptions.refetchPolicyInterceptor * Sets the [FetchPolicy] used when watching queries and a cache change has been published */ fun MutableExecutionOptions.refetchPolicyInterceptor(interceptor: ApolloInterceptor) = addExecutionContext( - RefetchPolicyContext(interceptor) + RefetchPolicyContext(interceptor), ) /** @@ -41,11 +41,8 @@ fun MutableExecutionOptions.refetchPolicy(fetchPolicy: FetchPolicy): T { FetchPolicy.CacheOnly -> refetchOnlyIfCached(true) FetchPolicy.NetworkOnly -> refetchNoCache(true) FetchPolicy.CacheFirst -> this as T - @Suppress("DEPRECATION") FetchPolicy.CacheAndNetwork, -> { - // CacheAndNetwork is deprecated but should still work - @Suppress("DEPRECATION") refetchPolicyInterceptor(CacheAndNetworkInterceptor) } } From 683be365446bd5c741788c7e9dde8bf58aaabbe4 Mon Sep 17 00:00:00 2001 From: BoD Date: Wed, 5 Nov 2025 09:41:48 +0100 Subject: [PATCH 2/2] Tweak warning --- .../kotlin/com/apollographql/cache/normalized/FetchPolicy.kt | 4 ++-- .../apollographql/cache/normalized/FetchPolicyInterceptors.kt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt index 8a3c62c1..2b4609eb 100644 --- a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt +++ b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicy.kt @@ -28,8 +28,8 @@ enum class FetchPolicy { /** * Emit the response from the cache first, and then emit the response(s) from the network. * - * Warning: this can emit multiple successful responses, which is not allowed by [ApolloCall.execute] and will - * crash. Use only with [ApolloCall.toFlow] or [ApolloCall.watch]. + * Warning: this can emit multiple successful responses, therefore [ApolloCall.execute] should not be used with this fetch policy. + * Use only with [ApolloCall.toFlow] or [ApolloCall.watch]. */ CacheAndNetwork, } diff --git a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt index af934fcd..4f1590d3 100644 --- a/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt +++ b/normalized-cache/src/commonMain/kotlin/com/apollographql/cache/normalized/FetchPolicyInterceptors.kt @@ -107,8 +107,8 @@ val NetworkFirstInterceptor = object : ApolloInterceptor { /** * An interceptor that emits the response from the cache first, and then emits the response(s) from the network. * - * Warning: this can emit multiple successful responses, which is not allowed by [ApolloCall.execute] and will - * crash. Use only with [ApolloCall.toFlow] or [ApolloCall.watch]. + * Warning: this can emit multiple successful responses, therefore [ApolloCall.execute] should not be used with this fetch policy. + * Use only with [ApolloCall.toFlow] or [ApolloCall.watch]. * */ val CacheAndNetworkInterceptor = object : ApolloInterceptor {