Optimistic updates not working tRPC useQuery

So I'm currently trying to implement an optimistic data update with tRPC. I'm following the docs. So far if I just do onSetteled it works fine and the data refetches after a second or two. Now I'm trying to implement the onMutate portion, and for some reason my queryClient.getQueryData() returns undefined, even though I'm passing it the same query that I pass invalidateQueries. Any insight on what I might be missing with the syntax?
42 Replies
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
Hmm interesting, I did
const previousTodos = await queryClient.getQueryData(['userRouter.getUser'])
console.log(previousTodos)
const previousTodos = await queryClient.getQueryData(['userRouter.getUser'])
console.log(previousTodos)
And the console log is still undefined
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
Sorry if it's obvious, but I don't understand the distinction between awaiting the query data and waiting for the data to come back
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
But the above example would be in the ../router/router.ts file, no? I'm not exactly sure why queryClient.getQueryData(['userRouter.getUser']) would involve 2 queries. Aren't I just requesting the cached data from the query? And if so, why would awaiting on the .getQueryDate not be sufficient?
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
Version 9.27.2
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
Ya I'm looking at the docs But isn't my client fine if queryClient.invalidateQueries(['userRouter.getUser']) is working just fine?
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
Optimistic Updates | TanStack Query Docs
When you optimistically update your state before performing a mutation, there is a chance that the mutation will fail. In most of these failure cases, you can just trigger a refetch for your optimistic queries to revert them to their true server state. In some circumstances though, refetching may not work correctly and the mutation error could ...
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
Instead of setQueryData?
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
But that behavior is already handled by onSettled: invalidateQuery.... onMutate should change it immediately and the invalidateQuery will make sure it's updated from the backend
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
XD
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
No that's just the syntax from the docs, I copy and pasted it. I'm trying to get the behavior to be correct then was gonna bother to rename everything The problem is: why doesn't const previousTodos = await queryClient.getQueryData(['userRouter.getUser']) return anything, just ignore the setQueryData line to make it simpler
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
That's exactly it not sure how I didn't think of that lol
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Ben
Ben•3y ago
Long story short your mutation should look something like this(without the type errors ideally 😅 ) Make sure your variable names are consistent across your mutation/optimistic update and prisma model/backend, otherwise the data will obviously have different keys in your optimistic object vs the one you get back from your query
julius
julius•3y ago
GitHub
stocks/transactions.tsx at e69c9db6f4a2b669717018b8f9c2f84ab9e92dd7...
Track all your stock purchases at one place. Contribute to juliusmarminge/stocks development by creating an account on GitHub.
julius
julius•3y ago
That’s how I did it
zenith
zenith•3y ago
I'm stumped on this one too I have it exactly like julius now except for that I'm using v10
zenith
zenith•3y ago
Here's the code
julius
julius•3y ago
Yea there is a v10 bug with the inference there, Will be fixed next beta. For now just type it any or smth
julius
julius•3y ago
GitHub
bug: utils.setData updaterFn has implicit any by juliusmarminge · P...
Closes # 🎯 Changes What changes are made in this PR? Is it a feature or a bug fix? Updates @tanstack/react-query peerDependency to ^4.3.0 and resolves a private import to fix utils.setData((prev) ...
zenith
zenith•3y ago
oh that's not the issue altho thanks My issue is that both of those console logs show undefined
julius
julius•3y ago
The old in your code is implicit any Oh thats weird
zenith
zenith•3y ago
my getData in there returns undefined yeah and old as well
julius
julius•3y ago
You need go give it an id right? getLessonList.getData({ studyId })
zenith
zenith•3y ago
OH I was thinking in a totally different way about that but that will be it I'm sure
julius
julius•3y ago
Is the input optional or Else thats another bug that its not yelling at you
zenith
zenith•3y ago
input could be an empty string this has fixed it Ok so it's half fixed it GOT IT in setData the query has to come after the callback function Here's the final code for future reference
zenith
zenith•3y ago
cje
cje•3y ago
you can get better typing if you add a condition like if (prevData) { do optimistic update stuff }
julius
julius•3y ago
apparently this is not needed in v4, you can return undefined from setData and the queryCache wont be set
cje
cje•3y ago
im not sure about trpc off the top of my head, but vanilla rq v4's getData is still typed as TData | undefined so annoying stuff happens when you try to spread it thats what the old: any is doing in that screenshot
julius
julius•3y ago
the any is cause we have a bug in trpc tk said this https://github.com/trpc/trpc/pull/2758#discussion_r976491445 but yea ofc it can be undefined in the function, but you are now able to return undefined so you dont need to do the if (!old) return { someMockedStructure } fyi its fixed in beta.2
Want results from more Discord servers?
Add your server