Skip to content

Commit 17f20fc

Browse files
committed
feat: move mutations into the core
1 parent f4b2bdc commit 17f20fc

Some content is hidden

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

54 files changed

+1670
-724
lines changed

docs/src/pages/guides/default-query-function.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ const defaultQueryFn = async key => {
1313
}
1414

1515
// provide the default query function to your app with defaultOptions
16-
const cache = new QueryCache()
1716
const client = new QueryClient({
18-
cache,
1917
defaultOptions: {
2018
queries: {
2119
queryFn: defaultQueryFn,

docs/src/pages/guides/migrating-to-react-query-3.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ This has some benefits:
2323
Use the `QueryClientProvider` component to connect a `QueryClient` to your application:
2424

2525
```js
26-
import { QueryClient, QueryClientProvider, QueryCache } from 'react-query'
26+
import { QueryClient, QueryClientProvider } from 'react-query'
2727

28-
const cache = new QueryCache()
29-
const client = new QueryClient({ cache })
28+
const client = new QueryClient()
3029

3130
function App() {
3231
return <QueryClientProvider client={client}>...</QueryClientProvider>
@@ -58,7 +57,6 @@ The `ReactQueryConfigProvider` component has been removed. Default options for q
5857

5958
```js
6059
const client = new QueryClient({
61-
cache,
6260
defaultOptions: {
6361
queries: {
6462
staleTime: Infinity,
@@ -370,7 +368,7 @@ const unsubscribe = observer.subscribe(result => {
370368
The `client.setQueryDefaults()` method to set default options for a specific query. If the query does not exist yet it will create it.
371369

372370
```js
373-
client.setQueryDefaults('posts', fetchPosts)
371+
client.setQueryDefaults('posts', { queryFn: fetchPosts })
374372

375373
function Component() {
376374
const { data } = useQuery('posts')

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ The default `retryDelay` is set to double (starting at `1000`ms) with each attem
2929

3030
```js
3131
// Configure for all queries
32-
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
32+
import { QueryClient, QueryClientProvider } from 'react-query'
3333

34-
const cache = new QueryCache()
3534
const client = new QueryClient({
36-
cache,
3735
defaultOptions: {
3836
queries: {
3937
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, 30000),

docs/src/pages/guides/ssr.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ React Query supports prefetching multiple queries on the server in Next.js and t
4949

5050
To support caching queries on the server and set up hydration:
5151

52-
- Create a new `QueryCache` and `QueryClient` instance
52+
- Create a new `QueryClient` instance
5353
- Wrap your app component with `<QueryClientProvider>` and pass it the client instance
5454
- Wrapp your app component with `<Hydrate>` and pass it the `dehydratedState` prop from `pageProps`
5555

@@ -58,8 +58,7 @@ To support caching queries on the server and set up hydration:
5858
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
5959
import { Hydrate } from 'react-query/hydration'
6060

61-
const cache = new QueryCache()
62-
const client = new QueryClient({ cache })
61+
const client = new QueryClient()
6362

6463
export default function MyApp({ Component, pageProps }) {
6564
return (
@@ -74,18 +73,17 @@ export default function MyApp({ Component, pageProps }) {
7473

7574
Now you are ready to prefetch some data in your pages with either [`getStaticProps`](https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation) (for SSG) or [`getServerSideProps`](https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering) (for SSR). From React Query's perspective, these integrate in the same way, `getStaticProps` is shown below.
7675

77-
- Create a new `QueryCache` and `QueryClient` instance for each page request
76+
- Create a `QueryClient` instance for each page request
7877
- Prefetch the data using the clients `prefetchQuery` method and wait for it to complete
7978
- Use `dehydrate` to dehydrate the query cache and pass it to the page via the `dehydratedState` prop. This is the same prop that the cache will be picked up from in your `_app.js`
8079

8180
```js
8281
// pages/posts.jsx
83-
import { QueryCache, QueryClient, useQuery } from 'react-query'
82+
import { QueryClient, useQuery } from 'react-query'
8483
import { dehydrate } from 'react-query/hydration'
8584

8685
export async function getStaticProps() {
87-
const cache = new QueryCache()
88-
const client = new QueryClient({ cache })
86+
const client = new QueryClient()
8987

9088
await client.prefetchQuery('posts', getPosts)
9189

@@ -119,10 +117,9 @@ This guide is at-best, a high level overview of how SSR with React Query should
119117
120118
### On the Server
121119

122-
- Create a new `QueryCache` instance
123120
- Create a new `QueryClient` instance
124121
- Using the client, prefetch any data you need
125-
- Dehydrate the cache
122+
- Dehydrate the client
126123
- Render your app with the client provider and also **using the dehydrated state. This is extremely important! You must render both server and client using the same dehydrated state to ensure hydration on the client produces the exact same markup as the server.**
127124
- Serialize and embed the dehydrated cache to be sent to the client with the HTML
128125

@@ -132,10 +129,9 @@ This guide is at-best, a high level overview of how SSR with React Query should
132129
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
133130
import { dehydrate, Hydrate } from 'react-query/hydration'
134131

135-
const cache = new QueryCache()
136-
const client = new QueryClient({ cache })
132+
const client = new QueryClient()
137133
await client.prefetchQuery('key', fn)
138-
const dehydratedState = dehydrate(cache)
134+
const dehydratedState = dehydrate(client)
139135

140136
const html = ReactDOM.renderToString(
141137
<ReactQueryClientProvider client={client}>
@@ -162,18 +158,16 @@ res.send(`
162158
### Client
163159

164160
- Parse the dehydrated cache state that was sent to the client with the HTML
165-
- Create a new `QueryCache` instance
166161
- Create a new `QueryClient` instance
167162
- Render your app with the client provider and also **using the dehydrated state. This is extremely important! You must render both server and client using the same dehydrated state to ensure hydration on the client produces the exact same markup as the server.**
168163

169164
```js
170-
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
165+
import { QueryClient, QueryClientProvider } from 'react-query'
171166
import { Hydrate } from 'react-query/hydration'
172167

173168
const dehydratedState = JSON.parse(window.__REACT_QUERY_INITIAL_QUERIES__)
174169

175-
const cache = new QueryCache()
176-
const client = new QueryClient({ cache })
170+
const client = new QueryClient()
177171

178172
ReactDOM.hydrate(
179173
<ReactQueryClientProvider client={client}>

docs/src/pages/guides/suspense.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ Global configuration:
1111

1212
```js
1313
// Configure for all queries
14-
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query'
14+
import { QueryClient, QueryClientProvider } from 'react-query'
1515

16-
const cache = new QueryCache()
1716
const client = new QueryClient({
18-
cache,
1917
defaultOptions: {
2018
queries: {
2119
suspense: true,

docs/src/pages/overview.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,9 @@ In the example below, you can see React Query in its most basic and simple form
4747
[Open in CodeSandbox](https://codesandbox.io/s/github/tannerlinsley/react-query/tree/master/examples/simple)
4848

4949
```js
50-
import {
51-
useQuery,
52-
QueryCache,
53-
QueryClient,
54-
QueryClientProvider,
55-
} from 'react-query'
56-
57-
const cache = new QueryCache()
58-
const client = new QueryClient({ cache })
50+
import { useQuery, QueryClient, QueryClientProvider } from 'react-query'
51+
52+
const client = new QueryClient()
5953

6054
export default function App() {
6155
return (

docs/src/pages/quick-start.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ import {
2020
} from 'react-query'
2121
import { getTodos, postTodo } from '../my-api'
2222

23-
// Create a cache
24-
const cache = new QueryCache()
25-
2623
// Create a client
27-
const client = new QueryClient({ cache })
24+
const client = new QueryClient()
2825

2926
function App() {
3027
return (

docs/src/pages/reference/QueryClient.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ title: QueryClient
88
The `QueryClient` can be used to interact with a cache:
99

1010
```js
11-
import { QueryClient, QueryCache } from 'react-query'
11+
import { QueryClient } from 'react-query'
1212

13-
const cache = new QueryCache()
1413
const client = new QueryClient({
15-
cache,
1614
defaultOptions: {
1715
queries: {
1816
staleTime: Infinity,
@@ -43,11 +41,13 @@ Its available methods are:
4341

4442
**Options**
4543

46-
- `cache: QueryCache`
44+
- `queryCache: QueryCache`
4745
- The query cache this client is connected to.
46+
- `mutationCache: MutationCache`
47+
- The mutation cache this client is connected to.
4848
- `defaultOptions: DefaultOptions`
4949
- Optional
50-
- Define defaults for all queries and mutations using this query client.
50+
- Define defaults for all queries and mutations using this client.
5151

5252
## `client.fetchQueryData`
5353

@@ -176,7 +176,7 @@ console.log(state.updatedAt)
176176
`setQueryDefaults` is a synchronous method to set default options for a specific query. If the query does not exist yet it will create it.
177177

178178
```js
179-
client.setQueryDefaults('posts', fetchPosts)
179+
client.setQueryDefaults('posts', { queryFn: fetchPosts })
180180

181181
function Component() {
182182
const { data } = useQuery('posts')

docs/src/pages/reference/QueryClientProvider.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ title: QueryClientProvider
66
Use the `QueryClientProvider` component to connect and provide a `QueryClient` to your application:
77

88
```js
9-
import { QueryClient, QueryClientProvider, QueryCache } from 'react-query'
9+
import { QueryClient, QueryClientProvider } from 'react-query'
1010

11-
const cache = new QueryCache()
12-
const client = new QueryClient({ cache })
11+
const client = new QueryClient()
1312

1413
function App() {
1514
return <QueryClientProvider client={client}>...</QueryClientProvider>

examples/auto-refetching/pages/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import {
77
useQuery,
88
useQueryClient,
99
useMutation,
10-
QueryCache,
1110
QueryClient,
1211
QueryClientProvider,
1312
} from 'react-query'
1413
import { ReactQueryDevtools } from 'react-query-devtools'
1514

16-
const cache = new QueryCache()
17-
const client = new QueryClient({ cache })
15+
const client = new QueryClient()
1816

1917
export default function App() {
2018
return (

0 commit comments

Comments
 (0)