mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-26 18:42:49 +00:00
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { MutationCache, QueryClient } from "@tanstack/react-query";
|
|
|
|
import type { InvalidatesMeta } from "@/utils/endpoint";
|
|
|
|
/**
|
|
* Single app-wide React Query client (do not nest additional providers).
|
|
*
|
|
* Declarative invalidation: any mutation built via `api.*.mutationOptions()`
|
|
* (see `services/api.ts` + `utils/endpoint.ts`) carries an `invalidates`
|
|
* function in its `meta`. The shared `MutationCache` below runs it on success
|
|
* and invalidates the returned query keys — so invalidation is declared once in
|
|
* the endpoint definition rather than re-wired in every component.
|
|
*/
|
|
export const queryClient = new QueryClient({
|
|
mutationCache: new MutationCache({
|
|
onSuccess: (data, variables, _context, mutation) => {
|
|
const invalidates = mutation.meta?.invalidates as
|
|
| InvalidatesMeta
|
|
| undefined;
|
|
if (typeof invalidates !== "function") return;
|
|
|
|
for (const queryKey of invalidates(variables, data)) {
|
|
void queryClient.invalidateQueries({ queryKey });
|
|
}
|
|
},
|
|
}),
|
|
defaultOptions: {
|
|
queries: {
|
|
retry: 1,
|
|
staleTime: 30_000,
|
|
},
|
|
},
|
|
});
|