Skip to content

Commit 6d81263

Browse files
committed
queries and mutations pages
1 parent f9e3ad9 commit 6d81263

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

docs/rtk-query/usage/mutations.mdx

+56
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,59 @@ export const {
326326
allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"
327327
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
328328
></iframe>
329+
330+
## Runtime Validation using Schemas
331+
332+
Endpoints can use any [Standard Schema](https://standardschema.dev/) compliant library for runtime validation of various values. See [API reference](./api/createApi.mdx#schema-validation) for full list of available schemas.
333+
334+
Most commonly, you'll want to use `responseSchema` to validate the response from the server (or `rawResponseSchema` when using `transformResponse`).
335+
336+
```ts title="Using responseSchema" no-transpile
337+
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
338+
import * as v from 'valibot'
339+
340+
const postSchema = v.object({
341+
id: v.number(),
342+
name: v.string(),
343+
})
344+
type Post = v.InferOutput<typeof postSchema>
345+
type TransformedPost = Omit<Post, 'published_at'> & { published_at: Date }
346+
347+
const api = createApi({
348+
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
349+
endpoints: (build) => ({
350+
updatePost: build.mutation<Post, Partial<Post>>({
351+
query(data) {
352+
const { id, ...body } = data
353+
return {
354+
url: `post/${id}`,
355+
method: 'PUT',
356+
body,
357+
}
358+
},
359+
responseSchema: postSchema,
360+
}),
361+
updatePostWithTransform: build.mutation<TransformedPost, Partial<Post>>({
362+
query(data) {
363+
const { id, ...body } = data
364+
return {
365+
url: `post/${id}`,
366+
method: 'PUT',
367+
body,
368+
}
369+
},
370+
rawResponseSchema: postSchema,
371+
transformResponse: (response) => ({
372+
...response,
373+
published_at: new Date(response.published_at),
374+
}),
375+
// responseSchema can still be provided, to validate the transformed response
376+
responseSchema: v.object({
377+
id: v.number(),
378+
name: v.string(),
379+
published_at: v.date(),
380+
}),
381+
}),
382+
}),
383+
})
384+
```

docs/rtk-query/usage/queries.mdx

+42
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,48 @@ const { status, data, error, refetch } = dispatch(
350350

351351
:::
352352

353+
### Runtime Validation using Schemas
354+
355+
Endpoints can use any [Standard Schema](https://standardschema.dev/) compliant library for runtime validation of various values. See [API reference](./api/createApi.mdx#schema-validation) for full list of available schemas.
356+
357+
Most commonly, you'll want to use `responseSchema` to validate the response from the server (or `rawResponseSchema` when using `transformResponse`).
358+
359+
```ts title="Using responseSchema" no-transpile
360+
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
361+
import * as v from 'valibot'
362+
363+
const postSchema = v.object({
364+
id: v.number(),
365+
name: v.string(),
366+
})
367+
type Post = v.InferOutput<typeof postSchema>
368+
type TransformedPost = Omit<Post, 'published_at'> & { published_at: Date }
369+
370+
const api = createApi({
371+
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
372+
endpoints: (build) => ({
373+
getPost: build.query<Post, { id: number }>({
374+
query: ({ id }) => `/post/${id}`,
375+
responseSchema: postSchema,
376+
}),
377+
getTransformedPost: build.query<TransformedPost, { id: number }>({
378+
query: ({ id }) => `/post/${id}`,
379+
rawResponseSchema: postSchema,
380+
transformResponse: (response) => ({
381+
...response,
382+
published_at: new Date(response.published_at),
383+
}),
384+
// responseSchema can still be provided, to validate the transformed response
385+
responseSchema: v.object({
386+
id: v.number(),
387+
name: v.string(),
388+
published_at: v.date(),
389+
}),
390+
}),
391+
}),
392+
})
393+
```
394+
353395
## Example: Observing caching behavior
354396

355397
This example demonstrates request deduplication and caching behavior:

0 commit comments

Comments
 (0)