mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 12:14:40 +01:00
update readme
This commit is contained in:
34
README.md
34
README.md
@@ -34,18 +34,24 @@ Expose your tRPC [routes](https://trpc.io/docs/router) under `~/server/trpc/inde
|
|||||||
// ~/server/trpc/index.ts
|
// ~/server/trpc/index.ts
|
||||||
import type { inferAsyncReturnType } from '@trpc/server'
|
import type { inferAsyncReturnType } from '@trpc/server'
|
||||||
import * as trpc from '@trpc/server'
|
import * as trpc from '@trpc/server'
|
||||||
|
import { z } from 'zod' // yup/superstruct/zod/myzod/custom
|
||||||
|
|
||||||
export const router = trpc
|
export const router = trpc.router()
|
||||||
.router<inferAsyncReturnType<typeof createContext>>()
|
|
||||||
// queries and mutations...
|
// queries and mutations...
|
||||||
.query('hello', {
|
.query('getUsers', {
|
||||||
resolve: () => 'world',
|
async resolve(req) {
|
||||||
|
// use your ORM of choice
|
||||||
|
return await UserModel.all()
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.query('bye', {
|
.mutation('createUser', {
|
||||||
resolve() {
|
// validate input with Zod
|
||||||
return {
|
input: z.object({ name: z.string().min(5) }),
|
||||||
text: 'goodbye',
|
async resolve(req) {
|
||||||
}
|
// use your ORM of choice
|
||||||
|
return await UserModel.create({
|
||||||
|
data: req.input,
|
||||||
|
})
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@@ -55,11 +61,13 @@ Use the client like so:
|
|||||||
```ts
|
```ts
|
||||||
const client = useClient() // auto-imported
|
const client = useClient() // auto-imported
|
||||||
|
|
||||||
const greeting = await client.query('hello')
|
const users = await client.query('getUsers')
|
||||||
console.log(greeting) // => 👈 world
|
|
||||||
|
|
||||||
const farewell = await client.query('bye')
|
const addUser = async () => {
|
||||||
console.log(farewell) // => 👈 goodbye
|
const mutate = await client.mutation('createUser', {
|
||||||
|
name: 'wagmi'
|
||||||
|
})
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## useAsyncQuery
|
## useAsyncQuery
|
||||||
|
|||||||
Reference in New Issue
Block a user