Flutter graphql 使用带有 hive 的缓存,当使用警察 'cacheAndNetwork' 调用查询时,它总是使用没有缓存的网络

问题描述

Flutter graphql 使用带有 hive 的缓存,当使用警察 'cacheAndNetwork' 调用查询时,它总是使用没有缓存的网络

这是我的代码

    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;
  }

它总是从网络返回,实际上,它应该按照graphql文档中描述的方式工作“'cacheAndNetwork'首先从缓存返回结果(如果存在),然后在可用时返回网络结果。"

这是我的 GraphQL 客户端

  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,);
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)