Skip to content

Commit 80a675e

Browse files
fix(svelte-5-adapter): function for createMutation options (#7805)
* fix(svelte-5-adapter): require function for createMutation options * Fix type errors
1 parent 8f190fd commit 80a675e

File tree

16 files changed

+46
-35
lines changed

16 files changed

+46
-35
lines changed

examples/svelte/auto-refetching/src/routes/+page.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
refetchInterval: intervalMs,
2020
}))
2121
22-
const addMutation = createMutation({
22+
const addMutation = createMutation(() => ({
2323
mutationFn: (value: string) =>
2424
fetch(`${endpoint}?add=${value}`).then((r) => r.json()),
2525
onSuccess: () => client.invalidateQueries({ queryKey: ['refetch'] }),
26-
})
26+
}))
2727
28-
const clearMutation = createMutation({
28+
const clearMutation = createMutation(() => ({
2929
mutationFn: () => fetch(`${endpoint}?clear=1`).then((r) => r.json()),
3030
onSuccess: () => client.invalidateQueries({ queryKey: ['refetch'] }),
31-
})
31+
}))
3232
</script>
3333

3434
<h1>Auto Refetch with stale-time set to 1s</h1>

examples/svelte/optimistic-updates/src/routes/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
queryFn: fetchTodos,
4242
}))
4343
44-
const addTodoMutation = createMutation({
44+
const addTodoMutation = createMutation(() => ({
4545
mutationFn: createTodo,
4646
onMutate: async (newTodo: string) => {
4747
text = ''
@@ -74,7 +74,7 @@
7474
onSettled: () => {
7575
client.invalidateQueries({ queryKey: ['optimistic'] })
7676
},
77-
})
77+
}))
7878
</script>
7979

8080
<h1>Optimistic Updates</h1>

examples/svelte/playground/src/routes/AddTodo.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
})
4141
}
4242
43-
const addMutation = createMutation({
43+
const addMutation = createMutation(() => ({
4444
mutationFn: postTodo,
4545
onSuccess: () => {
4646
queryClient.invalidateQueries({ queryKey: ['todos'] })
4747
},
48-
})
48+
}))
4949
</script>
5050

5151
<div>

examples/svelte/playground/src/routes/EditTodo.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@
7373
enabled: editingIndex.value !== null,
7474
}))
7575
76-
const saveMutation = createMutation({
76+
const saveMutation = createMutation(() => ({
7777
mutationFn: patchTodo,
7878
onSuccess: (data) => {
7979
// Update `todos` and the individual todo queries when this mutation succeeds
8080
queryClient.invalidateQueries({ queryKey: ['todos'] })
8181
queryClient.setQueryData(['todo', { id: editingIndex }], data)
8282
},
83-
})
83+
}))
8484
8585
const todo = $derived(query.data)
8686

packages/svelte-query-persist-client/tests/FreshData/FreshData.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030

3131
<div>data: {query.data ?? 'undefined'}</div>
3232
<div>fetchStatus: {query.fetchStatus}</div>
33+
<div>fetched: {fetched}</div>

packages/svelte-query/src/createBaseQuery.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function createBaseQuery<
3333

3434
/** Creates a store that has the default options applied */
3535
function updateOptions() {
36-
const queryKey: TQueryKey = $state.snapshot(options().queryKey) as any // remove proxy prevent reactive query in devTools
36+
const queryKey: TQueryKey = $state.snapshot(options().queryKey) as any // remove proxy prevent reactive query in DevTools
3737
const defaultedOptions = client.defaultQueryOptions({
3838
...options(),
3939
queryKey: queryKey,

packages/svelte-query/src/createMutation.svelte.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
CreateMutateFunction,
77
CreateMutationOptions,
88
CreateMutationResult,
9+
FunctionedParams,
910
} from './types'
1011

1112
import type { DefaultError, QueryClient } from '@tanstack/query-core'
@@ -16,13 +17,18 @@ export function createMutation<
1617
TVariables = void,
1718
TContext = unknown,
1819
>(
19-
options: CreateMutationOptions<TData, TError, TVariables, TContext>,
20+
options: FunctionedParams<
21+
CreateMutationOptions<TData, TError, TVariables, TContext>
22+
>,
2023
queryClient?: QueryClient,
2124
): CreateMutationResult<TData, TError, TVariables, TContext> {
2225
const client = useQueryClient(queryClient)
2326

2427
const observer = $derived(
25-
new MutationObserver<TData, TError, TVariables, TContext>(client, options),
28+
new MutationObserver<TData, TError, TVariables, TContext>(
29+
client,
30+
options(),
31+
),
2632
)
2733
const mutate = $state<
2834
CreateMutateFunction<TData, TError, TVariables, TContext>
@@ -31,7 +37,7 @@ export function createMutation<
3137
})
3238

3339
$effect.pre(() => {
34-
observer.setOptions(options)
40+
observer.setOptions(options())
3541
})
3642

3743
const result = $state(observer.getCurrentResult())

packages/svelte-query/tests/createInfiniteQuery/BaseExample.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
)
2424
2525
$effect(() => {
26+
// @ts-expect-error
2627
states.value = [...untrack(() => states.value), $state.snapshot(query)]
2728
})
2829
</script>

packages/svelte-query/tests/createInfiniteQuery/SelectExample.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
)
2424
2525
$effect(() => {
26+
// @ts-expect-error
2627
states.value = [...untrack(() => states.value), $state.snapshot(query)]
2728
})
2829
</script>

packages/svelte-query/tests/createMutation/FailureExample.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
const queryClient = new QueryClient()
1313
setQueryClientContext(queryClient)
1414
15-
const mutation = createMutation({ mutationFn: mutationFn })
15+
const mutation = createMutation(() => ({ mutationFn: mutationFn }))
1616
</script>
1717

1818
<button onclick={() => mutation.mutate({ count: ++count })}>Mutate</button>

packages/svelte-query/tests/createMutation/OnSuccessExample.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
const queryClient = new QueryClient()
1212
setQueryClientContext(queryClient)
1313
14-
const mutation = createMutation({
14+
const mutation = createMutation(() => ({
1515
mutationFn: (vars: { count: number }) => Promise.resolve(vars.count),
1616
onSuccess: (data) => {
1717
onSuccessMock(data)
1818
},
1919
onSettled: (data) => {
2020
onSettledMock(data)
2121
},
22-
})
22+
}))
2323
</script>
2424

2525
<button onclick={() => mutation.mutate({ count: ++$count })}>Mutate</button>

packages/svelte-query/tests/createMutation/ResetExample.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
const queryClient = new QueryClient()
66
setQueryClientContext(queryClient)
77
8-
const mutation = createMutation({
8+
const mutation = createMutation(() => ({
99
mutationFn: () => {
1010
const err = new Error('Expected mock error')
1111
err.stack = ''
1212
return Promise.reject(err)
1313
},
14-
})
14+
}))
1515
</script>
1616

1717
<button onclick={() => mutation.reset()}>Reset</button>

packages/svelte-query/tests/createQuery/PlaceholderData.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
)
2828
2929
$effect(() => {
30+
// @ts-expect-error
3031
states.value = [...untrack(() => states.value), $state.snapshot(query)]
3132
})
3233
</script>

packages/svelte-query/tests/useIsMutating/BaseExample.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
const isMutating = useIsMutating(undefined, queryClient)
99
1010
const mutation = createMutation(
11-
{
11+
() => ({
1212
mutationKey: ['mutation-1'],
1313
mutationFn: async () => {
1414
await sleep(5)
1515
return 'data'
1616
},
17-
},
17+
}),
1818
queryClient,
1919
)
2020
</script>

packages/svelte-query/tests/useMutationState/BaseExample.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { useMutationState } from '../../src/useMutationState.svelte'
66
import type {
77
CreateMutationOptions,
8+
FunctionedParams,
89
MutationStateOptions,
910
} from '../../src/index'
1011
@@ -13,8 +14,8 @@
1314
errorMutationOpts,
1415
mutationStateOpts,
1516
}: {
16-
successMutationOpts: CreateMutationOptions
17-
errorMutationOpts: CreateMutationOptions
17+
successMutationOpts: FunctionedParams<CreateMutationOptions>
18+
errorMutationOpts: FunctionedParams<CreateMutationOptions>
1819
mutationStateOpts?: MutationStateOptions | undefined
1920
} = $props()
2021

packages/svelte-query/tests/useMutationState/useMutationState.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ describe('useMutationState', () => {
1212

1313
const rendered = render(BaseExample, {
1414
props: {
15-
successMutationOpts: {
15+
successMutationOpts: () => ({
1616
mutationKey: ['success'],
1717
mutationFn: successMutationFn,
18-
},
18+
}),
1919

20-
errorMutationOpts: {
20+
errorMutationOpts: () => ({
2121
mutationKey: ['error'],
2222
mutationFn: errorMutationFn,
23-
},
23+
}),
2424
},
2525
})
2626

@@ -49,15 +49,15 @@ describe('useMutationState', () => {
4949

5050
const rendered = render(BaseExample, {
5151
props: {
52-
successMutationOpts: {
52+
successMutationOpts: () => ({
5353
mutationKey: ['success'],
5454
mutationFn: successMutationFn,
55-
},
55+
}),
5656

57-
errorMutationOpts: {
57+
errorMutationOpts: () => ({
5858
mutationKey: ['error'],
5959
mutationFn: errorMutationFn,
60-
},
60+
}),
6161

6262
mutationStateOpts: {
6363
filters: { status: 'error' },
@@ -88,15 +88,15 @@ describe('useMutationState', () => {
8888

8989
const rendered = render(BaseExample, {
9090
props: {
91-
successMutationOpts: {
91+
successMutationOpts: () => ({
9292
mutationKey: ['success'],
9393
mutationFn: successMutationFn,
94-
},
94+
}),
9595

96-
errorMutationOpts: {
96+
errorMutationOpts: () => ({
9797
mutationKey: ['error'],
9898
mutationFn: errorMutationFn,
99-
},
99+
}),
100100

101101
mutationStateOpts: {
102102
filters: { mutationKey: ['success'] },

0 commit comments

Comments
 (0)