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

1.9.0-beta - forceRefetch doesn't trigger for queries with object as args #2816

Closed
bradbooth opened this issue Oct 25, 2022 · 4 comments
Closed
Milestone

Comments

@bradbooth
Copy link

New to rtkquery and was playing around with the merge & forceRefetch functionality for 1.9.0-beta.0 and it seems like forceRefetch doesn't work how Im expecting if a query has an object for the argument.

Sandbox reproduction: https://codesandbox.io/s/merge-query-sandbox-forked-jjo0y7?file=/src/services/pokemon.ts

For example if I have a query that looks like:

query: ({ pageNumber }) => `pokemon?limit=1&offset=${pageNumber}`,
...
forceRefetch: ({ currentArg, previousArg }) => {
        return true;
}

Im expecting the query to re-fire whenever pageNumber changes - which doesn't seem to be the case. However, if I change the query to a number argument for example:

query: (pageNumber) => `pokemon?limit=1&offset=${pageNumber}`,
...
forceRefetch: ({ currentArg, previousArg }) => {
        return true;
}

Then it seems to work as expected. The issue being if a query needs multiple arguments then I'm not sure how to trigger the forceRefetch. Not sure if I'm perhaps using forceRefetch incorrectly however.

@markerikson
Copy link
Collaborator

Interesting. Thanks for the repro! Will try to look at this in the next few days.

@dorsharonfuse
Copy link

This is causing us issues as well.
@markerikson Any update on this?

@markerikson markerikson added this to the 1.9 milestone Oct 30, 2022
@markerikson
Copy link
Collaborator

markerikson commented Oct 30, 2022

Hmm. At first glance, it seems to be related to this check:

export function useStableQueryArgs<T>(
  queryArgs: T,
  serialize: SerializeQueryArgs<any>,
  endpointDefinition: EndpointDefinition<any, any, any, any>,
  endpointName: string
) {
  const incoming = useMemo(
    () => ({
      queryArgs,
      serialized:
        typeof queryArgs == 'object'
          ? serialize({ queryArgs, endpointDefinition, endpointName })
          : queryArgs,
    }),
    [queryArgs, serialize, endpointDefinition, endpointName]
  )
  const cache = useRef(incoming)
  useEffect(() => {
    if (cache.current.serialized !== incoming.serialized) {
      cache.current = incoming
    }
  }, [incoming])

  return cache.current.serialized === incoming.serialized
    ? cache.current.queryArgs
    : queryArgs
}

Still digging into this.

@markerikson
Copy link
Collaborator

Should be fixed now. Will put out another beta later today.

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