Skip to content

resetQueries method does not trigger refetch on errored queries #10705

@alZyad

Description

@alZyad

Describe the bug

Hello !

I encountered a behavior of resetQueries that i think is not an intended behavior. In my usecase, i tried to reset all my errored queries with this code:

void queryClient.resetQueries({
  predicate: (query) => query.state.status === "error",
});

and what i found out is the queries were reset, but they never got refetched, and stayed stuck in their initial pending status.

The reason is in the source code:

resetQueries<TTaggedQueryKey extends QueryKey = QueryKey>(
  filters?: QueryFilters<TTaggedQueryKey>,
  options?: ResetOptions,
): Promise<void> {
  const queryCache = this.#queryCache

  return notifyManager.batch(() => {
    queryCache.findAll(filters).forEach((query) => {
      query.reset()
    })
    return this.refetchQueries(
      {
        type: 'active',
        ...filters,
      },
      options,
    )
  })
}

The initial step of resetting queries leads to their status being changed to pending (or success if they have initial data), so when we get to the refetch step, the queries we intended to reset + refetch, are not errored anymore. Applying the "only errored queries" filter on the, now changed, queries means we don't refetch any queries.

This also impacts successful queries (if they don't have initial data), as well as any query attribute that is susceptible to mutate during the operation.

The same pattern is found in invalidateQueries but since the only field it mutates - as far as I understand - is isInvalidated the impact is much smaller.
I added it in my reproduction app, but as one can get imagine it's pretty wonky to filter on that attribute.

Your minimal, reproducible example

https://stackblitz.com/github/alZyad/reset-errored-queries-no-refetch/tree/stackblitz?file=src%2FResetQueriesReproduction.jsx

Steps to reproduce

1- reset errored queries by filtering on their status
2- queries which are initially errored stay pending forever

Expected behavior

queries which are initially errored are reset to pending status, but also get refetched.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: MacOS
  • Browser: Arc
  • Version: 1.146.0 (80402)
  • Chromium Engine Version 148.0.7778.97

Tanstack Query adapter

None

TanStack Query version

5.99.2

TypeScript version

No response

Additional context

I opened a PR with a fix suggestion in case my analysis of the issue is correct !
#10704

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions