mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 12:14:40 +01:00
update merging routers example
This commit is contained in:
@@ -2,21 +2,11 @@
|
||||
|
||||
Writing all API-code in your code in the same file is not a great idea. It's easy to merge routers with other routers.
|
||||
|
||||
Define your routes:
|
||||
|
||||
```ts
|
||||
// ~/server/trpc/routes/posts.ts
|
||||
export const posts = trpc.router()
|
||||
.mutation('create', {
|
||||
input: z.object({
|
||||
title: z.string(),
|
||||
}),
|
||||
resolve: ({ input }) => {
|
||||
// ..
|
||||
return {
|
||||
id: 'xxxx',
|
||||
...input,
|
||||
}
|
||||
},
|
||||
})
|
||||
.query('list', {
|
||||
resolve() {
|
||||
// ..
|
||||
@@ -45,3 +35,12 @@ export const router = trpc.router()
|
||||
.merge('user.', users) // prefix user procedures with "user."
|
||||
.merge('post.', posts) // prefix post procedures with "post."
|
||||
```
|
||||
|
||||
and use it like this:
|
||||
|
||||
```html
|
||||
<script setup lang="ts">
|
||||
const { data: users } = await useAsyncQuery(['user.list'])
|
||||
const { data: posts } = await useAsyncQuery(['post.list'])
|
||||
</script>
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user