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

fix(queryFilters): fetchStatus to queryFilters #3061

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/src/pages/guides/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ A query filter object supports the following properties:
- `stale?: boolean`
- When set to `true` it will match stale queries.
- When set to `false` it will match fresh queries.
- `fetching?: boolean`
- When set to `true` it will match queries that are currently fetching.
- When set to `false` it will match queries that are not fetching.
- `fetchStatus?: FetchStatus`
- When set to `fetching` it will match queries that are currently fetching.
- When set to `paused` it will match queries that wanted to fetch, but have been `paused`.
- When set to `idle` it will match queries that are not fetching.
- `predicate?: (query: Query) => boolean`
- This predicate function will be called for every single query in the cache and be expected to return truthy for queries that are `found`.
- `queryKey?: QueryKey`
Expand Down
4 changes: 0 additions & 4 deletions src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ export class Query<
return this.observers.some(observer => observer.options.enabled !== false)
}

isFetching(): boolean {
return this.state.fetchStatus === 'fetching'
}

isStale(): boolean {
return (
this.state.isInvalidated ||
Expand Down
2 changes: 1 addition & 1 deletion src/core/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class QueryClient {
isFetching(queryKey?: QueryKey, filters?: QueryFilters): number
isFetching(arg1?: QueryKey | QueryFilters, arg2?: QueryFilters): number {
const [filters] = parseFilterArgs(arg1, arg2)
filters.fetching = true
filters.fetchStatus = 'fetching'
return this.queryCache.findAll(filters).length
}

Expand Down
21 changes: 21 additions & 0 deletions src/core/tests/queryCache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('queryCache', () => {
test('should filter correctly', async () => {
const key1 = queryKey()
const key2 = queryKey()
const keyFetching = queryKey()
await queryClient.prefetchQuery(key1, () => 'data1')
await queryClient.prefetchQuery(key2, () => 'data2')
await queryClient.prefetchQuery([{ a: 'a', b: 'b' }], () => 'data3')
Expand Down Expand Up @@ -137,6 +138,26 @@ describe('queryCache', () => {
queryCache.findAll({ predicate: query => query === query3 })
).toEqual([query3])
expect(queryCache.findAll(['posts'])).toEqual([query4])

expect(queryCache.findAll({ fetchStatus: 'idle' })).toEqual([
query1,
query2,
query3,
query4,
])
expect(queryCache.findAll(key2, { fetchStatus: undefined })).toEqual([
query2,
])

const promise = queryClient.prefetchQuery(keyFetching, async () => {
await sleep(20)
return 'dataFetching'
})
expect(queryCache.findAll({ fetchStatus: 'fetching' })).toEqual([
queryCache.find(keyFetching),
])
await promise
expect(queryCache.findAll({ fetchStatus: 'fetching' })).toEqual([])
})

test('should return all the queries when no filters are defined', async () => {
Expand Down
19 changes: 15 additions & 4 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Mutation } from './mutation'
import type { Query } from './query'
import type {
FetchStatus,
MutationFunction,
MutationKey,
MutationOptions,
Expand Down Expand Up @@ -33,9 +34,9 @@ export interface QueryFilters {
*/
stale?: boolean
/**
* Include or exclude fetching queries
* Include queries matching their fetchStatus
*/
fetching?: boolean
fetchStatus?: FetchStatus
}

export interface MutationFilters {
Expand Down Expand Up @@ -164,7 +165,14 @@ export function matchQuery(
filters: QueryFilters,
query: Query<any, any, any, any>
): boolean {
const { type = 'all', exact, fetching, predicate, queryKey, stale } = filters
const {
type = 'all',
exact,
fetchStatus,
predicate,
queryKey,
stale,
} = filters

if (isQueryKey(queryKey)) {
if (exact) {
Expand All @@ -190,7 +198,10 @@ export function matchQuery(
return false
}

if (typeof fetching === 'boolean' && query.isFetching() !== fetching) {
if (
typeof fetchStatus !== 'undefined' &&
fetchStatus !== query.state.fetchStatus
) {
return false
}

Expand Down
4 changes: 2 additions & 2 deletions src/devtools/tests/devtools.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('ReactQueryDevtools', () => {

// When the query is fetching then expect number of
// fetching queries to be 1
expect(currentQuery?.isFetching()).toEqual(true)
expect(currentQuery?.state.fetchStatus).toEqual('fetching')
await screen.findByText(
getByTextContent(
'fresh (0) fetching (1) paused (0) stale (0) inactive (0)'
Expand All @@ -138,7 +138,7 @@ describe('ReactQueryDevtools', () => {
// until 300ms after, so expect the number of fresh
// queries to be 1
await waitFor(() => {
expect(currentQuery?.isFetching()).toEqual(false)
expect(currentQuery?.state.fetchStatus).toEqual('idle')
})
await screen.findByText(
getByTextContent(
Expand Down