tRPC allows to build easily & consume fully type-safe APIs without schemas or code generation. It is a framework for building end-to-end type-safe APIs with TypeScript.
With the demand of type-safe APIs, tRPC is a great choice to build and consume APIs. It's make writing endpoints type-safe which use in both the front and backend of your app. Type errors with your API contracts will be caught at build time, reducing the surface for bugs in your application at runtime.
we will be using the latest version of next.js 14 to create a new app. We can use the following command to create a new next.js app with app router.
We will be using MongoDB as our database and tRPC to create our API endpoints. We will be using Mongoose as our ODM for MongoDB.
Server Action in Client and Server Component in Next.Js Explained
Server Actions are asynchronous functions that are executed on the server. They can be used in Server and Client Components to handle form submissions and data mutations in Next.js applications.
We will create a new folder db in the root of our project. This folder will contain the setup for our MongoDB connection.
This is a simple setup for our MongoDB connection. We are using mongoose.connect to connect to our MongoDB database. NextJs advised to use global to maintain a global connection to the database. since we don't want to create a new connection for every re-render of our app.
We can now create a new folder models in the root of our project. This folder will contain the models for our MongoDB database.
Create a new folder trpc-client in the root of our project. This folder will contain the setup for our tRPC client.
We have setup our tRPC client using @trpc/react-query. since we will be using react-query for our data fetching and mutations for our client components.
To fetch data or do mutations in our server we have to create new file called server-client.ts in the same folder.
With the above setup, we can now use serverClient to fetch data or do mutations in our server.
We have to expose our tRPC endpoints to the client. We will create a new folder inside of app directory called api. This folder will contain the setup for our tRPC endpoints.
We have setup our tRPC endpoints using @trpc/server/adapters/fetch. This will allow us to use fetch to make requests to our tRPC server.
Wrapping our app with tRPC provider using React Query#
We will create a folder lib in the root of our project. This folder will contain the setup for our tRPC provider.
we can now wrap the root of your app with the Provider component to use tRPC with React Query.
we can use the getUser endpoint in our components to fetch data from our server.
to create new user we can use the createUser endpoint in our client component.
With the above setup, we have successfully setup tRPC with Next.js 14 and MongoDB. We can now use tRPC to create and consume type-safe APIs in our app.
tRPC is a great choice to build and consume APIs. It's make writing endpoints type-safe which use in both the front and backend of your app. Type errors with your API contracts will be caught at build time, reducing the surface for bugs in your application at runtime.
To get the full code of this setup, you can check out the GitHub. I hope you find this article helpful. If you have any questions or feedback, feel free to comment below.
Comments (3)