You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -26,6 +26,30 @@ See also [`baseQuery API Reference`](../api/createApi.mdx#basequery).
26
26
27
27
RTK Query expects a `baseQuery` function to be called with three arguments: `args`, `api`, and `extraOptions`. It is expected to return an object with either a `data` or `error` property, or a promise that resolves to return such an object.
28
28
29
+
:::tip
30
+
31
+
Base query and query functions must _always_ catch errors themselves, and return it in an object!
32
+
33
+
```ts no-transpile
34
+
function brokenCustomBaseQuery() {
35
+
// ❌ Don't let this throw by itself
36
+
const data =awaitfetchSomeData()
37
+
return { data }
38
+
}
39
+
40
+
function correctCustomBaseQuery() {
41
+
// ✅ Catch errors and _return_ them so the RTKQ logic can track it
42
+
try {
43
+
const data =awaitfetchSomeData()
44
+
return { data }
45
+
} catch (error) {
46
+
return { error }
47
+
}
48
+
}
49
+
```
50
+
51
+
:::
52
+
29
53
#### baseQuery function arguments
30
54
31
55
```ts title="baseQuery example arguments" no-transpile
@@ -205,7 +229,11 @@ argument, which can be used while determining the transformed response. The valu
205
229
dependent on the `baseQuery` used.
206
230
207
231
```ts title="transformErrorResponse meta example" no-transpile
Individual endpoints on [`createApi`](../api/createApi.mdx) accept a [`queryFn`](../api/createApi.mdx#queryfn) property which allows a given endpoint to ignore `baseQuery` for that endpoint by providing an inline function determining how that query resolves.
259
+
RTK Query comes with `fetchBaseQuery` out of the box, which makes it straightforward to define endpoints that talk to HTTP URLs (such as a typical REST API). We also have integrations with GraphQL as well. However, at its core, RTK Query is really about tracking loading state and cached values for _any_ async request/response sequence, not just HTTP requests.
232
260
233
-
This can be useful for scenarios where you want to have particularly different behaviour for a single endpoint, or where the query itself is not relevant. Such situations may include:
261
+
RTK Query supports defining endpoints that run arbitrary async logic and return a result. Individual endpoints on [`createApi`](../api/createApi.mdx) accept a [`queryFn`](../api/createApi.mdx#queryfn) property, which let you write your own async function with whatever logic you want inside.
262
+
263
+
This can be useful for scenarios where you want to have particularly different behaviour for a single endpoint, or where the query itself is not relevant, including:
234
264
235
265
- One-off queries that use a different base URL
236
266
- One-off queries that use different request handling, such as automatic re-tries
237
267
- One-off queries that use different error handling behaviour
268
+
- Queries that make requests using a third-party library SDK, such as Firebase or Supabase
269
+
- Queries that perform async tasks that are not a typical request/response
238
270
- Performing multiple requests with a single query ([example](#performing-multiple-requests-with-a-single-query))
239
271
- Leveraging invalidation behaviour with no relevant query ([example](#using-a-no-op-queryfn))
240
272
- Using [Streaming Updates](./streaming-updates) with no relevant initial request ([example](#streaming-data-with-no-initial-request))
@@ -243,7 +275,39 @@ See also [`queryFn API Reference`](../api/createApi.mdx#queryfn) for the type si
243
275
244
276
### Implementing a `queryFn`
245
277
246
-
In order to use `queryFn`, it can be treated as an inline `baseQuery`. It will be called with the same arguments as `baseQuery`, as well as the provided `baseQuery` function itself (`arg`, `api`, `extraOptions`, and `baseQuery`). Similarly to `baseQuery`, it is expected to return an object with either a `data` or `error` property, or a promise that resolves to return such an object.
278
+
A `queryFn` can be thought of as an inline `baseQuery`. It will be called with the same arguments as `baseQuery`, as well as the provided `baseQuery` function itself (`arg`, `api`, `extraOptions`, and `baseQuery`). Similarly to `baseQuery`, it is expected to return an object with either a `data` or `error` property, or a promise that resolves to return such an object.
You could also try creating a custom base query that uses the SDK, and define endpoints that pass method names or args into that base query.
968
+
883
969
### Using a no-op queryFn
884
970
885
971
In certain scenarios, you may wish to have a `query` or `mutation` where sending a request or returning data is not relevant for the situation. Such a scenario would be to leverage the `invalidatesTags` property to force re-fetch specific `tags` that have been provided to the cache.
0 commit comments