T
TanStack•3y ago
adverse-sapphire

Best pattern to "create on error"?

I have a query that queries for an object that may or may not exist. If that object doesn't exist, it should automatically run the mutation to create it. What's the best pattern to do this? (I'm using tRPC if that matters at all) My initial thought was to throw an error inside the endpoint if the object doesn't exist and then provide an onError to useQuery inside which I would run the mutation, but onError doesn't seem to exist?
1 Reply
optimistic-gold
optimistic-gold•3y ago
Does the query have all the information needed to create the object in case it doesn't exist yet? IMO this logic belongs to your backend. If you query for an non-existing object it should be created "on the fly" and returned afterwards (if this satisfies your business requirements) Still a little weird to create something on a GET 🤔 Maybe you can give more context to give another/better answer

Did you find this page helpful?