writeCache<T, U extends JsonSerializable> method

Future<void> writeCache<T, U extends JsonSerializable>(
  1. GraphQLQuery<T, U> query,
  2. Map<String, dynamic> data, {
  3. Duration? ttl,
})

Writes data to the cache for a specific query.

This is useful for:

  • Optimistic updates
  • Pre-populating cache
  • Manual cache management

Example:

await client.writeCache(
  GetUserQuery(variables: GetUserArguments(id: '123')),
  {'user': {'id': '123', 'name': 'John Doe'}},
  ttl: Duration(minutes: 10),
);

Implementation

Future<void> writeCache<T, U extends JsonSerializable>(
  GraphQLQuery<T, U> query,
  Map<String, dynamic> data, {
  Duration? ttl,
}) async {
  final request = Request(
    operation: Operation(
      document: query.document,
      operationName: query.operationName,
    ),
    variables: query.getVariablesMap(),
  );

  final cacheKey = _cacheLink.generateCacheKey(request);
  _cacheLink.write(cacheKey, data, ttl: ttl);
}