Skip to content

Commit 5ea5f7c

Browse files
committed
update tags api
1 parent 51fa49b commit 5ea5f7c

File tree

5 files changed

+85
-10
lines changed

5 files changed

+85
-10
lines changed

packages/tanstack-query-builder/src/builder/QueryBuilderTagsManager.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@ export class QueryBuilderTagsManager<TVars, TData, TError, TKey extends unknown[
4949
*/
5050
update({
5151
tags = [],
52-
optimistic = false,
5352
client = this.builder.config.queryClient,
5453
...ctx
5554
}: {
5655
tags: readonly QueryUpdateTag<TVars, TData, TError, TTags>[];
57-
optimistic?: boolean;
5856
} & WithOptional<QueryTagContext<TVars, TData, TError>, 'client'>) {
59-
updateTags({ tags, queryClient: client!, ctx: { client: client!, ...ctx }, optimistic });
57+
updateTags({ tags, queryClient: client!, ctx: { client: client!, ...ctx } });
6058
}
6159
}

packages/tanstack-query-builder/src/builder/createUpdateMiddleware.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export const createUpdateMiddleware: CreateUpdateMiddleware = (tags) =>
2525
queryClient: ctx.client,
2626
tags: preUpdates.filter((x) => x.updater),
2727
ctx: preCtx,
28-
optimistic: true,
2928
});
3029

3130
invalidates.push(...preUpdates);

packages/tanstack-query-builder/src/tags/updateTags.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ export function updateTags({
1919
tags,
2020
queryClient,
2121
ctx,
22-
optimistic,
2322
}: {
2423
tags: readonly QueryUpdateTag<any, any, any, any>[];
2524
queryClient: QueryClient;
2625
ctx: QueryTagContext<unknown>;
27-
optimistic?: boolean;
2826
}): UpdateTagsUndoer[] {
2927
if (!tags?.length) return [];
3028

@@ -76,7 +74,7 @@ export function updateTags({
7674

7775
let observer: QueryObserver<any, any> | InfiniteQueryObserver<any, any> | null = null;
7876

79-
const updateType = optimistic ? ('optimistic' as const) : ('pessimistic' as const);
77+
const updateType = tag.optimistic ? ('optimistic' as const) : ('pessimistic' as const);
8078
const meta = { updated: updateType };
8179

8280
let subscribePaused = false;

website/docs/api/builder.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,18 @@ get client(): QueryBuilderClient
144144
Exposes some methods to perform imperative operations on the query client, such as `prefetch`, `invalidate`, `refetch`, `remove` and more.
145145
See [Client API Reference](./client) to learn more.
146146

147+
Note that this API is only available if [`withClient`](#withclient) was called.
148+
147149
### tags
148150

149151
```ts
150152
get tags(): QueryBuilderTagsManager
151153
```
152154

153155
Exposes some methods to perform imperative tag operations.
154-
See [Tags Manager API Reference](./tags) to learn more.
156+
See [Tags API Reference](./tags) to learn more.
157+
158+
Note that this API is only available if [`withClient`](#withclient) was called.
155159

156160
## HTTP Builder API
157161

website/docs/api/tags.mdx

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,82 @@
11
---
2-
title: Tags Manager API
2+
title: Tags API
33
sidebar_position: 20
44
---
55

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

Comments
 (0)