Justin Cook's Blog.

When to use tRPC vs GraphQL

Cover Image for 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. GraphQL API

Pros

  1. Define types without requiring TypeScript
  2. Scalable: rules defined in one schema, can be improved on gradually
  3. Efficient: fetch just data required, not entire endpoint payload

Cons

  1. Can be challenging to build schemas that work well if not expert
  2. Version management can get messy if running old application version
  3. n + 1 problem can be a expensive performance cost. What is it and potential solutions.

Use Cases

  1. You want to separate your backend and frontend (or work from two repos)
  2. You're scaling your backend and need clear rules for your API
  3. The application has increasingly more complex and intricate requirements
  4. Typescript isn’t an option

tRPC

tRPC stands for TypeScript Remote Procedure Call - a lightweight library for remotely calling backend functions on client side tRPC

  1. Requires TypeScript on both your backend and frontend
  2. Frontend uses composable procedures to remotely call backend data
  3. Queries to fetch
  4. Mutations to create, update and delete
  5. Quickly build working APIs without any schemas

Use Cases

  1. You want to bring the backend and frontend closer together
  2. You don't have complex needs
  3. Your app needs to be more lightweight
  4. Using Next.js - tRPC helps connect Next's back and frontend