When to use tRPC vs GraphQL
Justin Cook
Justin Cook
tRPC is a lightweight alternative to GraphQL. This article is a quick pointform overview, offering pros and cons of each and suggested use cases.
GraphQL
GraphQL is a querying language that allows you to create schemas that describe all the data the client can request, using a recursive structure.
Pros
- Define types without requiring TypeScript
- Scalable: rules defined in one schema, can be improved on gradually
- Efficient: fetch just data required, not entire endpoint payload
Cons
- Can be challenging to build schemas that work well if not expert
- Version management can get messy if running old application version
- n + 1 problem can be a expensive performance cost. What is it and potential solutions.
Use Cases
- You want to separate your backend and frontend (or work from two repos)
- You're scaling your backend and need clear rules for your API
- The application has increasingly more complex and intricate requirements
- Typescript isn’t an option
tRPC
tRPC stands for TypeScript Remote Procedure Call - a lightweight library for remotely calling backend functions on client side
- Requires TypeScript on both your backend and frontend
- Frontend uses composable procedures to remotely call backend data
- Queries to fetch
- Mutations to create, update and delete
- Quickly build working APIs without any schemas
Use Cases
- You want to bring the backend and frontend closer together
- You don't have complex needs
- Your app needs to be more lightweight
- Using Next.js - tRPC helps connect Next's back and frontend
- Cover image by by Alex Azabache on Unsplash