Skip to content

ApolloClient.apolloStore -> ApolloClient.store #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Next version (unreleased)

- Records are now rooted per operation type (QUERY_ROOT, MUTATION_ROOT, SUBSCRIPTION_ROOT) (#109)
- `ApolloClient.apolloStore` is deprecated in favor of `ApolloClient.store` for consistency.

# Version 0.0.8
_2025-03-28_
Expand Down
1 change: 1 addition & 0 deletions Writerside/topics/migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ store.writeOperation(operation, data).also { store.publish(it) }

- `readFragment()` now returns a `ReadResult<D>` (it previously returned a `<D>`). This allows for surfacing metadata associated to the returned data, e.g. staleness.
- Records are now rooted per operation type (`QUERY_ROOT`, `MUTATION_ROOT`, `SUBSCRIPTION_ROOT`), when previously these were all at the same level, which could cause conflicts.
- `ApolloClient.apolloStore` is deprecated in favor of `ApolloClient.store` for consistency.

## CacheResolver, CacheKeyResolver

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public final class com/apollographql/cache/normalized/NormalizedCache {
public static final fun getCacheHeaders (Lcom/apollographql/apollo/api/ApolloResponse;)Lcom/apollographql/cache/normalized/api/CacheHeaders;
public static final fun getCacheInfo (Lcom/apollographql/apollo/api/ApolloResponse;)Lcom/apollographql/cache/normalized/CacheInfo;
public static final fun getFetchFromCache (Lcom/apollographql/apollo/api/ApolloRequest;)Z
public static final fun getStore (Lcom/apollographql/apollo/ApolloClient;)Lcom/apollographql/cache/normalized/ApolloStore;
public static final fun isFromCache (Lcom/apollographql/apollo/api/ApolloResponse;)Z
public static final fun maxStale-HG0u8IE (Lcom/apollographql/apollo/api/MutableExecutionOptions;J)Ljava/lang/Object;
public static final fun memoryCacheOnly (Lcom/apollographql/apollo/api/MutableExecutionOptions;Z)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ final val com.apollographql.cache.normalized/fetchFromCache // com.apollographql
final fun <#A1: com.apollographql.apollo.api/Operation.Data> (com.apollographql.apollo.api/ApolloRequest<#A1>).<get-fetchFromCache>(): kotlin/Boolean // com.apollographql.cache.normalized/fetchFromCache.<get-fetchFromCache>|<get-fetchFromCache>@com.apollographql.apollo.api.ApolloRequest<0:0>(){0§<com.apollographql.apollo.api.Operation.Data>}[0]
final val com.apollographql.cache.normalized/isFromCache // com.apollographql.cache.normalized/isFromCache|@com.apollographql.apollo.api.ApolloResponse<0:0>{0§<com.apollographql.apollo.api.Operation.Data>}isFromCache[0]
final fun <#A1: com.apollographql.apollo.api/Operation.Data> (com.apollographql.apollo.api/ApolloResponse<#A1>).<get-isFromCache>(): kotlin/Boolean // com.apollographql.cache.normalized/isFromCache.<get-isFromCache>|<get-isFromCache>@com.apollographql.apollo.api.ApolloResponse<0:0>(){0§<com.apollographql.apollo.api.Operation.Data>}[0]
final val com.apollographql.cache.normalized/store // com.apollographql.cache.normalized/store|@com.apollographql.apollo.ApolloClient{}store[0]
final fun (com.apollographql.apollo/ApolloClient).<get-store>(): com.apollographql.cache.normalized/ApolloStore // com.apollographql.cache.normalized/store.<get-store>|<get-store>@com.apollographql.apollo.ApolloClient(){}[0]

final fun (com.apollographql.apollo/ApolloClient.Builder).com.apollographql.cache.normalized/logCacheMisses(kotlin/Function1<kotlin/String, kotlin/Unit> = ...): com.apollographql.apollo/ApolloClient.Builder // com.apollographql.cache.normalized/logCacheMisses|logCacheMisses@com.apollographql.apollo.ApolloClient.Builder(kotlin.Function1<kotlin.String,kotlin.Unit>){}[0]
final fun (com.apollographql.apollo/ApolloClient.Builder).com.apollographql.cache.normalized/normalizedCache(com.apollographql.cache.normalized.api/NormalizedCacheFactory, com.apollographql.cache.normalized.api/CacheKeyGenerator = ..., com.apollographql.cache.normalized.api/MetadataGenerator = ..., com.apollographql.cache.normalized.api/CacheResolver = ..., com.apollographql.cache.normalized.api/RecordMerger = ..., com.apollographql.cache.normalized.api/FieldKeyGenerator = ..., com.apollographql.cache.normalized.api/EmbeddedFieldsProvider = ..., com.apollographql.cache.normalized.api/MaxAgeProvider = ..., kotlin/Boolean = ...): com.apollographql.apollo/ApolloClient.Builder // com.apollographql.cache.normalized/normalizedCache|normalizedCache@com.apollographql.apollo.ApolloClient.Builder(com.apollographql.cache.normalized.api.NormalizedCacheFactory;com.apollographql.cache.normalized.api.CacheKeyGenerator;com.apollographql.cache.normalized.api.MetadataGenerator;com.apollographql.cache.normalized.api.CacheResolver;com.apollographql.cache.normalized.api.RecordMerger;com.apollographql.cache.normalized.api.FieldKeyGenerator;com.apollographql.cache.normalized.api.EmbeddedFieldsProvider;com.apollographql.cache.normalized.api.MaxAgeProvider;kotlin.Boolean){}[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,18 @@ internal fun <D : Query.Data> ApolloCall<D>.watchInternal(data: D?): Flow<Apollo
return copy().addExecutionContext(WatchContext(data)).toFlow()
}

@Deprecated("Use store instead", ReplaceWith("store"))
val ApolloClient.apolloStore: ApolloStore
get() = store

val ApolloClient.store: ApolloStore
get() {
return interceptors.firstOrNull { it is ApolloCacheInterceptor }?.let {
(it as ApolloCacheInterceptor).store
} ?: error("no cache configured")
}


/**
* Sets the initial [FetchPolicy]
* This only has effects for queries. Mutations and subscriptions always use [FetchPolicy.NetworkOnly]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.apollographql.cache.normalized.api.GlobalMaxAgeProvider
import com.apollographql.cache.normalized.api.MaxAge
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
import com.apollographql.cache.normalized.api.SchemaCoordinatesMaxAgeProvider
import com.apollographql.cache.normalized.apolloStore
import com.apollographql.cache.normalized.cacheHeaders
import com.apollographql.cache.normalized.cacheInfo
import com.apollographql.cache.normalized.fetchPolicy
Expand All @@ -20,6 +19,7 @@ import com.apollographql.cache.normalized.maxStale
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
import com.apollographql.cache.normalized.normalizedCache
import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory
import com.apollographql.cache.normalized.store
import com.apollographql.cache.normalized.storeExpirationDate
import com.apollographql.cache.normalized.testing.runTest
import com.apollographql.mockserver.MockResponse
Expand Down Expand Up @@ -69,7 +69,7 @@ class ClientAndServerSideCacheControlTest {
.storeExpirationDate(true)
.serverUrl(mockServer.url())
.build()
client.apolloStore.clearAll()
client.store.clearAll()

val data = """
{
Expand Down Expand Up @@ -148,7 +148,7 @@ class ClientAndServerSideCacheControlTest {
.storeExpirationDate(true)
.serverUrl(mockServer.url())
.build()
client.apolloStore.clearAll()
client.store.clearAll()

val data = """
{
Expand Down Expand Up @@ -220,7 +220,7 @@ class ClientAndServerSideCacheControlTest {
)
.serverUrl(mockServer.url())
.build()
client.apolloStore.clearAll()
client.store.clearAll()

val data = """
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import com.apollographql.cache.normalized.api.GlobalMaxAgeProvider
import com.apollographql.cache.normalized.api.MaxAge
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
import com.apollographql.cache.normalized.api.SchemaCoordinatesMaxAgeProvider
import com.apollographql.cache.normalized.apolloStore
import com.apollographql.cache.normalized.fetchPolicy
import com.apollographql.cache.normalized.internal.normalized
import com.apollographql.cache.normalized.maxStale
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
import com.apollographql.cache.normalized.normalizedCache
import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory
import com.apollographql.cache.normalized.store
import com.apollographql.cache.normalized.testing.runTest
import programmatic.GetCompanyQuery
import programmatic.GetUserAdminQuery
Expand Down Expand Up @@ -85,14 +85,14 @@ class ClientSideCacheControlTest {
)
.serverUrl("unused")
.build()
client.apolloStore.clearAll()
client.store.clearAll()

val query = GetUserQuery()
val data = GetUserQuery.Data(GetUserQuery.User("John", "john@doe.com", true))

val records = data.normalized(query).values

client.apolloStore.accessCache {
client.store.accessCache {
// store records in the past
it.merge(records, receivedDate(currentTimeSeconds() - 15), DefaultRecordMerger)
}
Expand All @@ -106,7 +106,7 @@ class ClientSideCacheControlTest {
.execute()
assertTrue(response1.data?.user?.name == "John")

client.apolloStore.accessCache {
client.store.accessCache {
// update records to be in the present
it.merge(records, receivedDate(currentTimeSeconds()), DefaultRecordMerger)
}
Expand All @@ -132,7 +132,7 @@ class ClientSideCacheControlTest {
)
.serverUrl("unused")
.build()
client.apolloStore.clearAll()
client.store.clearAll()

// Store records 25 seconds ago, more than default max age: should cache miss
mergeCompanyQueryResults(client, 25)
Expand Down Expand Up @@ -182,7 +182,7 @@ class ClientSideCacheControlTest {
private fun mergeCompanyQueryResults(client: ApolloClient, secondsAgo: Int) {
val data = GetCompanyQuery.Data(GetCompanyQuery.Company("42"))
val records = data.normalized(GetCompanyQuery()).values
client.apolloStore.accessCache {
client.store.accessCache {
it.merge(records, receivedDate(currentTimeSeconds() - secondsAgo), DefaultRecordMerger)
}
}
Expand All @@ -200,7 +200,7 @@ class ClientSideCacheControlTest {
)
.serverUrl("unused")
.build()
client.apolloStore.clearAll()
client.store.clearAll()

// Store records 25 seconds ago, more than default max age: should cache miss
mergeCompanyQueryResults(client, 25)
Expand Down Expand Up @@ -260,15 +260,15 @@ class ClientSideCacheControlTest {
private fun mergeUserQueryResults(client: ApolloClient, secondsAgo: Int) {
val data = GetUserQuery.Data(GetUserQuery.User("John", "john@doe.com", true))
val records = data.normalized(GetUserQuery()).values
client.apolloStore.accessCache {
client.store.accessCache {
it.merge(records, receivedDate(currentTimeSeconds() - secondsAgo), DefaultRecordMerger)
}
}

private fun mergeProjectQueryResults(client: ApolloClient, secondsAgo: Int) {
val data = declarative.GetProjectQuery.Data(declarative.GetProjectQuery.Project("42", "Stardust"))
val records = data.normalized(declarative.GetProjectQuery()).values
client.apolloStore.accessCache {
client.store.accessCache {
it.merge(records, receivedDate(currentTimeSeconds() - secondsAgo), DefaultRecordMerger)
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/cache-control/src/commonTest/kotlin/DoNotStoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import com.apollographql.cache.normalized.api.FieldKeyContext
import com.apollographql.cache.normalized.api.FieldKeyGenerator
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
import com.apollographql.cache.normalized.api.SchemaCoordinatesMaxAgeProvider
import com.apollographql.cache.normalized.apolloStore
import com.apollographql.cache.normalized.fetchFromCache
import com.apollographql.cache.normalized.fetchPolicy
import com.apollographql.cache.normalized.fetchPolicyInterceptor
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
import com.apollographql.cache.normalized.normalizedCache
import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory
import com.apollographql.cache.normalized.store
import com.apollographql.cache.normalized.testing.append
import com.apollographql.cache.normalized.testing.assertErrorsEquals
import com.apollographql.cache.normalized.testing.keyToString
Expand Down Expand Up @@ -99,7 +99,7 @@ class DoNotStoreTest {
)
.build()
.use { apolloClient ->
apolloClient.apolloStore.clearAll()
apolloClient.store.clearAll()
val networkResponse = apolloClient.query(GetUserQuery())
.fetchPolicy(FetchPolicy.NetworkOnly)
.execute()
Expand Down Expand Up @@ -195,7 +195,7 @@ class DoNotStoreTest {
)
.build()
.use { apolloClient ->
apolloClient.apolloStore.clearAll()
apolloClient.store.clearAll()
val networkResponse = apolloClient.mutation(SignInMutation("scott", "tiger"))
.fetchPolicy(FetchPolicy.NetworkOnly)
.execute()
Expand All @@ -221,7 +221,7 @@ class DoNotStoreTest {
networkResponse.data
)

apolloClient.apolloStore.accessCache { cache ->
apolloClient.store.accessCache { cache ->
val authRecord = cache.loadRecord(CacheKey.MUTATION_ROOT.append("auth"), CacheHeaders.NONE)!!
// No password in field key
assertContentEquals(listOf("signIn"), authRecord.fields.keys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import com.apollographql.apollo.exception.CacheMissException
import com.apollographql.cache.normalized.FetchPolicy
import com.apollographql.cache.normalized.api.CacheControlCacheResolver
import com.apollographql.cache.normalized.api.NormalizedCacheFactory
import com.apollographql.cache.normalized.apolloStore
import com.apollographql.cache.normalized.fetchPolicy
import com.apollographql.cache.normalized.maxStale
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
import com.apollographql.cache.normalized.normalizedCache
import com.apollographql.cache.normalized.sql.SqlNormalizedCacheFactory
import com.apollographql.cache.normalized.store
import com.apollographql.cache.normalized.storeExpirationDate
import com.apollographql.cache.normalized.testing.runTest
import com.apollographql.mockserver.MockResponse
Expand Down Expand Up @@ -48,7 +48,7 @@ class ServerSideCacheControlTest {
.storeExpirationDate(true)
.serverUrl(mockServer.url())
.build()
client.apolloStore.clearAll()
client.store.clearAll()

val query = GetUserQuery()
val data = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package test
import cache.include.GetUserQuery
import com.apollographql.apollo.ApolloClient
import com.apollographql.cache.normalized.FetchPolicy
import com.apollographql.cache.normalized.apolloStore
import com.apollographql.cache.normalized.fetchPolicy
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
import com.apollographql.cache.normalized.normalizedCache
import com.apollographql.cache.normalized.store
import com.apollographql.cache.normalized.testing.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -25,7 +25,7 @@ class IncludeTest {
user = GetUserQuery.User(__typename = "User", id = "42", userDetails = null)
)

client.apolloStore.writeOperation(operation, data)
client.store.writeOperation(operation, data)

val response = client.query(operation).fetchPolicy(FetchPolicy.CacheOnly).execute()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.apollographql.cache.normalized.ApolloStore
import com.apollographql.cache.normalized.FetchPolicy
import com.apollographql.cache.normalized.api.CacheHeaders
import com.apollographql.cache.normalized.api.CacheKey
import com.apollographql.cache.normalized.apolloStore
import com.apollographql.cache.normalized.fetchPolicy
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
import com.apollographql.cache.normalized.optimisticUpdates
Expand Down Expand Up @@ -543,7 +542,7 @@ class DeferNormalizedCacheTest {
val multipartBody = mockServer.enqueueMultipart("application/json")
multipartBody.enqueuePart(jsonList[0].encodeUtf8(), false)
val recordFields = apolloClient.query(SimpleDeferQuery()).fetchPolicy(FetchPolicy.NetworkOnly).toFlow().map {
apolloClient.apolloStore.accessCache { it.loadRecord(CacheKey("computers").append("0"), CacheHeaders.NONE)!!.fields }.also {
apolloClient.store.accessCache { it.loadRecord(CacheKey("computers").append("0"), CacheHeaders.NONE)!!.fields }.also {
multipartBody.enqueuePart(jsonList[1].encodeUtf8(), true)
}
}.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package test.fragmentnormalizer
import com.apollographql.apollo.ApolloClient
import com.apollographql.cache.normalized.api.CacheKey
import com.apollographql.cache.normalized.api.IdCacheKeyGenerator
import com.apollographql.cache.normalized.apolloStore
import com.apollographql.cache.normalized.internal.normalized
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
import com.apollographql.cache.normalized.normalizedCache
import com.apollographql.cache.normalized.store
import com.apollographql.cache.normalized.testing.append
import com.apollographql.cache.normalized.testing.runTest
import fragmentnormalizer.fragment.ConversationFragment
Expand Down Expand Up @@ -61,19 +61,19 @@ class FragmentNormalizerTest {
),
true
)
apolloClient.apolloStore.writeFragment(
apolloClient.store.writeFragment(
ConversationFragmentImpl(),
CacheKey(fragment1.id),
fragment1Read,
)

apolloClient.apolloStore.writeFragment(
apolloClient.store.writeFragment(
ConversationFragmentImpl(),
CacheKey(fragment2.id),
fragment2Read,
)

fragment1 = apolloClient.apolloStore.readFragment(
fragment1 = apolloClient.store.readFragment(
ConversationFragmentImpl(),
CacheKey(fragment1.id),
).data
Expand Down
6 changes: 3 additions & 3 deletions tests/pagination/src/commonTest/kotlin/EmbedTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.apollographql.apollo.ApolloClient
import com.apollographql.cache.normalized.api.CacheKey
import com.apollographql.cache.normalized.api.CacheKeyGenerator
import com.apollographql.cache.normalized.api.CacheKeyGeneratorContext
import com.apollographql.cache.normalized.apolloStore
import com.apollographql.cache.normalized.internal.normalized
import com.apollographql.cache.normalized.memory.MemoryCacheFactory
import com.apollographql.cache.normalized.normalizedCache
import com.apollographql.cache.normalized.store
import com.apollographql.cache.normalized.testing.runTest
import embed.GetHeroQuery
import kotlin.test.Test
Expand Down Expand Up @@ -47,8 +47,8 @@ class EmbedTest {
listOf(GetHeroQuery.Friend("Luke", GetHeroQuery.Pet("Snoopy")), GetHeroQuery.Friend("Leia", GetHeroQuery.Pet("Fluffy")))
)
)
client.apolloStore.writeOperation(query, data)
val dataFromStore = client.apolloStore.readOperation(query).data
client.store.writeOperation(query, data)
val dataFromStore = client.store.readOperation(query).data
assertEquals(data, dataFromStore)
}
}
Loading