trpc with Server Actions
Is it worth creating a trpc server purely for database queries, and then having mutations run by server actions? Originally I thought that server actions would cover everything, but I know they're specifically not recommended for querying data, only mutating.
However, I'm not sure if having a trpc server alongside that would be complicated from a DX point of view?
Not sure if I've explained that well, happy to clarify if needed.
4 Replies
i think mutations can be operated through the server, no server action is needed
If you're going to use server actions, that is what I would do. Server actions are not great for querying, unless you want to do a lot of route invalidation which might not be performant, but they do make certain data mutations a lot easier. Boiler plate of tRPC and generic API handlers to change a field or two is very annoying, but server actions handle that easily no issues
Tanstack query is also just great tooling for queries and you can't use that with server components / server actions
yes, the query is performed on the client side
@lanc3 so I believe you can use Tanstack Query with Server Actions, but obviously only from client. And I think in most cases where you wanted to fetch data that you owned, you can do that in better ways.
tRPC also provides a
react-query
provider which will integrate the two and provide type safety, which I think can be used for the same reason.
I think my plan would be:
- Server components fetch data - direct calls to db.
- Server components mutate data - not sure if this is really a thing? But if so then server actions
- Client components fetch data - tRPC with react-query
- Client components fetch data - server actions, possibly wrapped in react-query for integration with the RQ cache.