Skip to content

Commit de9abc0

Browse files
committed
chore: playground
1 parent 58aa516 commit de9abc0

File tree

4 files changed

+110
-9
lines changed

4 files changed

+110
-9
lines changed

playground/src/main.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
createWebHistory,
66
DataLoaderPlugin,
77
} from 'vue-router/auto'
8-
import { VueQueryPlugin } from '@tanstack/vue-query'
8+
import { MutationCache, QueryCache, VueQueryPlugin } from '@tanstack/vue-query'
99
import { createPinia } from 'pinia'
1010
import { QueryPlugin } from '@pinia/colada'
1111

@@ -21,7 +21,20 @@ const app = createApp(App)
2121

2222
app.use(createPinia())
2323
app.use(QueryPlugin)
24-
app.use(VueQueryPlugin, {})
24+
app.use(VueQueryPlugin, {
25+
queryClientConfig: {
26+
mutationCache: new MutationCache({
27+
onSuccess(data, vars, context, mutation) {
28+
// debugger
29+
mutation
30+
},
31+
async onSettled(...args) {
32+
await new Promise((r) => setTimeout(r, 1000))
33+
console.log('global onSettled', ...args)
34+
},
35+
}),
36+
},
37+
})
2538
app.use(DataLoaderPlugin, { router })
2639
app.use(router)
2740

playground/src/pages/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ definePage({
99
<template>
1010
<main>
1111
<h1>Home</h1>
12+
<pre>{{ $route.meta }}</pre>
1213
</main>
1314
</template>
1415

playground/src/pages/[email protected]

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
<script lang="ts" setup>
2+
definePage({
3+
meta: {
4+
named: 'index',
5+
},
6+
})
7+
</script>
8+
19
<template>
210
<main>
311
<h1>Home - named view</h1>

playground/src/pages/users/query.[id].vue

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type TypesConfig,
77
type RouteRecordName,
88
} from 'unplugin-vue-router/types'
9+
import { computed } from 'vue'
910
const a: RouteRecordName = '/articles'
1011
1112
// import type { RouteLocationNormalized, _RouteLocationNormalized } from 'vue-router/auto'
@@ -80,7 +81,7 @@ const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
8081
</script>
8182

8283
<script lang="ts" setup>
83-
import { useQuery } from '@tanstack/vue-query'
84+
import { useMutation, useQuery } from '@tanstack/vue-query'
8485
const route = useRoute('/users/[id]')
8586
8687
// const { data: user, isLoading, error } = useUserData()
@@ -89,10 +90,14 @@ const {
8990
error: tqError,
9091
status,
9192
fetchStatus,
93+
refetch,
9294
} = useQuery({
9395
async queryFn() {
94-
console.log('[TQ]useUserData', route.fullPath)
96+
console.log('[TQ]useUserData', route.fullPath, route.params)
9597
await delay(500)
98+
if (route.params.id === '0') {
99+
throw new Error('no user')
100+
}
96101
const user = {
97102
id: route.params.id,
98103
// @ts-expect-error: no param "name"!
@@ -102,9 +107,61 @@ const {
102107
return user
103108
},
104109
// FIXME: (to) => ['user-id', to.params.id]
105-
queryKey: ['users', () => route.params.id],
110+
queryKey: ['users', route.params.id],
106111
staleTime: 5000,
112+
retry: 0,
113+
})
114+
115+
const {
116+
mutateAsync: mutate,
117+
data,
118+
context,
119+
status: mutationStatus,
120+
error,
121+
} = useMutation({
122+
mutationFn: async (id: number) => {
123+
console.log('mutate', id)
124+
await delay(id)
125+
// throw new Error('data ' + id)
126+
return id
127+
},
128+
mutationKey: [
129+
'mutate',
130+
() => {
131+
console.log('mutationKey')
132+
return 205
133+
},
134+
],
135+
// mutationKey: () => {
136+
// console.log('mutationKey')
137+
// return ['mutate']
138+
// },
139+
onMutate: (id) => {
140+
console.log('onMutate', id)
141+
// throw new Error('hello')
142+
return { id }
143+
},
144+
onSuccess: (newData, vars, context) => {
145+
console.log('onSuccess', newData, data.value, vars, context)
146+
// throw new Error('onSuccess')
147+
},
148+
onError: (err) => {
149+
console.log('onError', err)
150+
// throw new Error('onError')
151+
},
152+
async onSettled(data, error) {
153+
await new Promise((r) => setTimeout(r, 100))
154+
console.log('onSettled', data, error)
155+
throw new Error('onSettled')
156+
},
157+
retry: false,
107158
})
159+
160+
function multipleMutate() {
161+
mutate(100).then((d) => console.log('DATA 100', d))
162+
mutate(200).then((d) => console.log('DATA 200', d))
163+
mutate(50).then((d) => console.log('DATA 50', d))
164+
}
108165
</script>
109166

110167
<template>
@@ -119,22 +176,44 @@ const {
119176
<RouterLink :to="{ params: { id: Number(route.params.id) + 1 } }"
120177
>Next</RouterLink
121178
>
179+
|
180+
<button
181+
@click="
182+
refetch().then((d) => {
183+
console.log('Got ', d)
184+
})
185+
"
186+
>
187+
Refresh
188+
</button>
122189

123-
<h2>Data Loaders</h2>
190+
<!-- <h2>Data Loaders</h2>
124191
<pre v-if="isLoading">Loading...</pre>
125192
<pre v-else-if="error">Error: {{ error }}</pre>
126193
<pre v-else>{{ user }}</pre>
127194
128195
<hr />
129196
130-
<h2>TQ</h2>
197+
-->
198+
199+
<h2>Query</h2>
131200

132201
<p>
133202
<code>status: {{ status }}</code>
134203
<br />
135204
<code>fetchStatus: {{ fetchStatus }}</code>
136205
</p>
137-
<pre v-if="tqError">Error: {{ tqError }}</pre>
138-
<pre v-else>{{ tqUser == null ? String(tqUser) : tqUser }}</pre>
206+
<pre>Error: {{ tqError }}</pre>
207+
<pre>User: {{ tqUser == null ? String(tqUser) : tqUser }}</pre>
208+
209+
<hr />
210+
211+
<h2>Mutations</h2>
212+
213+
<pre>Context: {{ context }}</pre>
214+
<pre>status: {{ mutationStatus }}</pre>
215+
<pre>error: {{ error }}</pre>
216+
<button @click="mutate(123)">Mutate</button>
217+
<button @click="multipleMutate()">Multi Mutate</button>
139218
</main>
140219
</template>

0 commit comments

Comments
 (0)