|
1 | 1 | --- |
2 | | -title: Tags Manager API |
| 2 | +title: Tags API |
3 | 3 | sidebar_position: 20 |
4 | 4 | --- |
5 | 5 |
|
6 | | -Work in progress. This page is not finished yet. |
| 6 | +The Tags API provides imperative methods invalidate or update tags in the cache. |
| 7 | +It can be accessed via [`builder.tags`](./builder#tags) in the `QueryBuilder` instance. |
| 8 | + |
| 9 | +## Methods |
| 10 | + |
| 11 | +### useOperation |
| 12 | + |
| 13 | +```ts |
| 14 | +useOperation(options: TagOperationOptions): [operate, MutationResult] |
| 15 | +``` |
| 16 | + |
| 17 | +This is a hook that returns a function to operate on the tags, and the result of the operation. |
| 18 | +This function uses the `useMutation` hook under the hood, so it can be used in a similar way. |
| 19 | +For example the `operate` function can be awaited, or `isPending` of the result be used to check if the operation is pending. |
| 20 | + |
| 21 | +### operate |
| 22 | + |
| 23 | +```ts |
| 24 | +operate(options: TagOperationOptions): Promise<void> |
| 25 | +``` |
| 26 | + |
| 27 | +This function behaves like the `useOperation` hook, but can also be used outside of React components. |
| 28 | + |
| 29 | +There are also shorthands for all of the operations, which are: `invalidate`, `refetch`, `reset`, `cancel`, and `remove`. |
| 30 | +These shorthands have the same signature as the `operate` function with the `operation` option set to the corresponding operation. |
| 31 | + |
| 32 | +### update |
| 33 | + |
| 34 | +```ts |
| 35 | +update(options: TagUpdateOptions): Promise<void> |
| 36 | +``` |
| 37 | + |
| 38 | +This function updates the queries that are tagged with the specified tags. |
| 39 | +It can be used to update the queries in the cache without refetching them. |
| 40 | +You don't need to use this function for most cases, it can be useful in some advanced scenarios. |
| 41 | + |
| 42 | +## Types |
| 43 | + |
| 44 | +### TagOperationOptions |
| 45 | + |
| 46 | +```ts |
| 47 | +type TagOperationOptions = { |
| 48 | + tags?: TagList; |
| 49 | + operation?: "invalidate" | "refetch" | "reset" | "cancel" | "remove"; |
| 50 | + filters?: InvalidateQueryFilters; |
| 51 | + options?: InvalidateOptions; |
| 52 | +}; |
| 53 | +``` |
| 54 | + |
| 55 | +### TagList |
| 56 | + |
| 57 | +This type is used to define the tags that will be used in operations like invalidate or update. |
| 58 | +It can be a single tag, an array of tags, or a function that returns a tag list. Each tag can be a string, an object with a `type` and `id`. |
| 59 | + |
| 60 | +Some examples: |
| 61 | + |
| 62 | +```ts |
| 63 | +// When defining tags: |
| 64 | +builder.withTags("articles"); |
| 65 | +builder.withTags(({ data }) => ({ type: "article", id: data.id })); |
| 66 | +builder.withTags("articles", "entities"); |
| 67 | + |
| 68 | +// Usage in Tags APIs: |
| 69 | +builder.tags.invalidate({ tags: "articles" }); |
| 70 | +builder.tags.invalidate({ tags: { type: "article", id: 1 } }); |
| 71 | +builder.tags.invalidate({ tags: ["articles", { type: "article", id: 1 }] }); |
| 72 | + |
| 73 | +// When updating tags: |
| 74 | +builder.withUpdates(({ vars }) => ({ |
| 75 | + type: "article", |
| 76 | + id: vars.params.id, |
| 77 | + optimistic: true, |
| 78 | + updater: () => { |
| 79 | + /* update logic */ |
| 80 | + }, |
| 81 | +})); |
| 82 | +``` |
0 commit comments