Skip to content

Commit 239d66c

Browse files
committed
Merge remote-tracking branch 'react-query/master' into alpha
2 parents e90e7f8 + 6d6d9d0 commit 239d66c

File tree

6 files changed

+75
-10
lines changed

6 files changed

+75
-10
lines changed

docs/src/pages/guides/query-cancellation.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ const query = useQuery(['todos'], ({ signal }) => {
9999
100100
## Using `graphql-request`
101101
102+
An `AbortSignal` can be set in the client `request` method.
103+
104+
```js
105+
const client = new GraphQLClient(endpoint)
106+
107+
const query = useQuery('todos', ({ signal }) => {
108+
client.request({ document: query, signal })
109+
})
110+
```
111+
112+
## Using `graphql-request` version less than v4.0.0
113+
102114
An `AbortSignal` can be set in the `GraphQLClient` constructor.
103115
104116
```js

docs/src/pages/reference/QueryClient.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ This distinction is more a "convenience" for ts devs that know which structure w
203203

204204
## `queryClient.setQueryData`
205205

206-
`setQueryData` is a synchronous function that can be used to immediately update a query's cached data. If the query does not exist, it will be created. **If the query is not utilized by a query hook in the default `cacheTime` of 5 minutes, the query will be garbage collected**.
206+
`setQueryData` is a synchronous function that can be used to immediately update a query's cached data. If the query does not exist, it will be created. **If the query is not utilized by a query hook in the default `cacheTime` of 5 minutes, the query will be garbage collected**. To update multiple queries at once and match query keys partially, you need to use [`queryClient.setQueriesData`](#queryclientsetqueriesdata) instead.
207207

208208
> The difference between using `setQueryData` and `fetchQuery` is that `setQueryData` is sync and assumes that you already synchronously have the data available. If you need to fetch the data asynchronously, it's suggested that you either refetch the query key or use `fetchQuery` to handle the asynchronous fetch.
209209
@@ -248,7 +248,7 @@ console.log(state.dataUpdatedAt)
248248

249249
## `queryClient.setQueriesData`
250250

251-
`setQueriesData` is a synchronous function that can be used to immediately update cached data of multiple queries. Only queries that match the passed queryKey or queryFilter will be updated - no new cache entries will be created. Under the hood, [`setQueryData`](#queryclientsetquerydata) is called for each query.
251+
`setQueriesData` is a synchronous function that can be used to immediately update cached data of multiple queries by using filter function or partially matching the query key. Only queries that match the passed queryKey or queryFilter will be updated - no new cache entries will be created. Under the hood, [`setQueryData`](#queryclientsetquerydata) is called for each query.
252252

253253
```js
254254
queryClient.setQueriesData(queryKey | filters, updater)
@@ -257,7 +257,7 @@ queryClient.setQueriesData(queryKey | filters, updater)
257257
**Options**
258258

259259
- `queryKey: QueryKey`: [Query Keys](../guides/query-keys) | `filters: QueryFilters`: [Query Filters](../guides/filters#query-filters)
260-
- if a queryKey is passed as first argument, queryKeys fuzzily matching this param will be updated
260+
- if a queryKey is passed as first argument, queryKeys partially matching this param will be updated
261261
- if a filter is passed, queryKeys matching the filter will be updated
262262
- `updater: TData | (oldData: TData | undefined) => TData`
263263
- the [setQueryData](#queryclientsetquerydata) updater function or new data, will be called for each matching queryKey

examples/load-more-infinite-scroll/pages/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function Example() {
4848
useIntersectionObserver({
4949
target: loadMoreButtonRef,
5050
onIntersect: fetchNextPage,
51-
enabled: hasNextPage,
51+
enabled: !!hasNextPage,
5252
})
5353

5454
return (

src/reactjs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export * from './types'
2020
export type { QueryClientProviderProps } from './QueryClientProvider'
2121
export type { QueryErrorResetBoundaryProps } from './QueryErrorResetBoundary'
2222
export type { HydrateProps } from './Hydrate'
23+
export type { QueriesOptions, QueriesResults } from './useQueries'

src/reactjs/tests/useQueries.test.tsx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919
QueryObserverResult,
2020
QueriesObserver,
2121
QueryFunction,
22+
UseQueryOptions,
23+
QueryKey,
2224
} from '../..'
25+
import { EnsuredQueryKey, QueryFunctionContext } from '../../core'
2326

2427
describe('useQueries', () => {
2528
const queryCache = new QueryCache()
@@ -894,7 +897,8 @@ describe('useQueries', () => {
894897
}
895898
})
896899

897-
it('handles types for QueryFunction factory with strongly typed QueryKey', () => {
900+
it('handles strongly typed queryFn factories and useQueries wrappers', () => {
901+
// QueryKey + queryFn factory
898902
type QueryKeyA = ['queryA']
899903
const getQueryKeyA = (): QueryKeyA => ['queryA']
900904
type GetQueryFunctionA = () => QueryFunction<number, QueryKeyA>
@@ -913,6 +917,32 @@ describe('useQueries', () => {
913917
type SelectorB = (data: string) => [string, number]
914918
const getSelectorB = (): SelectorB => data => [data, +data]
915919

920+
// Wrapper with strongly typed array-parameter
921+
function useWrappedQueries<
922+
TQueryFnData,
923+
TError,
924+
TData,
925+
TQueryKey extends QueryKey
926+
>(queries: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>[]) {
927+
return useQueries(
928+
queries.map(
929+
// no need to type the mapped query
930+
query => {
931+
const { queryFn: fn, queryKey: key, onError: err } = query
932+
expectType<QueryFunction<TQueryFnData, TQueryKey> | undefined>(fn)
933+
return {
934+
queryKey: key,
935+
onError: err,
936+
queryFn: (ctx: QueryFunctionContext<TQueryKey>) => {
937+
expectType<EnsuredQueryKey<TQueryKey>>(ctx.queryKey)
938+
return fn?.call({}, ctx)
939+
},
940+
}
941+
}
942+
)
943+
)
944+
}
945+
916946
// @ts-expect-error (Page component is not rendered)
917947
// eslint-disable-next-line
918948
function Page() {
@@ -951,6 +981,18 @@ describe('useQueries', () => {
951981
expectType<QueryObserverResult<[string, number], unknown>>(
952982
withSelector[1]
953983
)
984+
985+
const withWrappedQueries = useWrappedQueries(
986+
Array(10).map(() => ({
987+
queryKey: getQueryKeyA(),
988+
queryFn: getQueryFunctionA(),
989+
select: getSelectorA(),
990+
}))
991+
)
992+
993+
expectType<QueryObserverResult<number | undefined, unknown>[]>(
994+
withWrappedQueries
995+
)
954996
}
955997
})
956998

src/reactjs/useQueries.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type GetResults<T> =
6868
/**
6969
* QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
7070
*/
71-
type QueriesOptions<
71+
export type QueriesOptions<
7272
T extends any[],
7373
Result extends any[] = [],
7474
Depth extends ReadonlyArray<number> = []
@@ -84,15 +84,20 @@ type QueriesOptions<
8484
? T
8585
: // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!
8686
// use this to infer the param types in the case of Array.map() argument
87-
T extends UseQueryOptions<infer TQueryFnData, infer TError, infer TData>[]
88-
? UseQueryOptions<TQueryFnData, TError, TData>[]
87+
T extends UseQueryOptions<
88+
infer TQueryFnData,
89+
infer TError,
90+
infer TData,
91+
infer TQueryKey
92+
>[]
93+
? UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>[]
8994
: // Fallback
9095
UseQueryOptions[]
9196

9297
/**
9398
* QueriesResults reducer recursively maps type param to results
9499
*/
95-
type QueriesResults<
100+
export type QueriesResults<
96101
T extends any[],
97102
Result extends any[] = [],
98103
Depth extends ReadonlyArray<number> = []
@@ -104,7 +109,12 @@ type QueriesResults<
104109
? [...Result, GetResults<Head>]
105110
: T extends [infer Head, ...infer Tail]
106111
? QueriesResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]>
107-
: T extends UseQueryOptions<infer TQueryFnData, infer TError, infer TData>[]
112+
: T extends UseQueryOptions<
113+
infer TQueryFnData,
114+
infer TError,
115+
infer TData,
116+
any
117+
>[]
108118
? // Dynamic-size (homogenous) UseQueryOptions array: map directly to array of results
109119
UseQueryResult<unknown extends TData ? TQueryFnData : TData, TError>[]
110120
: // Fallback

0 commit comments

Comments
 (0)