Lucosa
Lucosa

Reputation: 101

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;
}
  

Upvotes: 1

Views: 3328

Answers (2)

Edmund Troche
Edmund Troche

Reputation: 21

I know there is a selected answer, but based on the question, a better option may be something like:

message FindResponse
{
    oneof fresponse
    {
        Response response = 1;
        User user = 2;
    }
}

Upvotes: 2

Sven Bieder
Sven Bieder

Reputation: 86

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;
    }

Upvotes: 2

Related Questions