Graphql-flutter: Disable cache

Created on 21 Jul 2020  路  4Comments  路  Source: zino-app/graphql-flutter

Is your feature request related to a problem? Please describe.
Our customer don't want to cache anything on the app. So we tried

return GraphQLClient(
    link: authLink.concat(link),
  );
return GraphQLClient(
    cache: null,
    link: authLink.concat(link),
  );

Both don't work:

'package:graphql/src/core/query_options.dart': Failed assertion: line 174 pos 16: 'cache != null': is not true.

Describe the solution you'd like
Remove @required for cache

Most helpful comment

Because of how integral the cache is, a null cache option is a #wontfix (#621). You can skip caching with a FetchPolicy.networkOnly:

  final policies = Policies(
    fetch: FetchPolicy.networkOnly,
  );

  return ValueNotifier<GraphQLClient>(
    GraphQLClient(
      cache: cache,
      link: link,
      defaultPolicies: DefaultPolicies(
        watchQuery: policies,
        query: policies,
        mutate: policies,
      ),
    ),
  );

I seem to remember someone implementing a noop cache, but can't find it. Also, if you want a lightweight client that simply fetches data, composing links from https://github.com/gql-dart/gql might be better for you. There was as good example of this somewhere but again, can't find it

All 4 comments

Because of how integral the cache is, a null cache option is a #wontfix (#621). You can skip caching with a FetchPolicy.networkOnly:

  final policies = Policies(
    fetch: FetchPolicy.networkOnly,
  );

  return ValueNotifier<GraphQLClient>(
    GraphQLClient(
      cache: cache,
      link: link,
      defaultPolicies: DefaultPolicies(
        watchQuery: policies,
        query: policies,
        mutate: policies,
      ),
    ),
  );

I seem to remember someone implementing a noop cache, but can't find it. Also, if you want a lightweight client that simply fetches data, composing links from https://github.com/gql-dart/gql might be better for you. There was as good example of this somewhere but again, can't find it

I tried with the above and still had (frustratingly hard to find) issues with the library changing some parts of the server response from under my feet. What I did to completely turn off caching (and messing up with server response altogether) was this instead:

  final policies = Policies(
    fetch: FetchPolicy.noCache,
  );

  return ValueNotifier<GraphQLClient>(
    GraphQLClient(
      cache: cache,
      link: link,
      defaultPolicies: DefaultPolicies(
        watchQuery: policies,
        query: policies,
        mutate: policies,
      ),
    ),
  );

@matehat if you could open an issue to document the frustrating response changes, that'd be appreciated. I'm guessing you're referring to the cache round trip we do to integrate in any optimistic changes?

This also just turned out to be the solution to a very annoying bug I was having, steps to repro:

  • Fetch list of items, result = [A, B, C]
  • Add item D using GraphQL mutate, result shown in UI: [A, B, C, D]
  • Refetch list of items, result = [A, B, C] again, item D disappears from the UI

Maybe I am just using it incorrectly, but this seems like strange default behaviour. Adding network-only luckily solved it in my case. Is this in docs anywhere? Maybe under caching the section we could add a "How to disable cache" item.

Was this page helpful?
0 / 5 - 0 ratings