DartpolloCachedClient class

A GraphQL client with built-in response caching capabilities.

DartpolloCachedClient extends the functionality of DartpolloClient by adding a caching layer that can significantly improve performance and reduce network requests.

Features:

  • Multiple cache policies (cacheFirst, networkFirst, cacheOnly, networkOnly, cacheAndNetwork)
  • Configurable TTL (Time To Live) for cache entries
  • Pluggable storage backends (in-memory, Hive, etc.)
  • Per-request cache policy overrides
  • Direct cache access methods

Example:

final client = DartpolloCachedClient(
  'https://api.example.com/graphql',
  cacheStore: InMemoryCacheStore(maxSize: 100),
  defaultCachePolicy: CachePolicy.cacheFirst,
  defaultCacheTtl: Duration(minutes: 5),
);

// Execute query with default cache policy
final response = await client.execute(GetUserQuery(variables: GetUserArguments(id: '123')));

// Override cache policy for specific request
final freshResponse = await client.execute(
  GetUserQuery(variables: GetUserArguments(id: '123')),
  context: Context().withCachePolicy(CachePolicy.networkOnly),
);

Constructors

DartpolloCachedClient(String graphQLEndpoint, {Client? httpClient, CacheStore? cacheStore, CachePolicy defaultCachePolicy = CachePolicy.cacheFirst, Duration? defaultCacheTtl})
Creates a cached GraphQL client.
factory
Creates a cached client from a custom Link.

Properties

cacheStore CacheStore
Accesses the underlying cache store directly.
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

clearCache() Future<void>
Clears all cached data.
dispose() → void
Closes the HTTP client and releases resources.
evictCache<T, U extends JsonSerializable>(GraphQLQuery<T, U> query) Future<void>
Evicts (removes) cached data for a specific query.
execute<T, U extends JsonSerializable>(GraphQLQuery<T, U> query, {Context context = const Context()}) Future<GraphQLResponse<T>>
Executes a GraphQLQuery, returning a typed response.
getCacheStats() Map<String, dynamic>
Returns cache statistics.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
readCache<T, U extends JsonSerializable>(GraphQLQuery<T, U> query) Future<T?>
Reads cached data for a query without making a network request.
stream<T, U extends JsonSerializable>(GraphQLQuery<T, U> query, {Context context = const Context()}) Stream<GraphQLResponse<T>>
Streams a GraphQLQuery, returning a typed response stream.
toString() String
A string representation of this object.
inherited
writeCache<T, U extends JsonSerializable>(GraphQLQuery<T, U> query, Map<String, dynamic> data, {Duration? ttl}) Future<void>
Writes data to the cache for a specific query.

Operators

operator ==(Object other) bool
The equality operator.
inherited