Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

MutationCache.md

306 lines6.1 KB

1---
2id: MutationCache
3title: MutationCache
4---
5​
6Defined in: [packages/vue-query/src/mutationCache.ts:15](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationCache.ts#L15)
7​
8Vue-aware subclass of `@tanstack/query-core`'s `MutationCache`. `find`/`findAll` also accept a
9MaybeRefDeep filters object, so `ref`s can be passed directly without unwrapping. Access it via
10`queryClient.getMutationCache()` β€” `QueryClient` constructs one of these by default.
11​
12## Extends
13​
14- `MutationCache`
15​
16## Constructors
17​
18### Constructor
19​
20```ts
21new MutationCache(config?: MutationCacheConfig): MutationCache;
22```
23​
24Defined in: [packages/query-core/src/mutationCache.ts:128](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L128)
25​
26#### Parameters
27​
28##### config?
29​
30[`MutationCacheConfig`](../interfaces/MutationCacheConfig.md) = `{}`
31​
32#### Returns
33​
34`MutationCache`
35​
36#### Inherited from
37​
38```ts
39MC.constructor
40```
41​
42## Properties
43​
44### config
45​
46```ts
47config: MutationCacheConfig = {};
48```
49​
50Defined in: [packages/query-core/src/mutationCache.ts:128](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L128)
51​
52#### Inherited from
53​
54```ts
55MC.config
56```
57​
58## Methods
59​
60### clear()
61​
62```ts
63clear(): void;
64```
65​
66Defined in: [packages/query-core/src/mutationCache.ts:262](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L262)
67​
68Removes all mutations from the cache.
69​
70#### Returns
71​
72`void`
73​
74#### Example
75​
76```ts
77const mutationCache = queryClient.getMutationCache()
78​
79mutationCache.clear()
80```
81​
82#### Inherited from
83​
84```ts
85MC.clear
86```
87​
88***
89​
90### find()
91​
92```ts
93find<TData, TError, TVariables, TOnMutateResult>(filters: MaybeRefDeep<MutationFilters<unknown, Error, unknown, unknown>>):
94 | Mutation<TData, TError, TVariables, TOnMutateResult>
95 | undefined;
96```
97​
98Defined in: [packages/vue-query/src/mutationCache.ts:16](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationCache.ts#L16)
99​
100A slightly more advanced method that can be used to get an existing mutation instance from
101the cache. If the mutation does not exist, `undefined` is returned.
102​
103This is not typically needed for most applications, but can come in handy when needing more
104information about a mutation in rare scenarios.
105​
106#### Type Parameters
107​
108##### TData
109​
110`TData` = `unknown`
111​
112##### TError
113​
114`TError` = `Error`
115​
116##### TVariables
117​
118`TVariables` = `any`
119​
120##### TOnMutateResult
121​
122`TOnMutateResult` = `unknown`
123​
124#### Parameters
125​
126##### filters
127​
128`MaybeRefDeep`\<[`MutationFilters`](../interfaces/MutationFilters.md)\<`unknown`, `Error`, `unknown`, `unknown`\>\>
129​
130The filters to match. `exact` defaults to `true`.
131​
132#### Returns
133​
134 \| [`Mutation`](Mutation.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
135 \| `undefined`
136​
137The first matching mutation, or `undefined`.
138​
139#### See
140​
141[MutationCache#findAll](#findall)
142​
143#### Example
144​
145```ts
146const mutationCache = queryClient.getMutationCache()
147​
148const mutation = mutationCache.find({ mutationKey: ['addPost'] })
149```
150​
151#### Overrides
152​
153```ts
154MC.find
155```
156​
157***
158​
159### findAll()
160​
161```ts
162findAll(filters?: MaybeRefDeep<MutationFilters<unknown, Error, unknown, unknown>>): Mutation<unknown, Error, unknown, unknown>[];
163```
164​
165Defined in: [packages/vue-query/src/mutationCache.ts:27](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationCache.ts#L27)
166​
167An even more advanced method that can be used to get existing mutation instances from the
168cache that match the given filters. If no mutations match, an empty array is returned.
169​
170This is not typically needed for most applications, but can come in handy when needing more
171information about mutations in rare scenarios.
172​
173#### Parameters
174​
175##### filters?
176​
177`MaybeRefDeep`\<[`MutationFilters`](../interfaces/MutationFilters.md)\<`unknown`, `Error`, `unknown`, `unknown`\>\> = `{}`
178​
179The filters to match. Without filters, every mutation is returned.
180​
181#### Returns
182​
183[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
184​
185The matching mutations.
186​
187#### See
188​
189[MutationCache#find](#find)
190​
191#### Example
192​
193```ts
194const mutationCache = queryClient.getMutationCache()
195​
196const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
197```
198​
199#### Overrides
200​
201```ts
202MC.findAll
203```
204​
205***
206​
207### getAll()
208​
209```ts
210getAll(): Mutation<unknown, Error, unknown, unknown>[];
211```
212​
213Defined in: [packages/query-core/src/mutationCache.ts:285](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L285)
214​
215Returns all mutations within the cache.
216​
217This is not typically needed for most applications, but can come in handy when needing more
218information about a mutation in rare scenarios.
219​
220#### Returns
221​
222[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
223​
224Every mutation in the cache.
225​
226#### Example
227​
228```ts
229const mutationCache = queryClient.getMutationCache()
230​
231const mutations = mutationCache.getAll()
232```
233​
234#### Inherited from
235​
236```ts
237MC.getAll
238```
239​
240***
241​
242### hasListeners()
243​
244```ts
245hasListeners(): boolean;
246```
247​
248Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
249​
250Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
251​
252#### Returns
253​
254`boolean`
255​
256`true` if at least one listener is registered.
257​
258#### Inherited from
259​
260```ts
261MC.hasListeners
262```
263​
264***
265​
266### subscribe()
267​
268```ts
269subscribe(listener: MutationCacheListener): () => void;
270```
271​
272Defined in: [packages/query-core/src/subscribable.ts:28](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L28)
273​
274Registers a listener to be called on every update this object notifies about. Returns a function
275that removes the listener again β€” call it to stop listening. The base class never drops a listener
276on its own, though some subclasses clear all of theirs in `destroy()`.
277​
278#### Parameters
279​
280##### listener
281​
282`MutationCacheListener`
283​
284Called on each update, with whatever the subclass passes to its subscribers.
285​
286#### Returns
287​
288A function that removes the listener.
289​
290() => `void`
291​
292#### Example
293​
294```ts
295const unsubscribe = subscribable.subscribe(() => {
296 // react to the update
297})
298​
299unsubscribe()
300```
301​
302#### Inherited from
303​
304```ts
305MC.subscribe
306```