Skip to content

Commit 92698cd

Browse files
celeroncoderForestry.io
authored andcommitted
Update from Forestry.io
Khushal Bhardwaj updated content/posts/the-type-safe-guide-to-trpc.md
1 parent 24d2a0b commit 92698cd

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

content/posts/the-type-safe-guide-to-trpc.md

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,40 @@ alt = "trpcBlogCover"
99
image = "/blog/uploads/trpcblog.png"
1010

1111
+++
12-
1312
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.
1413

1514
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.
1615

1716
**What is tRPC?**
17+
18+
1819
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.
1920

2021
**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.
2225

2326
**How does it implement type-safety?**
27+
28+
2429
tRPC encourages using the [zod](https://www.npmjs.com/package/zod), a library for type validation of input and output arguments.
2530

2631
**Is tRPC only limited to React?**
32+
33+
2734
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.
2835

2936
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)
3037

3138
**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
3846

3947
## tRPC x NextJS
4048

@@ -71,6 +79,7 @@ This is the router where the actual business logic will reside, create a `backen
7179

7280
If using prisma otherwise this is optional,
7381
`src/server/utils/prisma.ts`
82+
7483
```ts
7584
import { PrismaClient } from "@prisma/client";
7685

@@ -86,6 +95,7 @@ if (process.env.NODE_ENV != 'production') global.prisma = prisma;
8695
```
8796

8897
`src/server/router/context.ts`
98+
8999
```ts
90100
import * as trpc from "@trpc/server";
91101
import * as trpcNext from "@trpc/server/adapters/next";
@@ -110,10 +120,11 @@ export const createRouter = () => trpc.router<Context>();
110120
```
111121

112122
> **Using Contexts**
113-
>
114-
> 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_.
115125
116126
`src/server/router/index.ts`
127+
117128
```ts
118129
import {createRouter} from "./contex";
119130
import {exampleRouter} from "./example.router";
@@ -134,6 +145,7 @@ export type AppRouter = typeof appRouter;
134145
> The exact filename is necessary to make this work!
135146
136147
`src/pages/api/trpc/[trpc].ts`
148+
137149
```ts
138150
import * as trpcNext from "@trpc/server/adapters/next";
139151
import { appRouter, AppRouter } from "@/backend/router";
@@ -151,6 +163,7 @@ export default trpcNext.createNextApiHandler({
151163
These are the React hooks necessary to maintain the type-safety, this will give you React Query like hooks to fetch the API.
152164

153165
`src/utils/trpc.ts`
166+
154167
```ts
155168
import { createReactQueryHooks } from "@trpc/react";
156169
import type { AppRouter } from "@/backend/router";
@@ -170,6 +183,7 @@ export type InferQueryOutput<TRouteKey extends TQuery> = inferProcedureOutput<
170183
Now that tRPC is set up, this is how we use it inside react components.
171184

172185
`src/pages/index.tsx`
186+
173187
```tsx
174188
// we use the instance we created that has our router type definitions
175189
import { trpc } from "@/utils/trpc";
@@ -180,9 +194,8 @@ export default SomePage() {
180194
}
181195
```
182196

183-
184-
185197
### SSG Helpers
198+
186199
SSG Helpers are helper functions that can be used to pre-fetch queries on the server upon request to reduce loading time.
187200

188201
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
224237
```
225238
226239
**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

Comments
 (0)