MCPcopy Create free account
hub / github.com/TanStack/query / execute

Method execute

packages/query-core/src/mutation.ts:301–466  ·  view source on GitHub ↗

* Runs the mutation function for the given variables through a retryer, and * drives the mutation's state and lifecycle callbacks through to settlement. * * If this mutation's state is already `pending` when `execute` is called * (i.e. it was restored, still in-flight, from a dehydrated

(variables: TVariables)

Source from the content-addressed store, hash-verified

299 * ```
300 */
301 async execute(variables: TVariables): Promise<TData> {
302 const onContinue = () => {
303 this.#dispatch({ type: 'continue' })
304 }
305
306 const mutationFnContext = {
307 client: this.#client,
308 meta: this.options.meta,
309 mutationKey: this.options.mutationKey,
310 } satisfies MutationFunctionContext
311
312 const retryer = (this.#retryer = createRetryer({
313 fn: () => {
314 if (!this.options.mutationFn) {
315 return Promise.reject(new Error('No mutationFn found'))
316 }
317
318 return this.options.mutationFn(variables, mutationFnContext)
319 },
320 onFail: (failureCount, error) => {
321 this.#dispatch({ type: 'failed', failureCount, error })
322 },
323 onPause: () => {
324 this.#dispatch({ type: 'pause' })
325 },
326 onContinue,
327 retry: this.options.retry ?? 0,
328 retryDelay: this.options.retryDelay,
329 networkMode: this.options.networkMode,
330 canRun: () => this.#mutationCache.canRun(this),
331 }))
332
333 const restored = this.state.status === 'pending'
334 const isPaused = !retryer.canStart()
335
336 try {
337 if (restored) {
338 // Dispatch continue action to unpause restored mutation
339 onContinue()
340 } else {
341 this.#dispatch({ type: 'pending', variables, isPaused })
342 // Notify cache callback
343 if (this.#mutationCache.config.onMutate) {
344 await this.#mutationCache.config.onMutate(
345 variables,
346 this as Mutation<unknown, unknown, unknown, unknown>,
347 mutationFnContext,
348 )
349 }
350 const context = await this.options.onMutate?.(
351 variables,
352 mutationFnContext,
353 )
354 if (context !== this.state.context) {
355 this.#dispatch({
356 type: 'pending',
357 context,
358 variables,

Callers 15

continueMethod · 0.95
transformUsagesFunction · 0.80
transformUsagesFunction · 0.80
Devtools.test.tsxFile · 0.80
executeFunction · 0.80

Calls 5

#dispatchMethod · 0.95
createRetryerFunction · 0.90
canRunMethod · 0.80
startMethod · 0.80
runNextMethod · 0.80

Tested by 2

executeFunction · 0.64
executeMutationFunction · 0.64