Closed AlexShukel closed 2 months ago
@AlexShukel if your modeling to the query keys doesn't allow you to access the shape that you want, you can just use javascript to remove the undesired elements. The awesome thing the type-safety that the lib provides is that you know exactly the shape of the key and what indexes you want to remove:
const trips = createQueryKeys('trips', {
page: (showCompleted: boolean, pageNumber: number) => ({
queryKey: [showCompleted, pageNumber],
queryFn: async () => {
// ...
},
}),
});
queryClient.invalidateQueries({
queryKey: trips.page(true, 0).toSpliced(-1) // <<<<<<<< ['trips', 'page', true]
})
Issue
I have the following query keys for "trips" query:
After that, in application I need an access to several keys:
However, I do not have access to ["trips", "page", boolean] query key:
Workaround with
contextQueries
It is possible to get those keys using
contextQueries
:But it isn't convenient to call 2 functions when accessing page's key and it looks pretty messy:
trips.showCompleted(false)._ctx.page(1).queryKey