Protobuf service types #796
-
Hi there, I'm intrigued by Let's say I have a React application with Express (tRPC + zod) acting as a middleware server (proxy) to the external API (source of truth/data). Flow look like this: Having zod in the mix is very helpful and useful because I have an access not only to the TypeScript types of the I am very new to Protocol Buffers and from what I read in the
that when Registering a Service I would need to define Response/Request types of the message and that business logic is only a fetch to the API and passing down the response leads me to think that I would have duplicate
So question is:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hey! To use connect-es you have to define the types in protobuf. You will get similar type inference, if not better, on the client side when using connect-es. You can still use zod for validations like email etc... and protobuf will ensure the basic types (string/number) are as expected. We are working on https://github.com/bufbuild/protovalidate, which aims to bring validation to protobuf messages. It already has support for several languages with TS being actively worked on. |
Beta Was this translation helpful? Give feedback.
-
So lets say I have Prisma in my application stack . // Prisma model
model Post {
id String @id @default(uuid())
title String
text String
} // prisma call implementation
async ({ id }) => {
const post = await prisma.post.findUnique({
where: { id },
});
return post;
}) This implementation call is automatically type assisted thanks to the prisma model so return of such call is known as {
id: 'someid',
title: 'sometitle',
text: 'sometext',
} But If I would like to use prisma with message Request {
string id = 1;
}
message Response {
string id = 1;
string title = 2;
string text = 3;
}
service PostService {
rpc byId(Request) returns (Response) {}
} ...
export default (router: ConnectRouter) =>
router.service(PostService, {
async byId(req: Request, context: HandlerContext) {
const post = await prisma.post.findUnique({
where: { id },
});
return new Response(post);
}
}); Or can I reuse the Types provied by the prisma model and omit definition of the Request/Response in the proto Service file ? |
Beta Was this translation helpful? Give feedback.
Hey! To use connect-es you have to define the types in protobuf. You will get similar type inference, if not better, on the client side when using connect-es. You can still use zod for validations like email etc... and protobuf will ensure the basic types (string/number) are as expected.
We are working on https://github.com/bufbuild/protovalidate, which aims to bring validation to protobuf messages. It already has support for several languages with TS being actively worked on.