ApolloStore

class ApolloStore(val cacheManager: CacheManager, val customScalarAdapters: CustomScalarAdapters)

A wrapper around CacheManager that provides a simplified API for reading and writing data.

Although all operations are suspend functions, they may suspend or block the thread depending on the underlying cache implementation. For example, the SQL cache implementation on Android will block the thread while accessing the disk. As such, these operations must not run on the main thread. You can enclose them in a kotlinx.coroutines.withContext block with a Dispatchers.IO context to ensure that they run on a background thread.

Note that changes are not automatically published - pass publish = true or call publish to notify any watchers.

Constructors

Link copied to clipboard
constructor(cacheManager: CacheManager, customScalarAdapters: CustomScalarAdapters)

Properties

Link copied to clipboard
Link copied to clipboard
val changedKeys: SharedFlow<Set<String>>

Exposes the record field keys that have changed. This is collected internally to notify watchers.

Link copied to clipboard
val customScalarAdapters: CustomScalarAdapters

Functions

Link copied to clipboard
suspend fun <R> accessCache(block: suspend (NormalizedCache) -> R): R

Direct access to the cache.

Link copied to clipboard
suspend fun clearAll(publish: Boolean = false): Boolean

Clears all records.

Link copied to clipboard
fun dispose()

Releases resources associated with this store.

Link copied to clipboard
suspend fun dump(): Map<KClass<*>, Map<CacheKey, Record>>

Dumps the content of the store for debugging purposes.

Link copied to clipboard
suspend fun ApolloStore.garbageCollect(maxAgeProvider: MaxAgeProvider, maxStale: Duration = Duration.ZERO): GarbageCollectResult

Perform garbage collection on the store.

Link copied to clipboard
fun <D : Executable.Data> normalize(executable: Executable<D>, dataWithErrors: DataWithErrors, rootKey: CacheKey = CacheKey.QUERY_ROOT): Map<CacheKey, Record>

Normalizes executable data to a map of Record keyed by Record.key.

Link copied to clipboard
suspend fun publish(keys: Set<String>)

Publishes a set of keys of record fields that have changed. This will notify watchers and any subscribers of changedKeys.

Link copied to clipboard
suspend fun <D : Fragment.Data> readFragment(fragment: Fragment<D>, cacheKey: CacheKey, cacheHeaders: CacheHeaders = CacheHeaders.NONE): CacheManager.ReadResult<D>

Reads a fragment from the store.

Link copied to clipboard
suspend fun <D : Operation.Data> readOperation(operation: Operation<D>, cacheHeaders: CacheHeaders = CacheHeaders.NONE): ApolloResponse<D>

Reads an operation from the store.

Link copied to clipboard
suspend fun remove(cacheKey: CacheKey, cascade: Boolean = true): Boolean

Removes a record by its key.

suspend fun remove(cacheKeys: List<CacheKey>, cascade: Boolean = true): Int

Removes a list of records by their keys. This is an optimized version of remove for caches that can batch operations.

Link copied to clipboard

Remove all dangling references in the store.

Link copied to clipboard
suspend fun <D : Fragment.Data> ApolloStore.removeFragment(fragment: Fragment<D>, cacheKey: CacheKey, data: D, cacheHeaders: CacheHeaders = CacheHeaders.NONE, publish: Boolean = false): Set<String>

Removes a fragment from the store.

Link copied to clipboard
suspend fun <D : Operation.Data> ApolloStore.removeOperation(operation: Operation<D>, data: D, cacheHeaders: CacheHeaders = CacheHeaders.NONE, publish: Boolean = false): Set<String>

Removes an operation from the store.

Link copied to clipboard
suspend fun ApolloStore.removeStaleFields(maxAgeProvider: MaxAgeProvider, maxStale: Duration = Duration.ZERO): RemovedFieldsAndRecords

Remove all stale fields in the store.

Link copied to clipboard

Remove all unreachable records in the store.

Link copied to clipboard
suspend fun rollbackOptimisticUpdates(mutationId: Uuid, publish: Boolean = false): Set<String>

Rollbacks optimistic updates.

Link copied to clipboard
suspend fun trim(maxSizeBytes: Long, trimFactor: Float = 0.1f): Long

Trims the store if its size exceeds maxSizeBytes. The amount of data to remove is determined by trimFactor. The oldest records are removed according to their updated date.

Link copied to clipboard
suspend fun <D : Fragment.Data> writeFragment(fragment: Fragment<D>, cacheKey: CacheKey, data: D, cacheHeaders: CacheHeaders = CacheHeaders.NONE, publish: Boolean = false): Set<String>

Writes a fragment to the store.

Link copied to clipboard
suspend fun <D : Operation.Data> writeOperation(operation: Operation<D>, dataWithErrors: DataWithErrors, cacheHeaders: CacheHeaders = CacheHeaders.NONE, publish: Boolean = false): Set<String>
suspend fun <D : Operation.Data> writeOperation(operation: Operation<D>, data: D, errors: List<Error>? = null, cacheHeaders: CacheHeaders = CacheHeaders.NONE, publish: Boolean = false): Set<String>

Writes an operation to the store.

Link copied to clipboard
suspend fun <D : Operation.Data> writeOptimisticUpdates(operation: Operation<D>, data: D, mutationId: Uuid, publish: Boolean = false): Set<String>

Writes an operation to the optimistic store.

suspend fun <D : Fragment.Data> writeOptimisticUpdates(fragment: Fragment<D>, cacheKey: CacheKey, data: D, mutationId: Uuid, publish: Boolean = false): Set<String>

Writes a fragment to the optimistic store.