T
TanStack20h ago
like-gold

Error handling with `queryClient.fetchQuery`

Hi! I have a useQuery that used to manage fetching events that occur between two dates. The previous implementation expected external state, so this was no problem before. However, we are migrating to a different library, and it has a very different structure. It instead expects me to handle (start: Date, end: Date, onSuccess: (events: ...[]) => void. Most of the query hook is easily transferable to queryClient.fetchQuery (select, success handling, endpoint etc.). However, One thing I lose out on is the query.isError functionality, which is kind of a shame, because I know that this query can fail due to reasons that the user should know about. Maybe it's a mutation I'm looking for? I'm only reading data, so I'm not sure if I sacrifice some other feature by using that. Do you have suggestions?
2 Replies
foreign-sapphire
foreign-sapphire20h ago
fetchQuery rejects the promise when there is an error so with try/catch or .catch() you should get error handling
like-gold
like-goldOP20h ago
oh ... that sounds pretty good. So you suggest fetchQuery with external error state for this case?

Did you find this page helpful?