Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/v6-mutation-construction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/query-core': major
---

Move mutation construction to `Mutation.build(client, options, state?)`. Replace calls to the internal `MutationCache.build` method with this static method. Query-only bundles can now omit mutation execution code.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, input, inputBinding, signal } from '@angular/core'
import { TestBed } from '@angular/core/testing'
import { Mutation } from '@tanstack/query-core'
import { createKey, sleep } from '@tanstack/query-test-utils'
import { render } from '@testing-library/angular'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
Expand Down Expand Up @@ -89,7 +90,7 @@ describe('injectIsMutating', () => {
})

it('should support signal reads in filter predicates', async () => {
const mutation = queryClient.getMutationCache().build(queryClient, {
const mutation = Mutation.build(queryClient, {
mutationFn: () => sleep(100).then(() => 'data'),
})
void mutation.execute(undefined)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
signal,
} from '@angular/core'
import { TestBed } from '@angular/core/testing'
import { Mutation } from '@tanstack/query-core'
import { sleep } from '@tanstack/query-test-utils'
import { render } from '@testing-library/angular'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
Expand Down Expand Up @@ -37,8 +38,8 @@ describe('injectMutationState', () => {

it('publishes selected mutation values unchanged and compares snapshot entries by identity', () => {
const version = signal('before')
queryClient.getMutationCache().build(queryClient, { mutationKey: ['a'] })
queryClient.getMutationCache().build(queryClient, { mutationKey: ['b'] })
Mutation.build(queryClient, { mutationKey: ['a'] })
Mutation.build(queryClient, { mutationKey: ['b'] })
let selected: Array<{ value: string }> = []
const states = TestBed.runInInjectionContext(() =>
injectMutationState(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/lit-query/src/__tests__/useMutationState.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryClient } from '@tanstack/query-core'
import { Mutation, QueryClient } from '@tanstack/query-core'
import { createKey, sleep } from '@tanstack/query-test-utils'
import { LitElement, html } from 'lit'
import {
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('useMutationState', () => {

it('should not request another update when mutation cache emits with unchanged selected state', async () => {
const mutationKey = createKey()
const existingMutation = queryClient.getMutationCache().build(queryClient, {
const existingMutation = Mutation.build(queryClient, {
mutationKey,
})

Expand Down
21 changes: 11 additions & 10 deletions packages/preact-query/src/__tests__/useMutationState.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Mutation } from '@tanstack/query-core'
import { createKey, sleep } from '@tanstack/query-test-utils'
import { fireEvent, render } from '@testing-library/preact'
import { useEffect } from 'preact/hooks'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, useIsMutating, useMutation, useMutationState } from '..'
import { renderWithClient } from './utils'
import type { Mutation, MutationCacheNotifyEvent } from '..'
import type { MutationCacheNotifyEvent } from '..'

describe('useIsMutating', () => {
beforeEach(() => {
Expand Down Expand Up @@ -214,7 +215,7 @@ describe('useIsMutating', () => {
const queryClient = new QueryClient()
const key = createKey()
const otherKey = createKey()
const mutation = queryClient.getMutationCache().build(queryClient, {
const mutation = Mutation.build(queryClient, {
mutationKey: key,
mutationFn: () => sleep(100),
})
Expand Down Expand Up @@ -251,8 +252,8 @@ describe('useMutationState', () => {
const key1 = createKey()
const key2 = createKey()
const cache = queryClient.getMutationCache()
cache.build(queryClient, { mutationKey: key1 })
cache.build(queryClient, { mutationKey: key2 })
Mutation.build(queryClient, { mutationKey: key1 })
Mutation.build(queryClient, { mutationKey: key2 })
const onCacheChange = vi.fn<(event: MutationCacheNotifyEvent) => void>()
const unsubscribe = cache.subscribe(onCacheChange)

Expand Down Expand Up @@ -283,7 +284,7 @@ describe('useMutationState', () => {
it('should update when select changes without a cache update', () => {
const queryClient = new QueryClient()
const key = createKey()
queryClient.getMutationCache().build(queryClient, { mutationKey: key })
Mutation.build(queryClient, { mutationKey: key })

function Page({ select }: { select: (mutation: Mutation) => unknown }) {
const result = useMutationState({ select })
Expand All @@ -308,7 +309,7 @@ describe('useMutationState', () => {
const queryClient = new QueryClient()
const key = createKey()
const cache = queryClient.getMutationCache()
const existingMutation = cache.build(queryClient, {
const existingMutation = Mutation.build(queryClient, {
mutationKey: key,
meta: { time: 0 },
})
Expand Down Expand Up @@ -357,7 +358,7 @@ describe('useMutationState', () => {
it('should render selector results that cannot be structurally shared', () => {
const queryClient = new QueryClient()
const key = createKey()
queryClient.getMutationCache().build(queryClient, { mutationKey: key })
Mutation.build(queryClient, { mutationKey: key })

function Page({ time }: { time: number }) {
const result = useMutationState({
Expand Down Expand Up @@ -456,7 +457,7 @@ describe('useMutationState', () => {
(inputs) => {
const queryClient = new QueryClient()
const key = createKey()
queryClient.getMutationCache().build(queryClient, { mutationKey: key })
Mutation.build(queryClient, { mutationKey: key })
const onResult =
vi.fn<
(result: Array<{ key: ReadonlyArray<unknown> | undefined }>) => void
Expand Down Expand Up @@ -493,8 +494,8 @@ describe('useMutationState', () => {
const client2 = new QueryClient()
const key1 = createKey()
const key2 = createKey()
client1.getMutationCache().build(client1, { mutationKey: key1 })
client2.getMutationCache().build(client2, { mutationKey: key2 })
Mutation.build(client1, { mutationKey: key1 })
Mutation.build(client2, { mutationKey: key2 })
const select = (mutation: Mutation) => mutation.options.mutationKey

function Page({ client }: { client: QueryClient }) {
Expand Down
28 changes: 14 additions & 14 deletions packages/query-core/src/__tests__/mutation.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expectTypeOf, it } from 'vitest'
import { MutationCache } from '../mutationCache'
import { Mutation } from '../mutation'
import { QueryClient } from '../queryClient'
import type { Action, Mutation, MutationState } from '../mutation'
import type { Action, MutationState } from '../mutation'
import type { MutationObserver } from '../mutationObserver'
import type {
DefaultError,
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('mutation', () => {

describe('state', () => {
it('should be the mutation state for the same type parameters', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('mutation', () => {

describe('options', () => {
it('should be the mutation options for the same type parameters', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand Down Expand Up @@ -262,7 +262,7 @@ describe('mutation', () => {

describe('setOptions', () => {
it('should take the mutation options and return nothing', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -286,7 +286,7 @@ describe('mutation', () => {

describe('meta', () => {
it('should be the mutation meta or undefined', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -300,7 +300,7 @@ describe('mutation', () => {
})

it('should be a getter without a setter', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -321,7 +321,7 @@ describe('mutation', () => {

describe('addObserver', () => {
it('should take a single observer and return nothing', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -340,7 +340,7 @@ describe('mutation', () => {

describe('removeObserver', () => {
it('should take a single observer and return nothing', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -359,7 +359,7 @@ describe('mutation', () => {

describe('continue', () => {
it('should take no arguments and resolve with an unknown value', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -376,7 +376,7 @@ describe('mutation', () => {

describe('execute', () => {
it('should take the variables and resolve with the data', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -393,7 +393,7 @@ describe('mutation', () => {
})

it('should reject variables of another type', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -412,7 +412,7 @@ describe('mutation', () => {

describe('gcTime', () => {
it('should be a number inherited from Removable', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -428,7 +428,7 @@ describe('mutation', () => {

describe('destroy', () => {
it('should take no arguments and return nothing', () => {
const mutation = new MutationCache().build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand Down
41 changes: 20 additions & 21 deletions packages/query-core/src/__tests__/mutation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createKey, sleep } from '@tanstack/query-test-utils'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { MutationCache, QueryClient, focusManager, onlineManager } from '..'
import { Mutation } from '../mutation'
import { MutationObserver } from '../mutationObserver'
import { executeMutation, mockOnlineManagerIsOnline } from './utils'
import type { MutationCacheNotifyEvent, MutationFunctionContext } from '..'
Expand Down Expand Up @@ -274,25 +275,23 @@ describe('mutation', () => {
onSettled,
})

const mutation = queryClient
.getMutationCache()
.build<string, unknown, string, string>(
queryClient,
{
mutationKey: key,
},
{
context: 'todo',
data: undefined,
error: null,
failureCount: 1,
failureReason: 'err',
isPaused: true,
status: 'pending',
variables: 'todo',
submittedAt: 1,
},
)
const mutation = Mutation.build<string, unknown, string, string>(
queryClient,
{
mutationKey: key,
},
{
context: 'todo',
data: undefined,
error: null,
failureCount: 1,
failureReason: 'err',
isPaused: true,
status: 'pending',
variables: 'todo',
submittedAt: 1,
},
)

expect(mutation.state).toStrictEqual({
context: 'todo',
Expand Down Expand Up @@ -349,7 +348,7 @@ describe('mutation', () => {
it('should not add an existing observer with addObserver', () => {
const mutationCache = queryClient.getMutationCache()
const observer = new MutationObserver(queryClient, {})
const currentMutation = mutationCache.build(queryClient, {})
const currentMutation = Mutation.build(queryClient, {})

const fn = vi.fn<(type: MutationCacheNotifyEvent['type']) => void>()

Expand Down Expand Up @@ -1595,7 +1594,7 @@ describe('mutation', () => {
sleep(10).then(() => 'data'),
)
// a mutation restored from a dehydrated pending state has no retryer yet
const mutation = queryClient.getMutationCache().build(
const mutation = Mutation.build(
queryClient,
{ mutationFn },
{
Expand Down
10 changes: 5 additions & 5 deletions packages/query-core/src/__tests__/mutationCache.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { afterEach, beforeEach, describe, expectTypeOf, it } from 'vitest'
import { MutationCache, QueryClient } from '..'
import { Mutation } from '../mutation'
import type {
DefaultError,
Mutation,
MutationCacheConfig,
MutationCacheNotifyEvent,
MutationFilters,
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('mutationCache', () => {
| 'subscribe'
| 'hasListeners'
| 'config'
| 'build'
| 'register'
| 'add'
| 'remove'
| 'canRun'
Expand Down Expand Up @@ -266,9 +266,9 @@ describe('mutationCache', () => {
})
})

describe('build', () => {
describe('Mutation.build', () => {
it('should carry its type parameters into the mutation it returns', () => {
const mutation = mutationCache.build<
const mutation = Mutation.build<
{ value: string },
CustomError,
number,
Expand All @@ -290,7 +290,7 @@ describe('mutationCache', () => {

it('should accept an optional initial state', () => {
expectTypeOf(
mutationCache.build<{ value: string }, CustomError, number, string>,
Mutation.build<{ value: string }, CustomError, number, string>,
)
.parameter(2)
.toEqualTypeOf<
Expand Down
Loading
Loading