This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathclient.ts
More file actions
41 lines (39 loc) · 1.54 KB
/
client.ts
File metadata and controls
41 lines (39 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { QueryClient } from '@tanstack/query-core';
import type { InvalidationPredicate, QueryInfo } from '@zenstackhq/client-helpers';
import { parseQueryKey } from './query-key.js';
export function invalidateQueriesMatchingPredicate(queryClient: QueryClient, predicate: InvalidationPredicate) {
return queryClient.invalidateQueries({
predicate: ({ queryKey }) => {
const parsed = parseQueryKey(queryKey);
if (!parsed) {
return false;
}
return predicate({ model: parsed.model as string, args: parsed.args });
},
});
}
export function getAllQueries(queryClient: QueryClient): readonly QueryInfo[] {
return queryClient
.getQueryCache()
.getAll()
.map(({ queryKey, state }) => {
const parsed = parseQueryKey(queryKey);
if (!parsed) {
return undefined;
}
return {
model: parsed?.model,
operation: parsed?.operation,
args: parsed?.args,
data: state.data,
optimisticUpdate: !!parsed.flags.optimisticUpdate,
updateData: (data: unknown, cancelOnTheFlyQueries: boolean) => {
queryClient.setQueryData<unknown>(queryKey, data);
if (cancelOnTheFlyQueries) {
queryClient.cancelQueries({ queryKey }, { revert: false, silent: true });
}
},
};
})
.filter((entry) => !!entry);
}