codepod icon indicating copy to clipboard operation
codepod copied to clipboard

[prototype] switch backend from graphql to trpc

Open lihebi opened this issue 2 years ago • 0 comments

Rationale

Web APIs are hard to work with:

  • Working with GraphQL is very verbose and lacks type safety. As a result, updating APIs is a very slow process. We need to define a schema and then implement the backend resolvers. In the front end, we need to specify the schema again. There's no type-safety involved; you have to manually keep sync of backend-schema, backend-functions, frontend-schema, and frontend-calls.

  • REST is simpler but still lacks type safety. It is up to the developer to remember the /api/path/to/handler and the ?params=... and keep the frontend/backend in sync.

The real way of frontend/backend APIs should be remote procedure calls (RPCs). One simply defines a function at the backend and calls it from the front-end. How the call happens should be abstracted away. We have two RPCs: gRPC and tRPC.

  • gRPC is designed to allow communication among backend servers. It seems to be pretty awkward to make frontend to talk to backend (e.g., via gPRC-web or connectrpc). Both take significant work to set up, as far as I can tell.

  • tRPC is the closest to what we need. A backend function is callable from the front-end with minimal scaffolding and full type-safety.

About this PR

This PR is a prototype to switch from Graphql to tRPC. This PR only implements the server API. TODOs along this line:

  • [ ] Call /trpc endpoints from the frontend.
  • [ ] Since the desktop app and web app have different APIs, we likely need to get the desktop/web app fully separated first.

lihebi avatar Sep 30 '23 01:09 lihebi