protocol-buffersproto

grpc multiple returns types


What is the best approach to get one more guy in return on a service? I mean, i want a single method to be able to return more than one type, in which case the find method would return a User or a Response

syntax = "proto3";

service UserService {
    rpc Add(User) returns (Response);
    rpc Find(Id) returns (User);
}

message Response {
    string message = 1;
}

message Id {
    string id = 1;
}

message User {
    string id = 1;
    int32 money = 2;
}
  

Solution

  • You could return an object that includes the response and the user.

        syntax = "proto3";
    
        service UserService {
            rpc Add(User) returns (Response);
            rpc Find(Id) returns (FindResponse);
        }
        
        message Response {
            string message = 1;
        }
        
        message Id {
            string id = 1;
        }
        
        message User {
            string id = 1;
            int32 money = 2;
        }
    
        message FindResponse{
            Response response = 1;
            User user = 2;
        }