-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove existing interceptors from ApolloClient.Builder before adding …
…new ones (#5858) * Remove existing ApolloInterceptors when calling ApolloClient.Builder.httpCache() * Remove existing ApolloInterceptors when calling ApolloClient.Builder.autoPersistedQueries() * Remove existing HttpInterceptors when calling ApolloClient.Builder.httpCache() * Remove existing HttpInterceptors when calling ApolloClient.Builder.httpBatching() * Add a test for httpCache * Don't crash if no `X-APOLLO-SERVED-DATE` header is present * Revert CachingHttpInterceptor change
- Loading branch information
Showing
7 changed files
with
177 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
.../main/kotlin/com/apollographql/apollo3/cache/http/internal/CacheHeadersHttpInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.apollographql.apollo3.cache.http.internal | ||
|
||
import com.apollographql.apollo3.api.http.HttpRequest | ||
import com.apollographql.apollo3.api.http.HttpResponse | ||
import com.apollographql.apollo3.api.http.valueOf | ||
import com.apollographql.apollo3.cache.http.CachingHttpInterceptor | ||
import com.apollographql.apollo3.network.http.HttpInterceptor | ||
import com.apollographql.apollo3.network.http.HttpInterceptorChain | ||
|
||
internal class CacheHeadersHttpInterceptor(private val apolloRequestToCacheKey: MutableMap<String, String>) : HttpInterceptor { | ||
override suspend fun intercept(request: HttpRequest, chain: HttpInterceptorChain): HttpResponse { | ||
val cacheKey = CachingHttpInterceptor.cacheKey(request) | ||
val requestUuid = request.headers.valueOf(CachingHttpInterceptor.REQUEST_UUID_HEADER)!! | ||
synchronized(apolloRequestToCacheKey) { | ||
apolloRequestToCacheKey[requestUuid] = cacheKey | ||
} | ||
return chain.proceed( | ||
request.newBuilder() | ||
.headers(request.headers.filterNot { it.name == CachingHttpInterceptor.REQUEST_UUID_HEADER }) | ||
.addHeader(CachingHttpInterceptor.CACHE_KEY_HEADER, cacheKey) | ||
.build() | ||
) | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...c/main/kotlin/com/apollographql/apollo3/cache/http/internal/HttpCacheApolloInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.apollographql.apollo3.cache.http.internal | ||
|
||
import com.apollographql.apollo3.api.ApolloRequest | ||
import com.apollographql.apollo3.api.ApolloResponse | ||
import com.apollographql.apollo3.api.Mutation | ||
import com.apollographql.apollo3.api.Operation | ||
import com.apollographql.apollo3.api.Query | ||
import com.apollographql.apollo3.api.Subscription | ||
import com.apollographql.apollo3.cache.http.CachingHttpInterceptor | ||
import com.apollographql.apollo3.cache.http.HttpFetchPolicy | ||
import com.apollographql.apollo3.cache.http.HttpFetchPolicyContext | ||
import com.apollographql.apollo3.interceptor.ApolloInterceptor | ||
import com.apollographql.apollo3.interceptor.ApolloInterceptorChain | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.onCompletion | ||
import kotlinx.coroutines.flow.onEach | ||
import java.io.IOException | ||
|
||
internal class HttpCacheApolloInterceptor( | ||
private val apolloRequestToCacheKey: MutableMap<String, String>, | ||
private val cachingHttpInterceptor: CachingHttpInterceptor, | ||
) : ApolloInterceptor { | ||
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> { | ||
val policy = getPolicy(request) | ||
val policyStr = when (policy) { | ||
HttpFetchPolicy.CacheFirst -> CachingHttpInterceptor.CACHE_FIRST | ||
HttpFetchPolicy.CacheOnly -> CachingHttpInterceptor.CACHE_ONLY | ||
HttpFetchPolicy.NetworkFirst -> CachingHttpInterceptor.NETWORK_FIRST | ||
HttpFetchPolicy.NetworkOnly -> CachingHttpInterceptor.NETWORK_ONLY | ||
} | ||
|
||
return chain.proceed( | ||
request.newBuilder() | ||
.addHttpHeader( | ||
CachingHttpInterceptor.CACHE_OPERATION_TYPE_HEADER, | ||
when (request.operation) { | ||
is Query<*> -> "query" | ||
is Mutation<*> -> "mutation" | ||
is Subscription<*> -> "subscription" | ||
else -> error("Unknown operation type") | ||
} | ||
) | ||
.addHttpHeader(CachingHttpInterceptor.CACHE_FETCH_POLICY_HEADER, policyStr) | ||
.addHttpHeader(CachingHttpInterceptor.REQUEST_UUID_HEADER, request.requestUuid.toString()) | ||
.addHttpHeader(CachingHttpInterceptor.OPERATION_NAME_HEADER, request.operation.name()) | ||
.build() | ||
) | ||
.run { | ||
if (request.operation is Query<*>) { | ||
onEach { response -> | ||
// Revert caching of responses with errors | ||
val cacheKey = synchronized(apolloRequestToCacheKey) { apolloRequestToCacheKey[request.requestUuid.toString()] } | ||
if (response.hasErrors() || response.exception != null) { | ||
try { | ||
cacheKey?.let { cachingHttpInterceptor.cache.remove(it) } | ||
} catch (_: IOException) { | ||
} | ||
} | ||
}.onCompletion { | ||
synchronized(apolloRequestToCacheKey) { apolloRequestToCacheKey.remove(request.requestUuid.toString()) } | ||
} | ||
} else { | ||
this | ||
} | ||
} | ||
} | ||
|
||
private fun getPolicy(request: ApolloRequest<*>): HttpFetchPolicy { | ||
return if (request.operation is Mutation<*>) { | ||
// Don't cache mutations | ||
HttpFetchPolicy.NetworkOnly | ||
} else { | ||
request.executionContext[HttpFetchPolicyContext]?.httpFetchPolicy ?: HttpFetchPolicy.CacheFirst | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters