11package com.apollographql.cache.normalized
22
3+ import com.apollographql.apollo.ApolloCall
34import com.apollographql.apollo.api.ApolloRequest
45import com.apollographql.apollo.api.ApolloResponse
56import 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)" )
109114val 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 }
0 commit comments