Flutter graphql using cache with hive, when calling query with police ‘cacheAndNetwork’ it always uses the network without the cache

Flutter graphql using cache with hive, when calling query with police ‘cacheAndNetwork’ it always uses the network without the cache


2

Flutter graphql using cache with hive, when calling query with police ‘cacheAndNetwork’ it always uses the network without the cache

Here is caple of my code

    Future<QueryResult> query(String query,
      {Map<String, dynamic> variables}) async {
    QueryOptions options = QueryOptions(
      document: gql(query),
      variables: variables,
      fetchPolicy: FetchPolicy.cacheAndNetwork,
      errorPolicy: ErrorPolicy.none,
      cacheRereadPolicy: CacheRereadPolicy.mergeOptimistic,
    );

    final result = await _client.query(options);

    return result;
  }

It returns always from the network, actually, it should work how it’s described in the documentation of graphql "‘cacheAndNetwork’ returns result from cache first (if it exists), then return network result once it’s available."

Here is my GraphQL client

  GraphQLClient _client;
  Client httpClient;

  void initClient(session) {
    if (_client != null) return;

    final token = session.getAccessToken().getJwtToken();

    final HttpLink _httpLink = HttpLink(
      'https://*************/graphql',
      defaultHeaders: {
        'x-api-key': '************',
        'Content-Type': '*************'
      },
      httpClient: this.httpClient,
    );

    final AuthLink _authLink = AuthLink(getToken: () => token);

    final Link _link = _authLink.concat(_httpLink);

    _client = GraphQLClient(
      cache: GraphQLCache(
        store: HiveStore(),
      ),
      link: _link,
    );
  }

1

  • Please read @Hive tag info and do not use it for questions like this. Hive tag is used for completely differnt product. There is flutter-hive tag, use it, or create your own tag please

    – leftjoin

    Jul 6, 2021 at 17:17


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *