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

Determine whether the query was triggered by an invalidation #4757

Open
kelly-hansen opened this issue Dec 6, 2024 · 5 comments
Open

Determine whether the query was triggered by an invalidation #4757

kelly-hansen opened this issue Dec 6, 2024 · 5 comments

Comments

@kelly-hansen
Copy link

I'd like to access something like isInvalidated from within a queryFn so I can do conditional logic within that queryFn. The only thing I see available is forced within the api object, but that is not always true for invalidation refetches. Can this be added?

@jawakarD
Copy link

Interesting!

May I know the usecase for which you need this?

@kelly-hansen
Copy link
Author

Thanks for the response.

Our project has a primary and replica database. We try to make queries to our replica database but there is some lag before the data is updated there, so we query from the primary db immediately after a mutation. Basically I want to conditionally query from the primary db if the query refetch was triggered by an invalidation. I imagine this is a reasonably common scenario.

I have a workaround, which is to automatically log any invalidations and their time in a redux state object, so I can check for invalidations within the last x seconds when querying. As far as I can tell, there is no way to access that data by default, but let me know if I'm wrong.

Also, there isn't anything useful in the documentation about forced, it just says "Indicates if a query has been forced." If it is meant to be an accurate representation of whether a query was triggered by an invalidation, I believe it is not working properly in some cases.

@markerikson
Copy link
Collaborator

@kelly-hansen I think forced is only true if you're using the upsertQueryData util, but I'd have to check.

@kelly-hansen
Copy link
Author

@markerikson I'm definitely not using upsertQueryData, at least not explicitly. forced is still true for refetches triggered by an invalidation MOST of the time.

@markerikson
Copy link
Collaborator

Looks like the internal logic relies on this check:

  function isForcedQuery(
    arg: QueryThunkArg,
    state: RootState<any, string, ReducerPath>,
  ) {
    const requestState = state[reducerPath]?.queries?.[arg.queryCacheKey]
    const baseFetchOnMountOrArgChange =
      state[reducerPath]?.config.refetchOnMountOrArgChange

    const fulfilledVal = requestState?.fulfilledTimeStamp
    const refetchVal =
      arg.forceRefetch ?? (arg.subscribe && baseFetchOnMountOrArgChange)

    if (refetchVal) {
      // Return if it's true or compare the dates because it must be a number
      return (
        refetchVal === true ||
        (Number(new Date()) - Number(fulfilledVal)) / 1000 >= refetchVal
      )
    }
    return false
  }

I'll be honest, I don't actually know the reasoning behind this code :)

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

3 participants