Skip to content

Commit 38a40ae

Browse files
committed
infinite queries
1 parent 048df38 commit 38a40ae

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

docs/rtk-query/usage/infinite-queries.mdx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,59 @@ const projectsApi = createApi({
531531
}),
532532
})
533533
```
534+
535+
## Runtime Validation using Schemas
536+
537+
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.
538+
539+
Most commonly, you'll want to use `responseSchema` to validate the response from the server (or `rawResponseSchema` when using `transformResponse`).
540+
541+
```ts title="Using responseSchema" no-transpile
542+
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
543+
import * as v from 'valibot'
544+
545+
const pokemonSchema = v.object({
546+
id: v.number(),
547+
name: v.string(),
548+
})
549+
type Pokemon = v.InferOutput<typeof pokemonSchema>
550+
const transformedPokemonSchema = v.object({
551+
...pokemonSchema.entries,
552+
id: v.string(),
553+
})
554+
type TransformedPokemon = v.InferOutput<typeof transformedPokemonSchema>
555+
556+
const api = createApi({
557+
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com/pokemon' }),
558+
endpoints: (build) => ({
559+
getInfinitePokemon: build.infiniteQuery<Pokemon[], string, number>({
560+
query: ({ queryArg, pageParam }) => `type/${queryArg}?page=${pageParam}`,
561+
// argSchema for infinite queries must have both queryArg and pageParam
562+
argSchema: v.object({
563+
queryArg: v.string(),
564+
pageParam: v.number(),
565+
}),
566+
responseSchema: v.array(pokemonSchema),
567+
}),
568+
getTransformedPokemon: build.infiniteQuery<
569+
TransformedPokemon[],
570+
string,
571+
number
572+
>({
573+
query: ({ queryArg, pageParam }) => `type/${queryArg}?page=${pageParam}`,
574+
argSchema: v.object({
575+
queryArg: v.string(),
576+
pageParam: v.number(),
577+
}),
578+
rawResponseSchema: v.array(pokemonSchema),
579+
transformResponse: (response) =>
580+
response.map((pokemon) => ({
581+
...pokemon,
582+
id: String(pokemon.id),
583+
})),
584+
// responseSchema can still be provided, to validate the transformed response
585+
responseSchema: v.array(transformedPokemonSchema),
586+
}),
587+
}),
588+
})
589+
```

docs/rtk-query/usage/mutations.mdx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,14 @@ import * as v from 'valibot'
340340
const postSchema = v.object({
341341
id: v.number(),
342342
name: v.string(),
343+
published_at: v.string(),
343344
})
344345
type Post = v.InferOutput<typeof postSchema>
345-
type TransformedPost = Omit<Post, 'published_at'> & { published_at: Date }
346+
const transformedPost = v.object({
347+
...postSchema.entries,
348+
published_at: v.date(),
349+
})
350+
type TransformedPost = v.InferOutput<typeof transformedPost>
346351

347352
const api = createApi({
348353
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
@@ -373,11 +378,7 @@ const api = createApi({
373378
published_at: new Date(response.published_at),
374379
}),
375380
// 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+
responseSchema: transformedPost,
381382
}),
382383
}),
383384
})

docs/rtk-query/usage/queries.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ const postSchema = v.object({
365365
name: v.string(),
366366
})
367367
type Post = v.InferOutput<typeof postSchema>
368-
type TransformedPost = Omit<Post, 'published_at'> & { published_at: Date }
368+
const transformedPost = v.object({
369+
...postSchema.entries,
370+
published_at: v.date(),
371+
})
372+
type TransformedPost = v.InferOutput<typeof transformedPost>
369373

370374
const api = createApi({
371375
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
@@ -382,11 +386,7 @@ const api = createApi({
382386
published_at: new Date(response.published_at),
383387
}),
384388
// 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-
}),
389+
responseSchema: transformedPost,
390390
}),
391391
}),
392392
})

0 commit comments

Comments
 (0)