You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/the-type-safe-guide-to-trpc.md
+29-15Lines changed: 29 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,32 +9,40 @@ alt = "trpcBlogCover"
9
9
image = "/blog/uploads/trpcblog.png"
10
10
11
11
+++
12
-
13
12
This isn't the best guide to use tRPC, probably there are better ways to do this, like [create-t3-app](https://create.t3.gg/), the best I could find.
14
13
15
14
Most of what is here is from the [tRPC's documentation](https://trpc.io/docs), you can refer them, super helpful and easy to read.
16
15
17
16
**What is tRPC?**
17
+
18
+
18
19
tRPC is a typescript library, so to say, that makes it easy to create type-safe APIs without schema or any sort of code generation.
19
20
20
21
**Where to use?**
21
-
Create the *typed server* and then import its type and use it with an adaptor in the client side.
22
+
23
+
24
+
Create the _typed server_ and then import its type and use it with an adaptor in the client side.
22
25
23
26
**How does it implement type-safety?**
27
+
28
+
24
29
tRPC encourages using the [zod](https://www.npmjs.com/package/zod), a library for type validation of input and output arguments.
25
30
26
31
**Is tRPC only limited to React?**
32
+
33
+
27
34
tRPC's core API is built to work with any client, but right now it supports **React** and can be used with **React Meta Frameworks** like **NextJS** or **SolidJS**, since it uses **React Query** under the hood to talk to the server and maintaining type-safety across the data-pipeline or data-flow.
28
35
29
36
For now, it has first-party adaptors for **React**, **NextJS**, **Express**, **Fastify**, **SolidJS**, and some community packages like for [**tRPC for SveleteKit**](https://github.com/icflorescu/trpc-sveltekit)
30
37
31
38
**What are its features?**
32
-
- Lightweight, a tiny bundle size for such a powerful library.
33
-
- Type-safe to the max!
34
-
- Support subscriptions with **websockets** library.
35
-
- Request batching
36
-
- Request can be made simultaneously and then are batched into one.
37
-
- Strong User base and helpful Community
39
+
40
+
* Lightweight, a tiny bundle size for such a powerful library.
41
+
* Type-safe to the max!
42
+
* Support subscriptions with **websockets** library.
43
+
* Request batching
44
+
* Request can be made simultaneously and then are batched into one.
45
+
* Strong User base and helpful Community
38
46
39
47
## tRPC x NextJS
40
48
@@ -71,6 +79,7 @@ This is the router where the actual business logic will reside, create a `backen
> We can create router without using the above context by just using `trpc.router()` that will work just fine. But if you are using some external API like in the above case we are using prisma, it's better to use pass in repeatedly used instances to context to avoid having multiple ones for every query we use that in, that may *affect our performance and can also be vulnerable*.
123
+
>
124
+
> We can create router without using the above context by just using `trpc.router()` that will work just fine. But if you are using some external API like in the above case we are using prisma, it's better to use pass in repeatedly used instances to context to avoid having multiple ones for every query we use that in, that may _affect our performance and can also be vulnerable_.
115
125
116
126
`src/server/router/index.ts`
127
+
117
128
```ts
118
129
import {createRouter} from"./contex";
119
130
import {exampleRouter} from"./example.router";
@@ -134,6 +145,7 @@ export type AppRouter = typeof appRouter;
134
145
> The exact filename is necessary to make this work!
Now that tRPC is set up, this is how we use it inside react components.
171
184
172
185
`src/pages/index.tsx`
186
+
173
187
```tsx
174
188
// we use the instance we created that has our router type definitions
175
189
import { trpc } from"@/utils/trpc";
@@ -180,9 +194,8 @@ export default SomePage() {
180
194
}
181
195
```
182
196
183
-
184
-
185
197
### SSG Helpers
198
+
186
199
SSG Helpers are helper functions that can be used to pre-fetch queries on the server upon request to reduce loading time.
187
200
188
201
They are to be used when working with SSR and SSG or ISR.
@@ -224,6 +237,7 @@ export default function PostPage(props: InferGetServerSidePropsType<typeof getSe
224
237
```
225
238
226
239
**References**
227
-
- Check out [this](https://www.youtube.com/watch?v=I5tWWYBdlJo) amazing talk by [Theo](https://twitter.com/t3dotgg) on tRPC vs GraphQL and their risks.
228
-
- Check out Theo on YouTube or any other social media platform, he has a lot of content about tRPC
229
-
- Follow [Alex](https://twitter.com/alexdotjs) aka Katt, the creator of tRPC.
240
+
241
+
* Check out [this](https://www.youtube.com/watch?v=I5tWWYBdlJo) amazing talk by [Theo](https://twitter.com/t3dotgg) on tRPC vs GraphQL and their risks.
242
+
* Check out Theo on YouTube or any other social media platform, he has a lot of content about tRPC
243
+
* Follow [Alex](https://twitter.com/alexdotjs) aka Katt, the creator of tRPC.
0 commit comments