My useQuery is calling an API (/posts/:idOrSlug where each resource can be identified by either one of two keys - an ID or a slug. Initially the frontend only has the slug but not the ID, but the useQuery() network call returns the ID.
How do I make it so that the react-query cache 1. stores each item only once (instead of duplicating for slug and ID), and 2. preferably uses just the ID as the primary key (since ID is immutable and the slug could theoretically change)?
Essentially I want the key from useQuery() to change from slug to ID. Originally I was going to manually update the cache by adding an onSuccess to useQuery(), but that callback is getting deprecated (and I imagine there must be a cleaner way anyways).
To elaborate more on my specific use case - imagine a blogging application where the frontend initially only has the post slug from the URL but not the post ID, but calling GET /posts/:idOrSlug returns the post ID. I'm not exactly sure what to set the queryKey in the useQuery() to get this desired functionality, again since I initially don't know what the ID is when calling useQuery.
EDIT: So what I'm doing now is manually setting the react-query cache inside the queryFn of the useQuery(), using queryClient.setQueryData(["posts", { id: postData.id }], data);. Then I just created a local mapping of slugs to IDs, and if I know the ID of a slug, I use the ID in the queryKey. This works, but the downside again is that cache data is duplicated since it's stored by slug and by ID. Also I think this is fairly ugly since the cache is imperatively being updated here, when a more declarative approach would be cleaner - but react-query doesn't seem to enable a declarative approach to setting cache IDs (at least it's nowhere in the docs).