How to create CRUD with gRPC service without much repetition?

I'm trying to use gRPC to create a simple CRUD service, but I keep looking for messages with large overlaps.

This best describes an example:

message Todo {
  // id is only available for a persisted entity in database.
  string id = 1;
  string content = 2;
  // this is only available for users with admin role.
  string secret_content = 3;
}

service Todos {
  rpc CreateTodo(CreateRequest) returns (CreateResponse) {}
  rpc ReadTodo(ReadRequest) returns (ReadResponse) {}
}

message CreateRequest {
  // this todo is not supposed to have id,
  // should I create another version of Todo without an id field?
  Todo todo
}

message CreateResponse {
  // this todo will always have an id.
  Todo todo = 1;
}

message ReadRequest {
  string id = 1;
}

message ReadResponse {
  // this todo should only have the secret_content field if the
  // user is authenticated as an admin, if not, the field should not
  // fallback to the zero value, the whole field must be missing. 
  Todo todo = 1;
}

Is this a good approach to create a CRUD-like resource using gRPC? That is, having a single message ( Todo) representing the resource and wrapping this message in the types of responses / requests for each action.

If a Todo message has all the fields covered by all the requests / responses and does not set those that are not used by everyone?

+4
source share
1 answer

If a Todo message has all the fields covered by all the requests / responses and does not set those that are not used by everyone?

, . protobuf v2 optional, . v3 .

+1

Source: https://habr.com/ru/post/1654484/


All Articles