Skip to content

Commit eeec5f7

Browse files
authored
feat: remove overloads (#4714)
* refactor(query-core): remove overloads remove overloads and only allow all functions to be called with a single signature BREAKING CHANGE: Overloads are removed, query-core now supports a single signature * test(query-core): apply single signature to all tests * refactor(react-query): remove overloads remove overloads and only allow all functions to be called with a single signature BREAKING CHANGE: Overloads are removed, react-query now supports a single signature * test(react-query): apply single signature to all tests * refactor(solid-query): remove overloads remove overloads and only allow all functions to be called with a single signature BREAKING CHANGE: Overloads are removed, solid-query now supports a single signature * test(solid-query): apply single signature to all tests * refactor(vue-query): remove overloads remove overloads and only allow all functions to be called with a single signature BREAKING CHANGE: Overloads are removed, vue-query now supports a single signature * test(vue-query): apply single signature to all tests * test(react-query-persist-client): apply single signature to all tests * fix(react-query-devtools): apply single signature to all tests * test(react-query-devtools): apply single signature to all tests * test(query-sync-storage-persister): apply single signature to all tests * docs: apply object single signature to all docs * docs(examples/react): apply object single signature to all examples * docs(examples/solid): apply object single signature to all examples * docs(examples/vue): apply object single signature to all examples * style(examples): run prettier on all files * docs: use multiline whenever we use the object syntax * fix(setQueryDefaults): keep it as two arguments * fix(createMutation): remove unnecessary shallow copy * fix(vue-query): rename parseArgs functions to unrefArgs * docs(migrating-to-react-query-5): list all affected functions * fix(setQueryData): remove as alias * refactor(getQueryData): getQueryData now supports only queryKey as an argument BREAKING CHANGE: getQueryData now accepts only queryKey as an argument * refactor(getQueryState): getQueryState now supports only queryKey as an argument BREAKING CHANGE: getQueryState now accepts only queryKey as an argument * test(react-query/useQuery): missing single signature
1 parent af2e27e commit eeec5f7

File tree

82 files changed

+4032
-4850
lines changed

Some content is hidden

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

82 files changed

+4032
-4850
lines changed

docs/react/adapters/solid-query.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import { Switch, Match, For } from 'solid-js'
1313
const queryClient = new QueryClient()
1414

1515
function Example() {
16-
const query = createQuery(() => ['todos'], fetchTodos)
16+
const query = createQuery({
17+
queryKey: () => ['todos'],
18+
queryFn: fetchTodos
19+
})
1720

1821
return (
1922
<div>
@@ -69,10 +72,16 @@ Solid Query offers an API similar to React Query, but there are some key differ
6972

7073
```tsx
7174
// ❌ react version
72-
useQuery(["todos", todo], fetchTodos)
75+
useQuery({
76+
queryKey: ["todos", todo],
77+
queryFn: fetchTodos
78+
})
7379

7480
// ✅ solid version
75-
createQuery(() => ["todos", todo()], fetchTodos)
81+
createQuery({
82+
queryKey: () => ["todos", todo()],
83+
queryFn: fetchTodos
84+
})
7685
```
7786

7887
- Suspense works for queries out of the box if you access the query data inside a `<Suspense>` boundary.
@@ -81,7 +90,11 @@ createQuery(() => ["todos", todo()], fetchTodos)
8190
import { For, Suspense } from 'solid-js'
8291

8392
function Example() {
84-
const query = createQuery(() => ['todos'], fetchTodos)
93+
const query = createQuery({
94+
queryKey: () => ['todos'],
95+
queryFn: fetchTodos
96+
})
97+
8598
return (
8699
<div>
87100
{/* ✅ Will trigger loading fallback, data accessed in a suspense context. */}
@@ -113,20 +126,21 @@ export default function App() {
113126

114127
function Example() {
115128
// ❌ react version -- supports destructing outside reactive context
116-
// const { isLoading, error, data } = useQuery(['repoData'], () =>
117-
// fetch('https://api.github.com/repos/tannerlinsley/react-query').then(res =>
129+
// const { isLoading, error, data } = useQuery({
130+
// queryKey: ['repoData'], () =>
131+
// queryFn: fetch('https://api.github.com/repos/tannerlinsley/react-query').then(res =>
118132
// res.json()
119133
// )
120-
// )
134+
// })
121135

122136
// ✅ solid version -- does not support destructuring outside reactive context
123-
const query = createQuery(
124-
() => ['repoData'],
125-
() =>
137+
const query = createQuery({
138+
queryKey: () => ['repoData'],
139+
queryFn: () =>
126140
fetch('https://api.github.com/repos/tannerlinsley/react-query').then(
127141
(res) => res.json(),
128142
),
129-
)
143+
})
130144

131145
// ✅ access query properties in JSX reactive context
132146
return (
@@ -161,7 +175,9 @@ const queryClient = new QueryClient()
161175

162176
function Example() {
163177
const [enabled, setEnabled] = createSignal(false)
164-
const query = createQuery(() => ['todos'], fetchTodos, {
178+
const query = createQuery({
179+
queryKey: () => ['todos'],
180+
queryFn: fetchTodos,
165181
// ❌ passing a signal directly is not reactive
166182
// enabled: enabled(),
167183

docs/react/community/liaoliao666-react-query-kit.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ console.log(usePost.getKey(variables)) // ['/posts', { id: 1 }]
6666
export async function getStaticProps() {
6767
const queryClient = new QueryClient()
6868

69-
await queryClient.prefetchQuery(usePost.getKey(variables), usePost.queryFn)
69+
await queryClient.prefetchQuery({
70+
queryKey: usePost.getKey(variables),
71+
queryFn: usePost.queryFn
72+
})
7073

7174
return {
7275
props: {

docs/react/guides/migrating-to-react-query-5.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,96 @@ title: Migrating to TanStack Query v5
77

88
v5 is a major version, so there are some breaking changes to be aware of:
99

10+
### Supports a single signature, one object
11+
12+
useQuery and friends used to have many overloads in TypeScript - different ways how the function can be invoked. Not only this was tough to maintain, type wise, it also required a runtime check to see which type the first and the second parameter, to correctly create options.
13+
14+
now we only support the object format.
15+
16+
```diff
17+
- useQuery(key, fn, options)
18+
+ useQuery({ queryKey, queryFn, ...options })
19+
20+
- useInfiniteQuery(key, fn, options)
21+
+ useInfiniteQuery({ queryKey, queryFn, ...options })
22+
23+
- useMutation(fn, options)
24+
+ useMutation({ mutationFn, ...options })
25+
26+
- useIsFetching(key, filters)
27+
+ useIsFetching({ queryKey, ...filters })
28+
29+
- useIsMutating(key, filters)
30+
+ useIsMutating({ mutationKey, ...filters })
31+
```
32+
33+
```diff
34+
- queryClient.isFetching(key, filters)
35+
+ queryClient.isFetching({ queryKey, ...filters })
36+
37+
- queryClient.ensureQueryData(key, filters)
38+
+ queryClient.ensureQueryData({ queryKey, ...filters })
39+
40+
- queryClient.getQueriesData(key, filters)
41+
+ queryClient.getQueriesData({ queryKey, ...filters })
42+
43+
- queryClient.setQueriesData(key, updater, filters, options)
44+
+ queryClient.setQueriesData({ queryKey, ...filters }, updater, options)
45+
46+
- queryClient.removeQueries(key, filters)
47+
+ queryClient.removeQueries({ queryKey, ...filters })
48+
49+
- queryClient.resetQueries(key, filters, options)
50+
+ queryClient.resetQueries({ queryKey, ...filters }, options)
51+
52+
- queryClient.cancelQueries(key, filters, options)
53+
+ queryClient.cancelQueries({ queryKey, ...filters }, options)
54+
55+
- queryClient.invalidateQueries(key, filters, options)
56+
+ queryClient.invalidateQueries({ queryKey, ...filters }, options)
57+
58+
- queryClient.refetchQueries(key, filters, options)
59+
+ queryClient.refetchQueries({ queryKey, ...filters }, options)
60+
61+
- queryClient.fetchQuery(key, fn, options)
62+
+ queryClient.fetchQuery({ queryKey, queryFn, ...options })
63+
64+
- queryClient.prefetchQuery(key, fn, options)
65+
+ queryClient.prefetchQuery({ queryKey, queryFn, ...options })
66+
67+
- queryClient.fetchInfiniteQuery(key, fn, options)
68+
+ queryClient.fetchInfiniteQuery({ queryKey, queryFn, ...options })
69+
70+
- queryClient.prefetchInfiniteQuery(key, fn, options)
71+
+ queryClient.prefetchInfiniteQuery({ queryKey, queryFn, ...options })
72+
```
73+
74+
```diff
75+
- queryCache.find(key, filters)
76+
+ queryCache.find({ queryKey, ...filters })
77+
78+
- queryCache.findAll(key, filters)
79+
+ queryCache.findAll({ queryKey, ...filters })
80+
```
81+
82+
### `queryClient.getQueryData` now accepts queryKey only as an Argument
83+
84+
`queryClient.getQueryData` argument is changed to accept only a `queryKey`
85+
86+
```diff
87+
- queryClient.getQueryData(queryKey, filters)
88+
+ queryClient.getQueryData(queryKey)
89+
```
90+
91+
### `queryClient.getQueryState` now accepts queryKey only as an Argument
92+
93+
`queryClient.getQueryState` argument is changed to accept only a `queryKey`
94+
95+
```diff
96+
- queryClient.getQueryState(queryKey, filters)
97+
+ queryClient.getQueryState(queryKey)
98+
```
99+
10100
### The `remove` method has been removed from useQuery
11101

12102
Previously, remove method used to remove the query from the queryCache without informing observers about it. It was best used to remove data imperatively that is no longer needed, e.g. when logging a user out.

docs/react/reference/QueryClient.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ const data = queryClient.getQueryData(queryKey)
172172

173173
**Options**
174174

175-
- `filters?: QueryFilters`: [Query Filters](../guides/filters#query-filters)
175+
- `queryKey: QueryKey`: [Query Keys](../guides/query-keys)
176176

177177
**Returns**
178178

@@ -263,13 +263,13 @@ Updates via `setQueryData` must be performed in an _immuatable_ way. **DO NOT**
263263
`getQueryState` is a synchronous function that can be used to get an existing query's state. If the query does not exist, `undefined` will be returned.
264264

265265
```tsx
266-
const state = queryClient.getQueryState({ queryKey })
266+
const state = queryClient.getQueryState(queryKey)
267267
console.log(state.dataUpdatedAt)
268268
```
269269

270270
**Options**
271271

272-
- `filters?: QueryFilters`: [Query Filters](../guides/filters#query-filters)
272+
- `queryKey: QueryKey`: [Query Keys](../guides/query-keys)
273273

274274
## `queryClient.setQueriesData`
275275

docs/solid/overview.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import { Switch, Match, For } from 'solid-js'
1414
const queryClient = new QueryClient()
1515

1616
function Example() {
17-
const query = createQuery(() => ['todos'], fetchTodos)
17+
const query = createQuery({
18+
queryKey: () => ['todos'],
19+
queryFn: fetchTodos
20+
})
1821

1922
return (
2023
<div>
@@ -70,10 +73,16 @@ Solid Query offers an API similar to React Query, but there are some key differ
7073

7174
```tsx
7275
// ❌ react version
73-
useQuery(["todos", todo], fetchTodos)
76+
useQuery({
77+
queryKey: ["todos", todo],
78+
queryFn: fetchTodos
79+
})
7480

7581
// ✅ solid version
76-
createQuery(() => ["todos", todo()], fetchTodos)
82+
createQuery({
83+
queryKey: () => ["todos", todo()],
84+
queryFn: fetchTodos
85+
})
7786
```
7887

7988
- Suspense works for queries out of the box if you access the query data inside a `<Suspense>` boundary.
@@ -82,7 +91,11 @@ createQuery(() => ["todos", todo()], fetchTodos)
8291
import { For, Suspense } from 'solid-js'
8392

8493
function Example() {
85-
const query = createQuery(() => ['todos'], fetchTodos)
94+
const query = createQuery({
95+
queryKey: () => ['todos'],
96+
queryFn: fetchTodos
97+
}
98+
)
8699
return (
87100
<div>
88101
{/* ✅ Will trigger loading fallback, data accessed in a suspense context. */}
@@ -114,20 +127,21 @@ export default function App() {
114127

115128
function Example() {
116129
// ❌ react version -- supports destructing outside reactive context
117-
// const { isLoading, error, data } = useQuery(['repoData'], () =>
118-
// fetch('https://api.github.com/repos/tannerlinsley/react-query').then(res =>
130+
// const { isLoading, error, data } = useQuery({
131+
// queryKey: ['repoData'], () =>
132+
// queryFn: fetch('https://api.github.com/repos/tannerlinsley/react-query').then(res =>
119133
// res.json()
120134
// )
121-
// )
135+
// })
122136

123137
// ✅ solid version -- does not support destructuring outside reactive context
124-
const query = createQuery(
125-
() => ['repoData'],
126-
() =>
138+
const query = createQuery({
139+
queryKey: () => ['repoData'],
140+
queryFn: () =>
127141
fetch('https://api.github.com/repos/tannerlinsley/react-query').then(
128142
(res) => res.json(),
129143
),
130-
)
144+
})
131145

132146
// ✅ access query properties in JSX reactive context
133147
return (
@@ -162,7 +176,9 @@ const queryClient = new QueryClient()
162176

163177
function Example() {
164178
const [enabled, setEnabled] = createSignal(false)
165-
const query = createQuery(() => ['todos'], fetchTodos, {
179+
const query = createQuery({
180+
queryKey: () => ['todos'],
181+
queryFn: fetchTodos,
166182
// ❌ passing a signal directly is not reactive
167183
// enabled: enabled(),
168184

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ function Example() {
3333
fetchPreviousPage,
3434
hasNextPage,
3535
hasPreviousPage,
36-
} = useInfiniteQuery(
37-
['projects'],
38-
async ({ pageParam = 0 }) => {
36+
} = useInfiniteQuery({
37+
queryKey: ['projects'],
38+
queryFn: async ({ pageParam = 0 }) => {
3939
const res = await axios.get('/api/projects?cursor=' + pageParam)
4040
return res.data
4141
},
42-
{
43-
getPreviousPageParam: (firstPage) => firstPage.previousId ?? undefined,
44-
getNextPageParam: (lastPage) => lastPage.nextId ?? undefined,
45-
},
46-
)
42+
getPreviousPageParam: (firstPage) => firstPage.previousId ?? undefined,
43+
getNextPageParam: (lastPage) => lastPage.nextId ?? undefined,
44+
})
4745

4846
React.useEffect(() => {
4947
if (inView) {

examples/react/react-native/src/screens/MovieDetailsScreen.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ type Props = {
2222
};
2323

2424
export function MovieDetailsScreen({ route }: Props) {
25-
const { isLoading, error, data, refetch } = useQuery<MovieDetails, Error>(
26-
['movie', route.params.movie.title],
27-
() => fetchMovie(route.params.movie.title),
28-
{ initialData: route.params.movie as MovieDetails }
29-
);
25+
const { isLoading, error, data, refetch } = useQuery<MovieDetails, Error>({
26+
queryKey: ['movie', route.params.movie.title],
27+
queryFn: () => fetchMovie(route.params.movie.title),
28+
initialData: route.params.movie as MovieDetails,
29+
});
3030

3131
const { isRefetchingByUser, refetchByUser } = useRefreshByUser(refetch);
3232

examples/react/react-native/src/screens/MoviesListScreen.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ type Props = {
2222
};
2323

2424
export function MoviesListScreen({ navigation }: Props) {
25-
const { isLoading, error, data, refetch } = useQuery<Movie[], Error>(
26-
['movies'],
27-
fetchMovies
28-
);
25+
const { isLoading, error, data, refetch } = useQuery<Movie[], Error>({
26+
queryKey: ['movies'],
27+
queryFn: fetchMovies,
28+
});
2929
const { isRefetchingByUser, refetchByUser } = useRefreshByUser(refetch);
3030
useRefreshOnFocus(refetch);
3131

examples/react/react-router/src/routes/root.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const action = (queryClient) => async () => {
3737
export default function Root() {
3838
const { q } = useLoaderData();
3939
const { data: contacts } = useQuery(contactListQuery(q));
40-
const searching = useIsFetching(["contacts", "list"]) > 0;
40+
const searching = useIsFetching({ queryKey: ["contacts", "list"] }) > 0;
4141
const navigation = useNavigation();
4242
const submit = useSubmit();
4343

examples/solid/basic-typescript/src/index.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,11 @@ const getPostById = async (id: number): Promise<Post> => {
9797
}
9898

9999
function createPost(postId: number) {
100-
return createQuery(
101-
() => ['post', postId],
102-
() => getPostById(postId),
103-
{
104-
enabled: !!postId,
105-
},
106-
)
100+
return createQuery({
101+
queryKey: () => ['post', postId],
102+
queryFn: () => getPostById(postId),
103+
enabled: !!postId,
104+
})
107105
}
108106

109107
function Post(props: { postId: number; setPostId: Setter<number> }) {

0 commit comments

Comments
 (0)