Search code examples
grpc

gRPC is it possible to use simple type as input when declaring service method?


I am new to grpc and was wondering if there is a way to use simple types such as string or int32 inside method declaration in grpc proto file.

I tried so far using syntax as below but with no success:

    syntax = "proto3";
    
    package MyPackage.SimpleServices;

    option csharp_namespace = "MyGrpcServices";

    service MySimpleService {
       rpc GetData(string) returns (ServiceData);
    }

    message ServiceData {
       string fieldOne = 1;
       int32 fieldTwo = 2;
       string fieldThree = 3;
    }

When i try to generate code from this i get error: Expected message type.

So here is my question: Is there any way to use simple type as input/output without wrapping it in message?


Solution

  • It is not possible.

    But Google's Well-Known Types (WKT) includes e.g. StringValue which is a message that includes a singular field of type string and can be used as a solution to this problem.

    The WKTs not only provide a solution (wrapping scalar types in messages) but provide a mechanism whereby using these types provides "type compatibility"; your use of e.g. StringValue messages can be used in e.g. Google's RPCs and everywhere that this de facto standard is used.