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
I like to work with GraphQL. GraphQL helps me to create a typed-safe API from the server to the client. However, it's not easy to implement GraphQL. There are a lot of steps to deal with GraphQL when you work alone on a full-stack project. So I created soki to solve this problem. Let's dive in to see how it works.
Install Soki
To install soki, I will run this command.
yarn install soki
In the compilerOptions section of the tsconfig.json file, I will change the strict value to true.
To work with soki, first, I will create schemas for the API. And I will use these schemas both on the server and the client.
Message Schema
In this section, I will create a function type called hello. And I will put this function type into a child schema called MessageSchema. The hello function will have the input and the output like the code below.
Next, I will combine all child schemas into only one schema called RootSchema. I will use this schema to implement resolver functions on the server. And to create a typed-safe API client to call these functions on the client.
This step, I will implement MessageSchema with the Resolvers['message'] type.
// src/backend/resolvers/message.resolver.tsimport{Resolvers,createResolver}from'@backend/core';exportconstMessageResolver=createResolver<Resolvers['message']>({hello: async({ name },context)=>{return`Hello ${name}!`;},});
In the code above, you can see that the hello function has two parameters. The first one is the input parameter. And the second one is the context parameter. We can get the context result from the context function in the RootResolver below.
Root Resolver
Next, I will implement the RootSchema. I will combine all child resolvers into only one resolver called RootResolver as well.
I like to work with GraphQL. GraphQL helps me to create a typed-safe API from the server to the client. However, it's not easy to implement GraphQL. There are a lot of steps to deal with GraphQL when you work alone on a full-stack project. So I created soki to solve this problem. Let's dive in to see how it works.
Install Soki
To install soki, I will run this command.
In the compilerOptions section of the tsconfig.json file, I will change the strict value to true.
Soki Schemas
To work with soki, first, I will create schemas for the API. And I will use these schemas both on the server and the client.
Message Schema
In this section, I will create a function type called hello. And I will put this function type into a child schema called MessageSchema. The hello function will have the input and the output like the code below.
Soki is using zod to define schemas. You can find zod's documentation here: https://www.npmjs.com/package/zod
Root Schema
Next, I will combine all child schemas into only one schema called RootSchema. I will use this schema to implement resolver functions on the server. And to create a typed-safe API client to call these functions on the client.
Soki on Server
Core
Before I implement the RootSchema on the server. I want to create a file named core.ts. This file will contain:
Resolvers
Message Resolver
This step, I will implement MessageSchema with the Resolvers['message'] type.
In the code above, you can see that the hello function has two parameters. The first one is the input parameter. And the second one is the context parameter. We can get the context result from the context function in the RootResolver below.
Root Resolver
Next, I will implement the RootSchema. I will combine all child resolvers into only one resolver called RootResolver as well.
Don't forget the context function here. It's useful to work on authentication, cookies, database connection, ... for each request.
API Handler
This is the final step to implement the schemas on the server. I will create a handler for the RootResolver. The endpoint of this handler is /api.
I will also disable the bodyParser on this endpoint. So we can upload files via soki.
Soki on Client
API Service
To implement the schemas on the client, I'll create the ApiService like this.
You can also handle fetch's RequestInit options with the onRequest function.
Using the Hello Function
And, to use the hello function from MessageResolver, I will edit the src/pages/index.tsx file with the content below.
Finally, to see the result of the hello function, please run
yarn dev
to see how it works.Demo: https://next-full-stack-git-issue-4.maxvien.vercel.app/
Source Code
You can find the source code of this tutorial in this branch: https://github.com/Maxvien/next-full-stack/tree/issue-4
The text was updated successfully, but these errors were encountered: