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

MutationCache.md

502 lines10.0 KB

Class: MutationCache

Defined in: packages/query-core/src/mutationCache.ts:187

The MutationCache is the storage for mutations.

Normally, you will not interact with the MutationCache directly and instead use the QueryClient.

import { MutationCache } from '@tanstack/react-query'

const mutationCache = new MutationCache({
  onError: (error) => {
    console.log(error)
  },
  onSuccess: (data) => {
    console.log(data)
  },
})

Its available methods are:

Options

  • onError?: (error: unknown, variables: unknown, onMutateResult: unknown, mutation: Mutation, mutationFnContext: MutationFunctionContext) => Promise<unknown> | unknown
    • Optional
    • This function will be called if some mutation encounters an error.
    • If you return a Promise from it, it will be awaited
  • onSuccess?: (data: unknown, variables: unknown, onMutateResult: unknown, mutation: Mutation, mutationFnContext: MutationFunctionContext) => Promise<unknown> | unknown
    • Optional
    • This function will be called if some mutation is successful.
    • If you return a Promise from it, it will be awaited
  • onSettled?: (data: unknown | undefined, error: unknown | null, variables: unknown, onMutateResult: unknown, mutation: Mutation, mutationFnContext: MutationFunctionContext) => Promise<unknown> | unknown
    • Optional
    • This function will be called if some mutation is settled (either successful or errored).
    • If you return a Promise from it, it will be awaited
  • onMutate?: (variables: unknown, mutation: Mutation, mutationFnContext: MutationFunctionContext) => Promise<unknown> | unknown
    • Optional
    • This function will be called before some mutation executes.
    • If you return a Promise from it, it will be awaited

Global callbacks

The onError, onSuccess, onSettled and onMutate callbacks on the MutationCache can be used to handle these events on a global level. They are different to defaultOptions provided to the QueryClient because:

  • defaultOptions can be overridden by each Mutation - the global callbacks will always be called.
  • onMutate does not allow returning a result.

mutationCache.getAll

getAll returns all mutations within the cache.

Note: This is not typically needed for most applications, but can come in handy when needing more information about a mutation in rare scenarios

const mutations = mutationCache.getAll()

Returns

  • Mutation[]
    • Mutation instances from the cache

mutationCache.subscribe

The subscribe method can be used to subscribe to the mutation cache as a whole and be informed of safe/known updates to the cache like mutation states changing or mutations being updated, added or removed.

const callback = (event) => {
  console.log(event.type, event.mutation)
}

const unsubscribe = mutationCache.subscribe(callback)

Options

  • callback: (mutation?: MutationCacheNotifyEvent) => void
    • This function will be called with the mutation cache any time it is updated.

Returns

  • unsubscribe: Function => void
    • This function will unsubscribe the callback from the mutation cache.

mutationCache.clear

The clear method can be used to clear the cache entirely and start fresh.

mutationCache.clear()

Extends

  • Subscribable<MutationCacheListener>

Constructors

Constructor

new MutationCache(config): MutationCache;

Defined in: packages/query-core/src/mutationCache.ts:192

Parameters

config

MutationCacheConfig = {}

Returns

MutationCache

Overrides

Subscribable<MutationCacheListener>.constructor

Properties

config

config: MutationCacheConfig = {};

Defined in: packages/query-core/src/mutationCache.ts:192


listeners

protected listeners: Set<MutationCacheListener>;

Defined in: packages/query-core/src/subscribable.ts:2

Inherited from

Subscribable.listeners

Methods

add()

add(mutation): void;

Defined in: packages/query-core/src/mutationCache.ts:217

Parameters

mutation

Mutation<any, any, any, any>

Returns

void


build()

build<TData, TError, TVariables, TOnMutateResult>(
   client, 
   options, 
state?): Mutation<TData, TError, TVariables, TOnMutateResult>;

Defined in: packages/query-core/src/mutationCache.ts:199

Type Parameters

TData

TData

TError

TError

TVariables

TVariables

TOnMutateResult

TOnMutateResult

Parameters

client

QueryClient

options

MutationOptions<TData, TError, TVariables, TOnMutateResult>

state?

MutationState<TData, TError, TVariables, TOnMutateResult>

Returns

Mutation<TData, TError, TVariables, TOnMutateResult>


canRun()

canRun(mutation): boolean;

Defined in: packages/query-core/src/mutationCache.ts:254

Parameters

mutation

Mutation<any, any, any, any>

Returns

boolean


clear()

clear(): void;

Defined in: packages/query-core/src/mutationCache.ts:284

Returns

void


find()

find<TData, TError, TVariables, TOnMutateResult>(filters): 
  | Mutation<TData, TError, TVariables, TOnMutateResult>
  | undefined;

Defined in: packages/query-core/src/mutationCache.ts:298

Type Parameters

TData

TData = unknown

TError

TError = Error

TVariables

TVariables = any

TOnMutateResult

TOnMutateResult = unknown

Parameters

filters

MutationFilters

Returns

| Mutation<TData, TError, TVariables, TOnMutateResult> | undefined


findAll()

findAll(filters): Mutation<unknown, Error, unknown, unknown>[];

Defined in: packages/query-core/src/mutationCache.ts:313

Parameters

filters

MutationFilters = {}

Returns

Mutation<unknown, Error, unknown, unknown>[]


getAll()

getAll(): Mutation<unknown, Error, unknown, unknown>[];

Defined in: packages/query-core/src/mutationCache.ts:294

Returns

Mutation<unknown, Error, unknown, unknown>[]


hasListeners()

hasListeners(): boolean;

Defined in: packages/query-core/src/subscribable.ts:19

Returns

boolean

Inherited from

Subscribable.hasListeners

notify()

notify(event): void;

Defined in: packages/query-core/src/mutationCache.ts:317

Parameters

event

MutationCacheNotifyEvent

Returns

void


onSubscribe()

protected onSubscribe(): void;

Defined in: packages/query-core/src/subscribable.ts:23

Returns

void

Inherited from

Subscribable.onSubscribe

onUnsubscribe()

protected onUnsubscribe(): void;

Defined in: packages/query-core/src/subscribable.ts:27

Returns

void

Inherited from

Subscribable.onUnsubscribe

remove()

remove(mutation): void;

Defined in: packages/query-core/src/mutationCache.ts:231

Parameters

mutation

Mutation<any, any, any, any>

Returns

void


resumePausedMutations()

resumePausedMutations(): Promise<unknown>;

Defined in: packages/query-core/src/mutationCache.ts:325

Returns

Promise<unknown>


runNext()

runNext(mutation): Promise<unknown>;

Defined in: packages/query-core/src/mutationCache.ts:271

Parameters

mutation

Mutation<any, any, any, any>

Returns

Promise<unknown>


subscribe()

subscribe(listener): () => void;

Defined in: packages/query-core/src/subscribable.ts:8

Parameters

listener

MutationCacheListener

Returns

(): void;
Returns

void

Inherited from

Subscribable.subscribe