trpc / trpc

πŸ§™β€β™€οΈ Move Fast and Break Nothing. End-to-end typesafe APIs made easy.
https://tRPC.io
MIT License
34.64k stars 1.23k forks source link

feat: router-level arguments #5966

Open timvandam opened 1 month ago

timvandam commented 1 month ago

Describe the feature you'd like to request

I would like to be able to capture arguments at the router-level and have these propagate down to procedures. This can help deduplicate code but most importantly could make it easier to invalidate nested routers more precisely.

For instance, if we have a resource named Post we could create a router with an argument named id. Then, rather than invalidating the full Post router, we could invalidate the router for one specific id. This would make it much easier to accurately invalidate exactly what you want to invalidate.

Currently, your options would be to either manually invalidate all queries for Post that require id one by one, or to just invalidate the full Post router, for any id. They are both suboptimal; the former is not resistant to the scope of your router expanding, while the latter invalidates over eagerly

Describe the solution you'd like to see

In the backend a simple .input available on routers would do. The parsed input would then be made available to all inner procedures.

On the front-end, the proxy would now capture function calls to reference parameterized routers. E.g. we could use trpc.post({ id: 'xyz'}).mutate({ title: 'Hello, world!' }) and utils.post({ id: 'xyz'}).invalidate().

Describe alternate solutions

As mentioned, currently you could invalidate the full router or trust yourself enough (I don't) to always keep every mutation up to date

Additional information

No response

πŸ‘¨β€πŸ‘§β€πŸ‘¦ Contributing

Funding

Fund with Polar

timvandam commented 1 month ago

This could integrate nicely with mutation scopes to ensure that a subset of mutations all share the same scope (e.g. <some-resource>-<some-resource-id>) https://tanstack.com/query/latest/docs/framework/react/guides/mutations#mutation-scopes