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

MutationCache.md

308 lines6.2 KB

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