invalidateQueries(...)
The method programmatically invalidates cached data within the QueryClient
.
Refer to the TanStack Query Invalidation ๐ด
guide for more information.
- With
filters
- No
filters
Invalidates queries for the specified endpoint using the provided filters.
qraft.<service>.<operation>.invalidateQueries(
filters
)
Argumentsโ
filters: QueryFiltersByParameters | QueryFiltersByQueryKey
- Required, represents the Query Filters ๐ด to be used, strictly-typed โจ
filters.parameters: { path, query, header }
will be used for filtering queries by parametersfilters.infinite: boolean
will be used to filter infinite or normal queriesfilters.queryKey: QueryKey
will be used for filtering queries by QueryKey instead of parametersfilters.queryKey
andfilters.parameters
are mutually exclusive
filters.predicate?: (query: Query) => boolean
will be used for filtering queries by custom predicate- If not provided
- All queries for the specified endpoint will be invalidated
Invalidates all normal and Infinite queries for the specified endpoint.
qraft.<service>.<operation>.invalidateQueries()
Returnsโ
Promise<void>
: A promise that resolves once the invalidation is complete.
Examplesโ
filters
filters
predicate
queryKey
Queries invalidation with the specified parameters:
/**
* Will invalidate queries with the specified parameters:
* ###
* GET /entities/3e3e-3e3e-3e3e
* x-monite-version: 2023-09-01
**/
qraft.entities.getEntities.invalidateQueries(
{
infinite: false,
parameters: {
header: {
'x-monite-version': '2023-09-01',
},
path: {
entity_id: '3e3e-3e3e-3e3e',
},
},
}
);
To invalidate all queries for a particular endpoint, you can call invalidateQueries(...)
without parameters
:
/**
* Will invalidate queries matching the specified endpoint:
* ###
* GET /entities/3e3e-3e3e-3e3e
* x-monite-version: 2023-09-01
* ###
* GET /entities/4c4c-4c4c-4c4c
* x-monite-version: 2023-09-01
* ###
* โฌ๏ธ All queries for the specified endpoint will be invalidated
**/
qraft.entities.getEntities.invalidateQueries();
Invalidates queries with a custom predicate(...)
function,
which will be used as a final filter on all matching queries.
See Query Filters ๐ด
for more information.
/**
* Will invalidate queries matching the specified endpoint and predicate:
* ###
* GET /entities/3e3e-3e3e-3e3e
* x-monite-version: 2023-09-01
**/
qraft.entities.getEntities.invalidateQueries(
{
infinite: false,
parameters, // * optional, or specific parameters, alternatively, you can use `queryKey`
predicate: (query) => {
// `queryKey`โฌ๏ธ is fully typed to `qraft.entities.getEntities` operation parameters
if (query.queryKey[1].path.entity_id === '4c4c-4c4c-4c4c') return false;
return true;
},
}
);
It could be useful to invalidate queries using queryKey
directly:
/**
* Will invalidate queries matching the specified endpoint:
* ###
* GET /entities/3e3e-3e3e-3e3e
* x-monite-version: 2023-09-01
**/
qraft.entities.getEntities.invalidateQueries(
{
// `queryKey` is fully typed to `qraft.entities.getEntities`
queryKey: qraft.entities.getEntities.getQueryKey({
header: {
'x-monite-version': '2023-09-01',
},
path: {
entity_id: '3e3e-3e3e-3e3e',
},
}),
}
);