ts-proto: bug: stream should force observable

Heey there, I’m quite new to GRPC and protobuffers. Great library btw! I’m using the nestjs feature and it’s working great so far!

However I think when defining a stream parameter or return type it should be forced to be of type Observable right? Cause a Promise can’t support a stream of data.

The proto file I have:

syntax = "proto3";

import "google/protobuf/empty.proto";

package statistic;

service StatisticService {
  rpc FindOne (StatisticById) returns (Statistic);
  rpc FindMany (stream StatisticById) returns (stream Statistic);
  rpc CreateEmptyRow (EmptyStatisticRow) returns (google.protobuf.Empty);
}

message EmptyStatisticRow {
  int32 id = 1;
  string field = 2;
}

message StatisticById {
  int32 id = 1;
  string field = 2;
}

message Statistic {
  int32 entityId = 1;
  string sales = 2;
  int32 revenue = 3;
  int32 revenueCash = 4;
  int32 revenueDigital = 5;
}

As you can see the FindMany call takes a stream as input and returns a stream.

Right now ts-proto outputs this function as followed:

  findMany(request: StatisticById, metadata?: Metadata): Promise<Statistic>;

You can force the return type by providing this option --ts_proto_opt=returnObservable=true but this only changed the return type not the parameter type. Also if you set this option to false and the rpc call returns a stream it should be forced to the Observable type right?

I’m currently looking at the nestjs documentation: https://docs.nestjs.com/microservices/grpc#subject-strategy

And as you can see they wrap the input in an Observable and the return type also.

fix

I already implemented a fix and can create a pull request for this. But my main question is if we should do this if the nestJs option is true or not.

https://github.com/debugged-software/ts-proto/commit/dd5a808319cf5239b5ec9d3252e59d6731857ad5

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (16 by maintainers)

Commits related to this issue

Most upvoted comments

nice @ToonvanStrijp , yep i knew I overlooked that. Thanks!

Great work @ToonvanStrijp

Somebody reported an issue to me last night actually, I don’t know if yours fixes this or not.

Here reported this in the nestjs discord channel

i see the --ts_proto_opt=nestJs=true and i used it. beautiful. functions are lowercased. however, empty still appears for responses that should be void. am I missing an option for this?

I still need to confirm it but I must admit I think I overlooked that 😦

If you want I can take a look after you have merged? Or you can - let me know either way.

I agree with pretty much everything in #60 😃 The HeroServiceControllerMethods in particular is a great ergonomics trick. Next time I want to play with grpc streaming, I will be using this nestjs setup. 😃