Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable cache #692

Closed
thanhbinh84 opened this issue Jul 21, 2020 · 4 comments
Closed

Disable cache #692

thanhbinh84 opened this issue Jul 21, 2020 · 4 comments

Comments

@thanhbinh84
Copy link

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

@micimize
Copy link
Collaborator

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

@matehat
Copy link

matehat commented Dec 28, 2020

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

@micimize
Copy link
Collaborator

@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?

@reilem
Copy link

reilem commented Dec 30, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants