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

SuccessContext.svelte

33 lines920 Bytes

1<script lang="ts">
2 import { sleep } from '@tanstack/query-test-utils'
3 import { createMutation, setQueryClientContext } from '../../src/index.js'
4 import type {
5 MutationFunctionContext,
6 MutationKey,
7 QueryClient,
8 } from '@tanstack/query-core'
9​
10 type Props = {
11 queryClient: QueryClient
12 mutationKey?: MutationKey
13 onSuccessMock: (
14 data: string,
15 variables: string,
16 onMutateResult: { startedWith: string },
17 context: MutationFunctionContext,
18 ) => void
19 }
20​
21 let { queryClient, mutationKey, onSuccessMock }: Props = $props()
22​
23 setQueryClientContext(queryClient)
24​
25 const mutation = createMutation(() => ({
26 mutationKey,
27 mutationFn: (text: string) => sleep(10).then(() => text.toUpperCase()),
28 onMutate: (text: string) => ({ startedWith: text }),
29 onSuccess: onSuccessMock,
30 }))
31</script>
32​
33<button onclick={() => mutation.mutate('todo')}>Mutate</button>