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

useQuery re-triggers with variables it has already fetched and cache-first fetchPolicy #1041

Open
YanDjin opened this issue Aug 19, 2023 · 1 comment

Comments

@YanDjin
Copy link

YanDjin commented Aug 19, 2023

  • I'm submitting a ...
    [ * ] bug report
    [ ] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary

I use apollo-link-scalars to serialize and parse Dates. for my specific case, i need to send the time zone with the date. So i use the serialize function to transform and send the date in this format: 2023-08-19T02:00:00+02:00
I have a request with an input variable of type Date that is nullable, if i change this variable and then I put it back to the first value, the request will re-launch even with the cache-first fetchPolicy (using the useQuery hook).
This behavior is not preset if the serialize function only does a toISOString

so this code will work correctly

const typesMap = {
  Date: {
    serialize: (parsed: unknown): string | null => {
      return parsed instanceof Date ? parsed.toISOString() : null;
    },
    parseValue(raw: unknown) {
      ...
    }
}

while this one will re-launch query it has already fetched with fetchPolicy to cache-first

const typesMap = {
  Date: {
    serialize: (parsed: unknown): string | null => {
      return parsed instanceof Date ? toISOStringWithTimezone(parsed) : null;
    },
    parseValue(raw: unknown) {
      ...
    }
}

My guess is apollo when comparing old to new values, uses the old values that have been serialized using the custom serialize function, but serializes to new values using the default one (toJSON function). Just a guess thought.

@YanDjin
Copy link
Author

YanDjin commented Aug 19, 2023

A workaround is to override the toJSON of the Date prototype to return the format desired (may break other parts of the application ?!)

Date.prototype.toJSON = function (): string {
  return toISOStringWithTimezone(this);
};

or create a new Date class that overrides the Date toJSON and use this new one for the variables of useQuery

class Date1 extends Date {
  toJSON(): string {
    return toISOStringWithTimezone(this);
  }
}

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

1 participant