Skip to content

Commit 950362d

Browse files
committed
feat: initial v3 changes
1 parent e200c99 commit 950362d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2359
-3377
lines changed

docs/src/pages/docs/api.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const {
5252
const queryInfo = useQuery({
5353
queryKey,
5454
queryFn,
55-
config,
55+
enabled,
5656
})
5757
```
5858
@@ -124,6 +124,9 @@ const queryInfo = useQuery({
124124
- `onSettled: Function(data, error) => data`
125125
- Optional
126126
- This function will fire any time the query is either successfully fetched or errors and be passed either the data or error
127+
- `select: (data) => data`
128+
- Optional
129+
- This option can be used to transform or select a part of the data returned by the query function.
127130
- `suspense: Boolean`
128131
- Optional
129132
- Set this to `true` to enable suspense mode.
@@ -186,7 +189,7 @@ const queryInfo = useQuery({
186189
- The failure count for the query.
187190
- Incremented every time the query fails.
188191
- Reset to `0` when the query succeeds.
189-
- `refetch: Function({ throwOnError }) => Promise<TResult | undefined>`
192+
- `refetch: Function({ throwOnError }) => Promise<TData | undefined>`
190193
- A function to manually refetch the query.
191194
- If the query errors, the error will only be logged. If you want an error to be thrown, pass the `throwOnError: true` option
192195
- `remove: Function() => void`
@@ -253,7 +256,7 @@ The returned properties for `useInfiniteQuery` are identical to the [`useQuery`
253256
254257
- `isFetchingMore: false | 'next' | 'previous'`
255258
- If using `paginated` mode, this will be `true` when fetching more results using the `fetchMore` function.
256-
- `fetchMore: Function(fetchMoreVariableOverride) => Promise<TResult | undefined>`
259+
- `fetchMore: Function(fetchMoreVariableOverride) => Promise<TData | undefined>`
257260
- This function allows you to fetch the next "page" of results.
258261
- `fetchMoreVariableOverride` allows you to optionally override the fetch more variable returned from your `getFetchMore` option to your query function to retrieve the next page of results.
259262
- `canFetchMore: Boolean`
@@ -391,7 +394,7 @@ try {
391394
392395
**Returns**
393396
394-
- `Promise<TResult>`
397+
- `Promise<TData>`
395398
396399
## `queryCache.prefetchQuery`
397400
@@ -436,7 +439,7 @@ The options for `prefetchQuery` are exactly the same as those of [`useQuery`](#u
436439
437440
**Returns**
438441
439-
- `Promise<TResult | undefined>`
442+
- `Promise<TData | undefined>`
440443
- A promise is returned that will either immediately resolve with the query's cached response data, or resolve to the data returned by the fetch function. It **will not** throw an error if the fetch fails. This can be configured by setting the `throwOnError` option to `true`.
441444
442445
## `queryCache.getQueryData`
@@ -837,16 +840,16 @@ function App() {
837840
- `queryCache: QueryCache`
838841
- Instance of QueryCache.
839842

840-
## `ReactQueryErrorResetBoundary`
843+
## `QueryErrorResetBoundary`
841844

842-
When using **suspense** or **useErrorBoundaries** in your queries, you need a way to let queries know that you want to try again when re-rendering after some error occured. With the `ReactQueryErrorResetBoundary` component you can reset any query errors within the boundaries of the component.
845+
When using **suspense** or **useErrorBoundaries** in your queries, you need a way to let queries know that you want to try again when re-rendering after some error occured. With the `QueryErrorResetBoundary` component you can reset any query errors within the boundaries of the component.
843846

844847
```js
845-
import { ReactQueryErrorResetBoundary } from 'react-query'
848+
import { QueryErrorResetBoundary } from 'react-query'
846849
import { ErrorBoundary } from 'react-error-boundary'
847850

848851
const App: React.FC = () => (
849-
<ReactQueryErrorResetBoundary>
852+
<QueryErrorResetBoundary>
850853
{({ reset }) => (
851854
<ErrorBoundary
852855
onReset={reset}
@@ -860,13 +863,13 @@ const App: React.FC = () => (
860863
<Page />
861864
</ErrorBoundary>
862865
)}
863-
</ReactQueryErrorResetBoundary>
866+
</QueryErrorResetBoundary>
864867
)
865868
```
866869

867870
## `useErrorResetBoundary`
868871

869-
This hook will reset any query errors within the closest `ReactQueryErrorResetBoundary`. If there is no boundary defined it will reset them globally:
872+
This hook will reset any query errors within the closest `QueryErrorResetBoundary`. If there is no boundary defined it will reset them globally:
870873

871874
```js
872875
import { useErrorResetBoundary } from 'react-query'

docs/src/pages/docs/guides/suspense.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ In addition to queries behaving differently in suspense mode, mutations also beh
4747

4848
Whether you are using **suspense** or **useErrorBoundaries** in your queries, you will need a way to let queries know that you want to try again when re-rendering after some error occured.
4949

50-
Query errors can be reset with the `ReactQueryErrorResetBoundary` component or with the `useErrorResetBoundary` hook.
50+
Query errors can be reset with the `QueryErrorResetBoundary` component or with the `useErrorResetBoundary` hook.
5151

5252
When using the component it will reset any query errors within the boundaries of the component:
5353

5454
```js
55-
import { ReactQueryErrorResetBoundary } from 'react-query'
55+
import { QueryErrorResetBoundary } from 'react-query'
5656
import { ErrorBoundary } from 'react-error-boundary'
5757

5858
const App: React.FC = () => (
59-
<ReactQueryErrorResetBoundary>
59+
<QueryErrorResetBoundary>
6060
{({ reset }) => (
6161
<ErrorBoundary
6262
onReset={reset}
@@ -70,11 +70,11 @@ const App: React.FC = () => (
7070
<Page />
7171
</ErrorBoundary>
7272
)}
73-
</ReactQueryErrorResetBoundary>
73+
</QueryErrorResetBoundary>
7474
)
7575
```
7676

77-
When using the hook it will reset any query errors within the closest `ReactQueryErrorResetBoundary`. If there is no boundary defined it will reset them globally:
77+
When using the hook it will reset any query errors within the closest `QueryErrorResetBoundary`. If there is no boundary defined it will reset them globally:
7878

7979
```js
8080
import { useErrorResetBoundary } from 'react-query'

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module.exports = {
44
testMatch: ['<rootDir>/src/**/*.test.tsx'],
55
testPathIgnorePatterns: ['<rootDir>/types/'],
66
moduleNameMapper: {
7-
'react-query': '<rootDir>/src/react/index.ts',
7+
'react-query': '<rootDir>/src/index.ts',
88
},
99
}

rollup.config.js

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,6 @@ const babelConfig = { extensions, runtimeHelpers: true }
2727
const resolveConfig = { extensions }
2828

2929
export default [
30-
{
31-
input: inputSrc,
32-
output: {
33-
file: 'dist/react-query.mjs',
34-
format: 'es',
35-
sourcemap: true,
36-
},
37-
external,
38-
plugins: [
39-
resolve(resolveConfig),
40-
babel(babelConfig),
41-
commonJS(),
42-
externalDeps(),
43-
],
44-
},
45-
{
46-
input: inputSrc,
47-
output: {
48-
file: 'dist/react-query.min.mjs',
49-
format: 'es',
50-
sourcemap: true,
51-
},
52-
external,
53-
plugins: [
54-
resolve(resolveConfig),
55-
babel(babelConfig),
56-
commonJS(),
57-
externalDeps(),
58-
terser(),
59-
],
60-
},
6130
{
6231
input: inputSrc,
6332
output: {
@@ -99,37 +68,6 @@ export default [
9968
}),
10069
],
10170
},
102-
{
103-
input: hydrationSrc,
104-
output: {
105-
file: 'dist/hydration/react-query-hydration.mjs',
106-
format: 'es',
107-
sourcemap: true,
108-
},
109-
external: hydrationExternal,
110-
plugins: [
111-
resolve(resolveConfig),
112-
babel(babelConfig),
113-
commonJS(),
114-
externalDeps(),
115-
],
116-
},
117-
{
118-
input: hydrationSrc,
119-
output: {
120-
file: 'dist/hydration/react-query-hydration.min.mjs',
121-
format: 'es',
122-
sourcemap: true,
123-
},
124-
external: hydrationExternal,
125-
plugins: [
126-
resolve(resolveConfig),
127-
babel(babelConfig),
128-
commonJS(),
129-
externalDeps(),
130-
terser(),
131-
],
132-
},
13371
{
13472
input: hydrationSrc,
13573
output: {

src/core/config.ts

Lines changed: 24 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,42 @@
1-
import { stableStringify } from './utils'
2-
import type {
3-
ArrayQueryKey,
4-
MutationConfig,
5-
QueryConfig,
6-
QueryKey,
7-
QueryKeySerializerFunction,
8-
ReactQueryConfig,
9-
ResolvedQueryConfig,
10-
} from './types'
11-
import type { QueryCache } from './queryCache'
12-
13-
// TYPES
14-
15-
export interface ReactQueryConfigRef {
16-
current: ReactQueryConfig
17-
}
1+
import type { DefaultOptions } from './types'
2+
import { defaultQueryKeySerializerFn } from './utils'
183

194
// CONFIG
205

21-
export const defaultQueryKeySerializerFn: QueryKeySerializerFunction = (
22-
queryKey: QueryKey
23-
): [string, ArrayQueryKey] => {
24-
try {
25-
let arrayQueryKey: ArrayQueryKey = Array.isArray(queryKey)
26-
? queryKey
27-
: [queryKey]
28-
const queryHash = stableStringify(arrayQueryKey)
29-
arrayQueryKey = JSON.parse(queryHash)
30-
return [queryHash, arrayQueryKey]
31-
} catch {
32-
throw new Error('A valid query key is required!')
33-
}
34-
}
35-
36-
/**
37-
* Config merging strategy
38-
*
39-
* When using hooks the config will be merged in the following order:
40-
*
41-
* 1. These defaults.
42-
* 2. Defaults from the hook query cache.
43-
* 3. Combined defaults from any config providers in the tree.
44-
* 4. Query/mutation config provided to the hook.
45-
*
46-
* When using a query cache directly the config will be merged in the following order:
47-
*
48-
* 1. These defaults.
49-
* 2. Defaults from the query cache.
50-
* 3. Query/mutation config provided to the query cache method.
51-
*/
52-
export const DEFAULT_CONFIG: ReactQueryConfig = {
6+
export const DEFAULT_OPTIONS = {
537
queries: {
548
cacheTime: 5 * 60 * 1000,
559
enabled: true,
5610
notifyOnStatusChange: true,
57-
queryFn: () => Promise.reject(),
5811
queryKeySerializerFn: defaultQueryKeySerializerFn,
5912
refetchOnMount: true,
6013
refetchOnReconnect: true,
6114
refetchOnWindowFocus: true,
6215
retry: 3,
63-
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, 30000),
16+
retryDelay: (attempt: number) => Math.min(1000 * 2 ** attempt, 30000),
6417
staleTime: 0,
6518
structuralSharing: true,
6619
},
6720
}
6821

69-
export function getDefaultReactQueryConfig() {
70-
return {
71-
queries: { ...DEFAULT_CONFIG.queries },
72-
mutations: { ...DEFAULT_CONFIG.mutations },
73-
}
74-
}
75-
76-
export function mergeReactQueryConfigs(
77-
a: ReactQueryConfig,
78-
b: ReactQueryConfig
79-
): ReactQueryConfig {
80-
return {
81-
shared: {
82-
...a.shared,
83-
...b.shared,
84-
},
85-
queries: {
86-
...a.queries,
87-
...b.queries,
88-
},
89-
mutations: {
90-
...a.mutations,
91-
...b.mutations,
92-
},
93-
}
94-
}
95-
96-
export function getResolvedQueryConfig<TResult, TError>(
97-
queryCache: QueryCache,
98-
queryKey: QueryKey,
99-
contextConfig?: ReactQueryConfig,
100-
config?: QueryConfig<TResult, TError>
101-
): ResolvedQueryConfig<TResult, TError> {
102-
const queryCacheConfig = queryCache.getDefaultConfig()
103-
104-
const resolvedConfig = {
105-
...DEFAULT_CONFIG.queries,
106-
...queryCacheConfig?.shared,
107-
...queryCacheConfig?.queries,
108-
...contextConfig?.shared,
109-
...contextConfig?.queries,
110-
...config,
111-
} as ResolvedQueryConfig<TResult, TError>
112-
113-
const result = resolvedConfig.queryKeySerializerFn(queryKey)
114-
115-
resolvedConfig.queryCache = queryCache
116-
resolvedConfig.queryHash = result[0]
117-
resolvedConfig.queryKey = result[1]
118-
119-
return resolvedConfig
120-
}
121-
122-
export function isResolvedQueryConfig<TResult, TError>(
123-
config: any
124-
): config is ResolvedQueryConfig<TResult, TError> {
125-
return Boolean(config.queryHash)
126-
}
127-
128-
export function getResolvedMutationConfig<
129-
TResult,
130-
TError,
131-
TVariables,
132-
TSnapshot
133-
>(
134-
queryCache: QueryCache,
135-
contextConfig?: ReactQueryConfig,
136-
config?: MutationConfig<TResult, TError, TVariables, TSnapshot>
137-
): MutationConfig<TResult, TError, TVariables, TSnapshot> {
138-
const queryCacheConfig = queryCache.getDefaultConfig()
139-
return {
140-
...DEFAULT_CONFIG.mutations,
141-
...queryCacheConfig?.shared,
142-
...queryCacheConfig?.mutations,
143-
...contextConfig?.shared,
144-
...contextConfig?.mutations,
145-
...config,
146-
} as MutationConfig<TResult, TError, TVariables, TSnapshot>
22+
export function getDefaultOptions(): DefaultOptions {
23+
return DEFAULT_OPTIONS
24+
}
25+
26+
export function mergeDefaultOptions(
27+
a: DefaultOptions,
28+
b?: DefaultOptions
29+
): DefaultOptions {
30+
return b
31+
? {
32+
queries: {
33+
...a.queries,
34+
...b.queries,
35+
},
36+
mutations: {
37+
...a.mutations,
38+
...b.mutations,
39+
},
40+
}
41+
: a
14742
}

0 commit comments

Comments
 (0)