Dartpollo0.1.0-alpha.6
Documentation
On this page

Caching

DartpolloCachedClient stores complete operation responses. Your application controls expiry and invalidation.

Create a cached client

lib/graphql_client.dart

final client = DartpolloCachedClient(
  endpoint,
  cacheStore: InMemoryCacheStore(maxSize: 100),
  defaultCachePolicy: CachePolicy.cacheFirst,
  defaultCacheTtl: Duration(minutes: 5),
);

The default store is an unbounded in-memory store. The default policy is cacheFirst; a null default TTL means entries do not expire.

Policy behavior

Policy Cache hit Cache miss Network data
cacheFirst Return cache Request network Write to cache
networkFirst Used only after a thrown network/link failure Rethrow network failure Write to cache
cacheOnly Return cache Empty stream No request
networkOnly Ignore cache read Request network Still writes to cache
cacheAndNetwork Emit cache, then request network Request network Emit and cache network data

execute(...cacheOnly...) throws StateError on a cache miss because it awaits the first event of an empty stream. Use stream when an empty result is a valid outcome.

Use stream, not execute, for cacheAndNetwork; execute returns only the first emission.

Per-request policy and TTL

lib/viewer_request.dart

final response = await client.execute(
  ViewerQuery(),
  context: const Context().withCache(
    policy: CachePolicy.networkFirst,
    ttl: Duration(minutes: 10),
  ),
);

Shortcuts include withoutCache(), cacheOnly(), and cacheAndNetwork().

Current limitation: withCacheTtl(null) leaves the context unchanged, so it cannot override a non-null client default to “never expire.”

Mutations and invalidation

Run mutations with networkOnly, then evict affected queries. Otherwise the default cacheFirst policy can reuse a previous mutation response instead of sending the mutation. Query entries are not updated automatically.

lib/update_viewer.dart

await client.execute(
  UpdateViewerMutation(variables: input),
  context: const Context().withoutCache(),
);

await client.evictCache(ViewerQuery());

Direct cache access

lib/cache_maintenance.dart

final cached = await client.readCache(ViewerQuery());

await client.writeCache(
  ViewerQuery(),
  {'viewer': {'login': 'octocat'}},
  ttl: Duration(minutes: 10),
);

await client.evictCache(ViewerQuery());
await client.clearCache();
final stats = client.getCacheStats();

writeCache accepts a raw GraphQL data map. Its null TTL means that manually written entry does not expire; it does not inherit the client default.

Stores

In memory

InMemoryCacheStore(maxSize: n) uses entry-count LRU eviction. Reads refresh recency. Omit maxSize for an unbounded ephemeral store.

Hive

lib/graphql_cache.dart

Hive.init(appDataPath);
final store = await HiveCacheStore.open('graphql_cache');
final client = DartpolloCachedClient(endpoint, cacheStore: store);

// Shutdown
client.dispose();
await store.close();

The client does not close its store. Hive removes expired or corrupt entries lazily and offers compact, getStats, and isOpen.

Custom store

Implement synchronous get, set, delete, clear, and getAll on CacheStore. Implementations should filter and remove expired CacheEntry values.

Cache isolation

The key hashes operation name, printed document, and JSON variables. It does not include endpoint, headers, authenticated user, or arbitrary request context. Do not share one store namespace across users or endpoints; clear or isolate stores when identity changes.