Skip to content

Commit a8737d1

Browse files
rohandhruvaBoD
andauthored
Clear current ApolloStore related interceptors when calling .store() on builder (#5857)
* Clear old ApolloStore related interceptors when using .store * Update .api for removeInterceptor * Report changes to the incubating artifact --------- Co-authored-by: BoD <[email protected]>
1 parent 7d5c7c7 commit a8737d1

File tree

14 files changed

+63
-8
lines changed

14 files changed

+63
-8
lines changed

libraries/apollo-normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/ApolloStore.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.apollographql.apollo3.cache.normalized.api.Record
2424
import com.apollographql.apollo3.cache.normalized.api.RecordMerger
2525
import com.apollographql.apollo3.cache.normalized.api.TypePolicyCacheKeyGenerator
2626
import com.apollographql.apollo3.cache.normalized.internal.DefaultApolloStore
27+
import com.apollographql.apollo3.interceptor.ApolloInterceptor
2728
import com.benasher44.uuid.Uuid
2829
import kotlinx.coroutines.flow.SharedFlow
2930
import kotlin.reflect.KClass
@@ -235,3 +236,8 @@ fun ApolloStore(
235236
fieldNameGenerator = fieldNameGenerator,
236237
embeddedFieldsProvider = embeddedFieldsProvider,
237238
)
239+
240+
/**
241+
* Interface that marks all interceptors added when configuring a `store()` on ApolloClient.Builder.
242+
*/
243+
internal interface ApolloStoreInterceptor : ApolloInterceptor

libraries/apollo-normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/ClientCacheExtensions.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ fun ApolloClient.Builder.store(
159159
check(interceptors.none { it is AutoPersistedQueryInterceptor }) {
160160
"Apollo: the normalized cache must be configured before the auto persisted queries"
161161
}
162+
// Removing existing interceptors added for configuring an [ApolloStore].
163+
// If a builder is reused from an existing client using `newBuilder()` and we try to configure a new `store()` on it, we first need to
164+
// remove the old interceptors.
165+
val storeInterceptors = interceptors.filterIsInstance<ApolloStoreInterceptor>()
166+
storeInterceptors.forEach {
167+
removeInterceptor(it)
168+
}
162169
return addInterceptor(WatcherInterceptor(store))
163170
.addInterceptor(FetchPolicyRouterInterceptor)
164171
.addInterceptor(ApolloCacheInterceptor(store))

libraries/apollo-normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/FetchPolicyInterceptors.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ val CacheAndNetworkInterceptor = object : ApolloInterceptor {
129129
}
130130
}
131131

132-
internal object FetchPolicyRouterInterceptor : ApolloInterceptor {
132+
internal object FetchPolicyRouterInterceptor : ApolloInterceptor, ApolloStoreInterceptor {
133133
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
134134
if (request.operation !is Query) {
135135
// Subscriptions and Mutations do not support fetchPolicies

libraries/apollo-normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/internal/ApolloCacheInterceptor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.apollographql.apollo3.api.Operation
99
import com.apollographql.apollo3.api.Query
1010
import com.apollographql.apollo3.api.Subscription
1111
import com.apollographql.apollo3.cache.normalized.ApolloStore
12+
import com.apollographql.apollo3.cache.normalized.ApolloStoreInterceptor
1213
import com.apollographql.apollo3.cache.normalized.CacheInfo
1314
import com.apollographql.apollo3.cache.normalized.api.ApolloCacheHeaders
1415
import com.apollographql.apollo3.cache.normalized.api.CacheHeaders
@@ -36,7 +37,7 @@ import kotlinx.coroutines.launch
3637

3738
internal class ApolloCacheInterceptor(
3839
val store: ApolloStore,
39-
) : ApolloInterceptor {
40+
) : ApolloInterceptor, ApolloStoreInterceptor {
4041
private suspend fun <D : Operation.Data> maybeAsync(request: ApolloRequest<D>, block: suspend () -> Unit) {
4142
if (request.writeToCacheAsynchronously) {
4243
val scope = request.executionContext[ConcurrencyInfo]!!.coroutineScope

libraries/apollo-normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/internal/WatcherInterceptor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.apollographql.apollo3.api.CustomScalarAdapters
66
import com.apollographql.apollo3.api.Operation
77
import com.apollographql.apollo3.api.Query
88
import com.apollographql.apollo3.cache.normalized.ApolloStore
9+
import com.apollographql.apollo3.cache.normalized.ApolloStoreInterceptor
910
import com.apollographql.apollo3.cache.normalized.api.dependentKeys
1011
import com.apollographql.apollo3.cache.normalized.watchContext
1112
import com.apollographql.apollo3.interceptor.ApolloInterceptor
@@ -17,7 +18,7 @@ import kotlinx.coroutines.flow.flow
1718
import kotlinx.coroutines.flow.map
1819
import kotlinx.coroutines.flow.onEach
1920

20-
internal class WatcherInterceptor(val store: ApolloStore) : ApolloInterceptor {
21+
internal class WatcherInterceptor(val store: ApolloStore) : ApolloInterceptor, ApolloStoreInterceptor {
2122
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
2223
val watchContext = request.watchContext ?: return chain.proceed(request)
2324

libraries/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/ApolloStore.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.apollographql.apollo3.cache.normalized.api.NormalizedCacheFactory
1313
import com.apollographql.apollo3.cache.normalized.api.Record
1414
import com.apollographql.apollo3.cache.normalized.api.TypePolicyCacheKeyGenerator
1515
import com.apollographql.apollo3.cache.normalized.internal.DefaultApolloStore
16+
import com.apollographql.apollo3.interceptor.ApolloInterceptor
1617
import com.benasher44.uuid.Uuid
1718
import kotlinx.coroutines.flow.SharedFlow
1819
import kotlin.reflect.KClass
@@ -265,3 +266,8 @@ fun ApolloStore(
265266
cacheKeyGenerator: CacheKeyGenerator = TypePolicyCacheKeyGenerator,
266267
cacheResolver: CacheResolver = FieldPolicyCacheResolver,
267268
): ApolloStore = DefaultApolloStore(normalizedCacheFactory, cacheKeyGenerator, cacheResolver)
269+
270+
/**
271+
* Interface that marks all interceptors added when configuring a `store()` on ApolloClient.Builder.
272+
*/
273+
internal interface ApolloStoreInterceptor : ApolloInterceptor

libraries/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/ClientCacheExtensions.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ fun ApolloClient.Builder.store(store: ApolloStore, writeToCacheAsynchronously: B
122122
check(interceptors.none { it is AutoPersistedQueryInterceptor }) {
123123
"Apollo: the normalized cache must be configured before the auto persisted queries"
124124
}
125+
// Removing existing interceptors added for configuring an [ApolloStore].
126+
// If a builder is reused from an existing client using `newBuilder()` and we try to configure a new `store()` on it, we first need to
127+
// remove the old interceptors.
128+
val storeInterceptors = interceptors.filterIsInstance<ApolloStoreInterceptor>()
129+
storeInterceptors.forEach {
130+
removeInterceptor(it)
131+
}
125132
return addInterceptor(WatcherInterceptor(store))
126133
.addInterceptor(FetchPolicyRouterInterceptor)
127134
.addInterceptor(ApolloCacheInterceptor(store))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ val CacheAndNetworkInterceptor = object : ApolloInterceptor {
129129
}
130130
}
131131

132-
internal object FetchPolicyRouterInterceptor : ApolloInterceptor {
132+
internal object FetchPolicyRouterInterceptor : ApolloInterceptor, ApolloStoreInterceptor {
133133
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
134134
if (request.operation !is Query) {
135135
// Subscriptions and Mutations do not support fetchPolicies

libraries/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/internal/ApolloCacheInterceptor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.apollographql.apollo3.api.Operation
99
import com.apollographql.apollo3.api.Query
1010
import com.apollographql.apollo3.api.Subscription
1111
import com.apollographql.apollo3.cache.normalized.ApolloStore
12+
import com.apollographql.apollo3.cache.normalized.ApolloStoreInterceptor
1213
import com.apollographql.apollo3.cache.normalized.CacheInfo
1314
import com.apollographql.apollo3.cache.normalized.api.ApolloCacheHeaders
1415
import com.apollographql.apollo3.cache.normalized.api.CacheHeaders
@@ -36,7 +37,7 @@ import kotlinx.coroutines.launch
3637

3738
internal class ApolloCacheInterceptor(
3839
val store: ApolloStore,
39-
) : ApolloInterceptor {
40+
) : ApolloInterceptor, ApolloStoreInterceptor {
4041
private suspend fun <D : Operation.Data> maybeAsync(request: ApolloRequest<D>, block: suspend () -> Unit) {
4142
if (request.writeToCacheAsynchronously) {
4243
val scope = request.executionContext[ConcurrencyInfo]!!.coroutineScope

libraries/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/internal/WatcherInterceptor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.apollographql.apollo3.api.CustomScalarAdapters
66
import com.apollographql.apollo3.api.Operation
77
import com.apollographql.apollo3.api.Query
88
import com.apollographql.apollo3.cache.normalized.ApolloStore
9+
import com.apollographql.apollo3.cache.normalized.ApolloStoreInterceptor
910
import com.apollographql.apollo3.cache.normalized.api.dependentKeys
1011
import com.apollographql.apollo3.cache.normalized.watchContext
1112
import com.apollographql.apollo3.exception.DefaultApolloException
@@ -23,7 +24,7 @@ import kotlinx.coroutines.flow.onSubscription
2324

2425
internal val WatcherSentinel = DefaultApolloException("The watcher has started")
2526

26-
internal class WatcherInterceptor(val store: ApolloStore) : ApolloInterceptor {
27+
internal class WatcherInterceptor(val store: ApolloStore) : ApolloInterceptor, ApolloStoreInterceptor {
2728
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
2829
val watchContext = request.watchContext ?: return chain.proceed(request)
2930

0 commit comments

Comments
 (0)