Skip to content

Clear Cache #14

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

Open
alexwine36 opened this issue Sep 17, 2017 · 4 comments
Open

Clear Cache #14

alexwine36 opened this issue Sep 17, 2017 · 4 comments

Comments

@alexwine36
Copy link

I am trying to run a new query on the client and for the life of me I can not get not bypass the cached results
This is the code that I am running and changing the variables that are being used seems to have no effect on the result that is returned

const { user } = Template.instance().gqlQuery({
      query: USER_LUNCHES,
      vaiables: { userEmail: Session.get("email")},
    }).get();

Any help would be greatly appreciated currently I am at the banging my head on a wall stage...

@michaelcbrook
Copy link

michaelcbrook commented Apr 19, 2018

I also recently had this issue and thought I would leave my solution. This seems to be more related to Apollo. Adding fetchPolicy: "network-only" after variables will force the request to happen. Docs here

What I concluded is the way Apollo caches data, it automatically uses cached data and bypasses a request if a document with the same id or _id (by default) already exists in the cache. Even if the rest of the data has changed. Changing the fetch policy will change this behavior.

I'm assuming this is your problem as well and not the typo vaiables in your code.

@boboci9
Copy link

boboci9 commented Jan 22, 2019

I am having the same issues, and even as I am using fetchPolicy: "network-only" or I even tried fetchPolicy: "no-cache". I can't get the query to re-run even though an edit mutation has been done on the same document.

Template.patientInfo.created = function() {
  this.member = new ReactiveVar(false);
  Session.set("showLoadingPatientForm", false);
  tempInst = this;
  this.autorun(function() {
    if (Session.get("showLoadingPatientForm")) console.log("reload");
    if (FlowRouter.getParam("id")) {
      let variables = { _id: FlowRouter.getParam("id") };
      let currentPatient = Template.instance()
        .gqlQuery({
          query: GET_PATIENT,
          fetchPolicy: "no-cache",
          variables: variables
        })
        .get();
      if (currentPatient && currentPatient.patient) {
        tempInst.member.set(currentPatient.patient);
        Session.set("showLoadingPatientForm", false);
      }
      } else {
      tempInst.member.set({});
    }
  });
};

I am checking in the DB and the object has chanegd but the GET_PATIENT query is not run because I have logs there.

@jamiter
Copy link
Contributor

jamiter commented Jan 23, 2019

I think the issue is that Blaze Apollo does it's own (kind-of) caching to prevent unneeded reruns in autoruns (like template helpers).

const key = generateRequestKey(request);
if (!this._gqlQueries[key]) {
this._gqlQueries[key] = new ReactiveObserver(client.watchQuery(request), {
equals,
});
this._gqlQueriesDep.changed();
}
return this._gqlQueries[key];

If the request doesn't change (like when it has network-only the first and second time) I think the result will also not change.

I don't know yet how to fix this. If we don't use the Blaze caching then you can't directly use the queries in helpers, but need to define them in the onCreated and pass it to a ReactiveVar which you'll use in your helpers, for example. That's a lot less fluent, but safer.

Any other suggestions are welcome.

Side note: As you might have noticed, Blaze-Apollo isn't actively maintained/developed, because we're moving to React ourselves. Blaze is great, but has a lot of quirks. We have more trust in React for future development and projects. If we can fix things with reasonable investment, we will, but Blaze-Apollo will stay mostly "as is". Thanks.

@jamiter
Copy link
Contributor

jamiter commented May 13, 2019

@boboci9, re-reading your issue it seems that nothing will trigger your autorun so no attempt will be made to do a refetch. If a mutation was done somewhere, make sure you update the Apollo cache correctly. That should trigger the underlying watchQuery. Changing the FlowRouter id param starts a new query, right?

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

No branches or pull requests

4 participants