1
1
import type { QueryClient } from '../core/queryClient'
2
- import { Query , QueryState } from '../core/query'
3
- import type { QueryKey , QueryOptions } from '../core/types'
2
+ import type { Query , QueryState } from '../core/query'
3
+ import type {
4
+ MutationKey ,
5
+ MutationOptions ,
6
+ QueryKey ,
7
+ QueryOptions ,
8
+ } from '../core/types'
9
+ import type { Mutation , MutationState } from '../core/mutation'
4
10
5
11
// TYPES
6
12
7
13
export interface DehydrateOptions {
14
+ dehydrateMutations ?: boolean
15
+ dehydrateQueries ?: boolean
16
+ shouldDehydrateMutation ?: ShouldDehydrateMutationFunction
8
17
shouldDehydrateQuery ?: ShouldDehydrateQueryFunction
9
18
}
10
19
11
20
export interface HydrateOptions {
12
- defaultOptions ?: QueryOptions
21
+ defaultOptions ?: {
22
+ queries ?: QueryOptions
23
+ mutations ?: MutationOptions
24
+ }
25
+ }
26
+
27
+ interface DehydratedMutation {
28
+ mutationKey ?: MutationKey
29
+ state : MutationState
13
30
}
14
31
15
32
interface DehydratedQuery {
@@ -20,11 +37,14 @@ interface DehydratedQuery {
20
37
}
21
38
22
39
export interface DehydratedState {
40
+ mutations : DehydratedMutation [ ]
23
41
queries : DehydratedQuery [ ]
24
42
}
25
43
26
44
export type ShouldDehydrateQueryFunction = ( query : Query ) => boolean
27
45
46
+ export type ShouldDehydrateMutationFunction = ( mutation : Mutation ) => boolean
47
+
28
48
// FUNCTIONS
29
49
30
50
function serializePositiveNumber ( value : number ) : number {
@@ -35,6 +55,13 @@ function deserializePositiveNumber(value: number): number {
35
55
return value === - 1 ? Infinity : value
36
56
}
37
57
58
+ function dehydrateMutation ( mutation : Mutation ) : DehydratedMutation {
59
+ return {
60
+ mutationKey : mutation . options . mutationKey ,
61
+ state : mutation . state ,
62
+ }
63
+ }
64
+
38
65
// Most config is not dehydrated but instead meant to configure again when
39
66
// consuming the de/rehydrated data, typically with useQuery on the client.
40
67
// Sometimes it might make sense to prefetch data on the server and include
@@ -48,7 +75,11 @@ function dehydrateQuery(query: Query): DehydratedQuery {
48
75
}
49
76
}
50
77
51
- function defaultShouldDehydrate ( query : Query ) {
78
+ function defaultShouldDehydrateMutation ( mutation : Mutation ) {
79
+ return mutation . state . isPaused
80
+ }
81
+
82
+ function defaultShouldDehydrateQuery ( query : Query ) {
52
83
return query . state . status === 'success'
53
84
}
54
85
@@ -58,21 +89,38 @@ export function dehydrate(
58
89
) : DehydratedState {
59
90
options = options || { }
60
91
61
- const shouldDehydrateQuery =
62
- options . shouldDehydrateQuery || defaultShouldDehydrate
63
-
92
+ const mutations : DehydratedMutation [ ] = [ ]
64
93
const queries : DehydratedQuery [ ] = [ ]
65
94
66
- client
67
- . getQueryCache ( )
68
- . getAll ( )
69
- . forEach ( query => {
70
- if ( shouldDehydrateQuery ( query ) ) {
71
- queries . push ( dehydrateQuery ( query ) )
72
- }
73
- } )
95
+ if ( options ?. dehydrateMutations !== false ) {
96
+ const shouldDehydrateMutation =
97
+ options . shouldDehydrateMutation || defaultShouldDehydrateMutation
98
+
99
+ client
100
+ . getMutationCache ( )
101
+ . getAll ( )
102
+ . forEach ( mutation => {
103
+ if ( shouldDehydrateMutation ( mutation ) ) {
104
+ mutations . push ( dehydrateMutation ( mutation ) )
105
+ }
106
+ } )
107
+ }
108
+
109
+ if ( options ?. dehydrateQueries !== false ) {
110
+ const shouldDehydrateQuery =
111
+ options . shouldDehydrateQuery || defaultShouldDehydrateQuery
112
+
113
+ client
114
+ . getQueryCache ( )
115
+ . getAll ( )
116
+ . forEach ( query => {
117
+ if ( shouldDehydrateQuery ( query ) ) {
118
+ queries . push ( dehydrateQuery ( query ) )
119
+ }
120
+ } )
121
+ }
74
122
75
- return { queries }
123
+ return { mutations , queries }
76
124
}
77
125
78
126
export function hydrate (
@@ -84,9 +132,23 @@ export function hydrate(
84
132
return
85
133
}
86
134
135
+ const mutationCache = client . getMutationCache ( )
87
136
const queryCache = client . getQueryCache ( )
137
+
138
+ const mutations = ( dehydratedState as DehydratedState ) . mutations || [ ]
88
139
const queries = ( dehydratedState as DehydratedState ) . queries || [ ]
89
140
141
+ mutations . forEach ( dehydratedMutation => {
142
+ mutationCache . build (
143
+ client ,
144
+ {
145
+ ...options ?. defaultOptions ?. mutations ,
146
+ mutationKey : dehydratedMutation . mutationKey ,
147
+ } ,
148
+ dehydratedMutation . state
149
+ )
150
+ } )
151
+
90
152
queries . forEach ( dehydratedQuery => {
91
153
const query = queryCache . get ( dehydratedQuery . queryHash )
92
154
@@ -102,7 +164,7 @@ export function hydrate(
102
164
queryCache . build (
103
165
client ,
104
166
{
105
- ...options ?. defaultOptions ,
167
+ ...options ?. defaultOptions ?. queries ,
106
168
queryKey : dehydratedQuery . queryKey ,
107
169
queryHash : dehydratedQuery . queryHash ,
108
170
cacheTime : deserializePositiveNumber ( dehydratedQuery . cacheTime ) ,
0 commit comments