Skip to content

Commit

Permalink
Report changes to the incubating artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Apr 29, 2024
1 parent 23b6809 commit 496c094
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.apollographql.apollo3.cache.normalized.api.Record
import com.apollographql.apollo3.cache.normalized.api.RecordMerger
import com.apollographql.apollo3.cache.normalized.api.TypePolicyCacheKeyGenerator
import com.apollographql.apollo3.cache.normalized.internal.DefaultApolloStore
import com.apollographql.apollo3.interceptor.ApolloInterceptor
import com.benasher44.uuid.Uuid
import kotlinx.coroutines.flow.SharedFlow
import kotlin.reflect.KClass
Expand Down Expand Up @@ -235,3 +236,8 @@ fun ApolloStore(
fieldNameGenerator = fieldNameGenerator,
embeddedFieldsProvider = embeddedFieldsProvider,
)

/**
* Interface that marks all interceptors added when configuring a `store()` on ApolloClient.Builder.
*/
internal interface ApolloStoreInterceptor : ApolloInterceptor
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ fun ApolloClient.Builder.store(
check(interceptors.none { it is AutoPersistedQueryInterceptor }) {
"Apollo: the normalized cache must be configured before the auto persisted queries"
}
// Removing existing interceptors added for configuring an [ApolloStore].
// 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
// remove the old interceptors.
val storeInterceptors = interceptors.filterIsInstance<ApolloStoreInterceptor>()
storeInterceptors.forEach {
removeInterceptor(it)
}
return addInterceptor(WatcherInterceptor(store))
.addInterceptor(FetchPolicyRouterInterceptor)
.addInterceptor(ApolloCacheInterceptor(store))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ val CacheAndNetworkInterceptor = object : ApolloInterceptor {
}
}

internal object FetchPolicyRouterInterceptor : ApolloInterceptor {
internal object FetchPolicyRouterInterceptor : ApolloInterceptor, ApolloStoreInterceptor {
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
if (request.operation !is Query) {
// Subscriptions and Mutations do not support fetchPolicies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.Query
import com.apollographql.apollo3.api.Subscription
import com.apollographql.apollo3.cache.normalized.ApolloStore
import com.apollographql.apollo3.cache.normalized.ApolloStoreInterceptor
import com.apollographql.apollo3.cache.normalized.CacheInfo
import com.apollographql.apollo3.cache.normalized.api.ApolloCacheHeaders
import com.apollographql.apollo3.cache.normalized.api.CacheHeaders
Expand Down Expand Up @@ -36,7 +37,7 @@ import kotlinx.coroutines.launch

internal class ApolloCacheInterceptor(
val store: ApolloStore,
) : ApolloInterceptor {
) : ApolloInterceptor, ApolloStoreInterceptor {
private suspend fun <D : Operation.Data> maybeAsync(request: ApolloRequest<D>, block: suspend () -> Unit) {
if (request.writeToCacheAsynchronously) {
val scope = request.executionContext[ConcurrencyInfo]!!.coroutineScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.apollographql.apollo3.api.CustomScalarAdapters
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.Query
import com.apollographql.apollo3.cache.normalized.ApolloStore
import com.apollographql.apollo3.cache.normalized.ApolloStoreInterceptor
import com.apollographql.apollo3.cache.normalized.api.dependentKeys
import com.apollographql.apollo3.cache.normalized.watchContext
import com.apollographql.apollo3.interceptor.ApolloInterceptor
Expand All @@ -17,7 +18,7 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach

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

Expand Down

0 comments on commit 496c094

Please sign in to comment.