Skip to content

Commit 250d704

Browse files
committed
make tags unavailable when client isn't provided
1 parent e1a927e commit 250d704

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ Following code loads a list of Posts from an API, and presents a Delete button t
3535
When a Post is deleted, the list query is automatically invalidated and refetched to show the up-to-date data.
3636

3737
```tsx
38+
import { QueryClient } from "@tanstack/react-query";
3839
import { HttpQueryBuilder } from "react-query-builder";
3940

4041
type PostData = { id: number; title: string; body: string; userId: number };
4142

43+
const client = new QueryClient();
44+
4245
const builder = new HttpQueryBuilder()
46+
.withClient(client)
4347
.withBaseUrl("https://jsonplaceholder.typicode.com")
4448
.withTagTypes<{
4549
posts: PostData[];
@@ -57,7 +61,9 @@ const deletePostMutation = builder
5761
.withPath("/posts/:id");
5862

5963
export function MyApp() {
60-
const [refresh] = builder.tags.useOperation({ tags: "refreshable" });
64+
const [refresh, { isPending: isRefreshing }] = builder.tags.useOperation({
65+
tags: "refreshable",
66+
});
6167
const posts = postsQuery.useQuery({});
6268
const deletePost = deletePostMutation.useMutation();
6369

@@ -67,7 +73,9 @@ export function MyApp() {
6773

6874
return (
6975
<>
70-
<button onClick={() => refresh()}>Refresh all posts</button>
76+
<button onClick={() => refresh()} disabled={isRefreshing}>
77+
Refresh all posts
78+
</button>
7179

7280
{posts.data.map((post) => (
7381
<div key={post.id}>

examples/vite/src/SimpleExample.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { QueryClient } from '@tanstack/react-query';
12
import { HttpQueryBuilder } from 'react-query-builder';
23

34
type PostData = { id: number; title: string; body: string; userId: number };
45

5-
const builder = new HttpQueryBuilder().withBaseUrl('https://jsonplaceholder.typicode.com').withTagTypes<{
6+
const client = new QueryClient();
7+
8+
const builder = new HttpQueryBuilder().withClient(client).withBaseUrl('https://jsonplaceholder.typicode.com').withTagTypes<{
69
posts: PostData[];
710
refreshable: unknown;
811
}>();
@@ -12,7 +15,7 @@ const postsQuery = builder.withTags('refreshable', 'posts').withPath('/posts').w
1215
const deletePostMutation = builder.withUpdates('posts').withMethod('delete').withPath('/posts/:id');
1316

1417
export function MyApp() {
15-
const [refresh] = builder.tags.useOperation({ tags: 'refreshable' });
18+
const [refresh, { isPending: isRefreshing }] = builder.tags.useOperation({ tags: 'refreshable' });
1619
const posts = postsQuery.useQuery({});
1720
const deletePost = deletePostMutation.useMutation();
1821

@@ -22,7 +25,9 @@ export function MyApp() {
2225

2326
return (
2427
<>
25-
<button onClick={() => refresh()}>Refresh all posts</button>
28+
<button onClick={() => refresh()} disabled={isRefreshing}>
29+
Refresh all posts
30+
</button>
2631

2732
{posts.data.map((post) => (
2833
<div key={post.id}>

packages/react-query-builder/src/builder/QueryBuilderFrozen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export class QueryBuilderFrozen<
318318
}
319319

320320
private _tags?: QueryBuilderTagsManager<TVars, TData, TError, TKey, TTags>;
321-
get tags() {
322-
return (this._tags ??= new QueryBuilderTagsManager(this));
321+
get tags(): HasClient<TFlags, QueryBuilderTagsManager<TVars, TData, TError, TKey, TTags>> {
322+
return (this._tags ??= new QueryBuilderTagsManager(this)) as any;
323323
}
324324
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { WithOptional } from '../type-utils';
66
import type { QueryBuilderFrozen } from './QueryBuilderFrozen';
77

88
export class QueryBuilderTagsManager<TVars, TData, TError, TKey extends unknown[], TTags extends Record<string, unknown>> {
9-
constructor(private builder: QueryBuilderFrozen<TVars, TData, TError, TKey, TTags>) {}
9+
constructor(private builder: QueryBuilderFrozen<TVars, TData, TError, TKey, TTags, any>) {}
1010

1111
/**
1212
* This hook returns a function that can be used to operate on queries based on tags.

0 commit comments

Comments
 (0)