Skip to content

Feature/extended filters get query data #4190

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions docs/reference/QueryClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ The options for `prefetchInfiniteQuery` are exactly the same as those of [`fetch
`getQueryData` is a synchronous function that can be used to get an existing query's cached data. If the query does not exist, `undefined` will be returned.

```tsx
const data = queryClient.getQueryData(queryKey)
const data = queryClient.getQueryData(queryKey| filters | queryOptions)
```

**Options**

- `queryKey?: QueryKey`: [Query Keys](../guides/query-keys)
- `queryKey?: QueryKey`: [Query Keys](../guides/query-keys) | `filters: ExtendedQueryFilters`: [Query Filters](../guides/filters#query-filters) & [Query Options](../reference/useQuery)
- `filters?: QueryFilters`: [Query Filters](../guides/filters#query-filters)

**Returns**
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/queryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
}

find<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData>(
arg1: QueryKey,
arg1: QueryKey | QueryFilters,
arg2?: QueryFilters,
): Query<TQueryFnData, TError, TData> | undefined {
const [filters] = parseFilterArgs(arg1, arg2)
Expand Down
10 changes: 9 additions & 1 deletion packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
hashQueryKeyByOptions,
MutationFilters,
functionalUpdate,
ExtendedQueryFilters,
} from './utils'
import type {
QueryClientConfig,
Expand Down Expand Up @@ -106,11 +107,18 @@ export class QueryClient {
return this.mutationCache.findAll({ ...filters, fetching: true }).length
}

getQueryData<TData = unknown>(
filters: ExtendedQueryFilters<TData, any, TData, QueryKey>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filters: ExtendedQueryFilters<TData, any, TData, QueryKey>,
filters: ExtendedQueryFilters<TData, unknown, TData, QueryKey>,

could TError be unknown here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. I'll try it

): TData | undefined
getQueryData<TData = unknown>(
queryKey: QueryKey,
filters?: QueryFilters,
): TData | undefined
getQueryData<TData = unknown>(
arg1: QueryKey | ExtendedQueryFilters<TData, any, TData, QueryKey>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question here:

Suggested change
arg1: QueryKey | ExtendedQueryFilters<TData, any, TData, QueryKey>,
arg1: QueryKey | ExtendedQueryFilters<TData, unknown, TData, QueryKey>,

arg2?: QueryFilters,
): TData | undefined {
return this.queryCache.find<TData>(queryKey, filters)?.state.data
return this.queryCache.find<TData>(arg1, arg2)?.state.data
}

getQueriesData<TData = unknown>(queryKey: QueryKey): [QueryKey, TData][]
Expand Down
41 changes: 41 additions & 0 deletions packages/query-core/src/tests/queryClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,58 @@ describe('queryClient', () => {
const key = queryKey()
queryClient.setQueryData([key, 'id'], 'bar')
expect(queryClient.getQueryData([key, 'id'])).toBe('bar')
expect(queryClient.getQueryData({ queryKey: [key, 'id'] })).toBe('bar')
})

test('should return the query data with inactive filter', () => {
const key = queryKey()
queryClient.setQueryData([key, 'id'], 'bar')
expect(queryClient.getQueryData([key, 'id'], { type: 'inactive' })).toBe(
'bar',
)
expect(
queryClient.getQueryData({ queryKey: [key, 'id'], type: 'inactive' }),
).toBe('bar')
})

test('should return the query data with stale filter', () => {
const key = queryKey()
queryClient.setQueryData([key, 'id'], 'bar')
expect(
queryClient.getQueryData<string>([key, 'id'], { stale: false }),
).toBe('bar')
expect(
queryClient.getQueryData<string>({
queryKey: [key, 'id'],
stale: false,
}),
).toBe('bar')
})

test('should return the query data even with query options', () => {
const key = queryKey()
queryClient.setQueryData([key, 'id'], 'bar')
expect(
queryClient.getQueryData({
queryKey: [key, 'id'],
queryFn: async () => 'foo',
cacheTime: 1000,
retry: false,
}),
).toBe('bar')
})

test('should return undefined if the query is not found', () => {
const key = queryKey()
expect(queryClient.getQueryData(key)).toBeUndefined()
expect(queryClient.getQueryData({ queryKey: key })).toBeUndefined()
})

test('should match exact by default', () => {
const key = queryKey()
queryClient.setQueryData([key, 'id'], 'bar')
expect(queryClient.getQueryData([key])).toBeUndefined()
expect(queryClient.getQueryData({ queryKey: [key] })).toBeUndefined()
})
})

Expand Down
10 changes: 10 additions & 0 deletions packages/query-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export interface QueryFilters {
fetchStatus?: FetchStatus
}

export interface ExtendedQueryFilters<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> extends Omit<QueryFilters, 'queryKey'>,
Omit<QueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey'> {
queryKey: TQueryKey
}

export interface MutationFilters {
/**
* Match mutation key exactly
Expand Down