Closed jsbrain closed 3 years ago
Hey @jsbrain really late follow up, but I'm pretty sure RPC methods must return a value, but then within that message, you can have optional fields. So typically you see a pattern:
service HeroService {
rpc FindHero(request FindHeroRequest) returns FindHeroResponse {}
}
message FindHeroResponse
Hero hero = 1;
So that then the FindHeroResponse.hero
can be empty.
Granted, all of the FooRequest
+ FooResponse
messages get kind of tedious, but that's what allow the "a return value may be null" behavior you're looking for.
Thanks!
Right, that's what I came up with in the meantime as well. But thanks for the heads up! 😎
As seen in the NestJS simple example here the return type is sometimes enforced via
!
, e.g.but for me this is an anti pattern and I try to never use ! in my codebase. As this is just example, chances are, this isn't the proper way to do it anyway, which leads to my question:
What is the proper way to handle this?
Does gRPC not allow null types to be returned? If it does allow it, couldn't the generated code simply state
Hero | undefined
as return type?Or would it be correct to throw an gRPC error if no result if found?
I'm pretty new to gRPC so any advice how to handle this properly would be much appreciated!