개념
RPC란 무엇이며, 어떤 마인드셋을 가져야 하나요?
그저 함수일 뿐입니다
RPC는 "Remote Procedure Call(원격 프로시저 호출)"의 약자입니다. 이는 한 컴퓨터(서버)의 함수를 다른 컴퓨터(클라이언트)에서 호출하는 방식입니다. 전통적인 HTTP/REST API에서는 URL을 호출하여 응답을 받습니다. 반면 RPC에서는 함수를 호출하여 응답을 받습니다.
ts// HTTP/RESTconst res = await fetch('/api/users/1');const user = await res.json();// RPCconst user = await api.users.getById({ id: 1 });
ts// HTTP/RESTconst res = await fetch('/api/users/1');const user = await res.json();// RPCconst user = await api.users.getById({ id: 1 });
tRPC(TypeScript Remote Procedure Call)는 RPC의 한 구현체로, TypeScript 모노레포를 위해 설계되었습니다. 자체적인 특징이 있지만, 그 핵심은 RPC입니다.
HTTP/REST 구현 세부사항에 대해 생각하지 마세요
tRPC 앱의 네트워크 트래픽을 검사하면 상당히 표준적인 HTTP 요청과 응답을 볼 수 있지만, 애플리케이션 코드를 작성할 때 구현 세부사항에 대해 생각할 필요가 없습니다. 함수를 호출하면 tRPC가 나머지를 처리합니다. HTTP 동사(Verbs)와 같은 세부사항은 REST API에서는 의미를 지니지만, RPC에서는 함수 이름의 일부로 사용되므로 무시해야 합니다. 예를 들어, GET /users/:id 대신 getUser(id)를 사용합니다.
용어
아래는 tRPC 생태계에서 자주 사용되는 용어들입니다. 문서 전반에서 이 용어들을 사용할 예정이므로 익숙해지는 것이 좋습니다. 이 개념들의 대부분은 문서에 별도의 페이지가 있습니다.
| Term | Description |
|---|---|
| Procedure ↗ | API endpoint - can be a query, mutation, or subscription. |
| Query | A procedure that gets some data. |
| Mutation | A procedure that creates, updates, or deletes some data. |
| Subscription ↗ | A procedure that creates a persistent connection and listens to changes. |
| Router ↗ | A collection of procedures (and/or other routers) under a shared namespace. |
| Context ↗ | Stuff that every procedure can access. Commonly used for things like session state and database connections. |
| Middleware ↗ | A function that can run code before and after a procedure. Can modify context. |
| Validation ↗ | "Does this input data contain the right stuff?" |