I have the following schema in my .proto
file:
service MyService { rpc GetItem (ItemQuery) returns (Item) { } } message ItemQuery { int id = 1; } message Item { int id = 1; string name = 2; }
Now I want to add another rpc method to return multiple Items. Something like this:
rpc GetItems (ItemsQuery) returns (repeated Item) { }
Is there a better way to do it than define an Items message?
A RPC is a form of Client-Server Communication that uses a function call rather than a usual HTTP call. It uses IDL (Interface Definition Language) as a form of contract on functions to be called and on the data type. RPC Architecture. If you all haven't realized it yet, the RPC in gRPC stands for Remote Procedure Call ...
Protocol buffers are a mechanism for sending data through the series of tubes known as the Internet. One common use of them is to define gRPC specifications — essentially a form of remote procedure calls. With gRPC service definitions, you create a “service” that has RPC methods.
Protocol Buffer (Protobuf) provides two simpler options for dealing with values that might be of more than one type. The Any type can represent any known Protobuf message type. And you can use the oneof keyword to specify that only one of a range of fields can be set in any message.
Option 1 - Use stream:
rpc GetItems (ItemsQuery) returns (stream Item) { }
Option 2 - Set a response message which will use a repeated object:
service MyService { rpc GetItem (ItemQuery) returns (ItemResponse) { } } message ItemQuery { int id = 1; } message ItemResponse { repeated Item items = 1; } message Item { int id = 1; string name = 2; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With