GraphQL
GraphQL and trPC
1. GraphQL: The Query Language for APIs
Theory: GraphQL is a query language for your API and a server-side runtime for executing those queries using a type system you define for your data.
Client-Driven: The client specifies exactly what data it needs, and the server returns only that data.
The Schema: Everything is built around a Schema Definition Language (SDL) that acts as a contract between the frontend and backend.
Single Endpoint: Unlike REST, which has multiple URLs, GraphQL typically uses a single
/graphqlendpoint for all requests.Operations: It uses Queries to fetch data, Mutations to modify data, and Subscriptions for real-time updates via WebSockets.
2. trPC: Type-Safe Remote Procedure Calls
Theory: trPC (TypeScript Remote Procedure Call) allows you to build deeply integrated, type-safe APIs without the need for a schema or code generation. It effectively "blurs the line" between your frontend and backend.
TypeScript Only: trPC is designed specifically for TypeScript environments.
Zero-Schema / Zero-Codegen: It uses TypeScript's type inference to export your backend router's types directly to your frontend. If you change a function signature on the backend, your frontend code will immediately show an error in your IDE.
No "Over-fetching" (By Design): Like GraphQL, it only sends the data requested, but it does so by calling functions directly rather than parsing a query string.
GraphQL Resolvers
Theory: A resolver is a function that populates the data for a single field in your schema. If a client asks for user { posts }, GraphQL calls the user resolver first, then the posts resolver for that specific user.
trPC Procedures
Theory: In trPC, you define Procedures (Queries or Mutations) in a router. Because the types are shared, the frontend gets full autocomplete for the input parameters and the output data structure automatically.
Architectural Impact
GraphQL is ideal when you have a diverse ecosystem where different clients (Mobile, Web, IoT) need different slices of data from a complex backend.
trPC is the "speed-run" choice for developers building a TypeScript-based application where the same person or team manages both the frontend and backend.