I was recently discussing grpc-gateway with a colleague and compared it to GraphQL. In the modern front-end world libraries like React and Relay (using GraphQL) provide a compelling user experience that we cannot easily replicate with gRPC.
After researching various selections we determine that there's no equivalent tooling or even patterns that allow us to replicate the bulk or combined nature of GraphQL. Keep in mind that we're aware that GraphQL operates very differently than REST and even RPC. The intention isn't to replicate what it does but determine a pattern to come near to a solution. Also we're trying to solve this mostly for browsers. Mobile gRPC and other environments can solve this in other acceptable ways.
The solutions we devised are:
- Option 1 - Consolidate your endpoints and proxy them with supra endpoints. This however requires more code and development. It also requires redeploying your services.
- Option 2 - Find out how to use WebSockets as a proxy for gRPC and implement bidi to pipe gRPC requests and receive responses on a single TCP connection.
- Option 3 - Find a way to bulk the requests.
- Option 4 - Extend protobuf definitions with a custom generator and options that identify an endpoint that accumulates all the endpoints within the service.
Each option has its pros and cons. Option 4 seems the cleanest but it still ties you to the service. What if you want something beyond a single service?
That led us to the idea of using multipart REST requests containing all RPC methods in one payload then pipelining the results back as each gRPC call is resolved server side. Here's a rough example:
POST /multirpc HTTP/1.1
Content-Type: multipart/mixed; boundary=multirpc_part
--multirpc_part
Content-Type: application/protobuf
Content-ID: id1
POST /routeguide.RouteGuide/GetFeature HTTP/2
[protobuf binary data here]
--multirpc_part
Content-Type: application/protobuf
Content-ID: id2
POST /routeguide.RouteGuide/ListFeature HTTP/2
[protobuf binary data here]
--multirpc_part--
The response structure would have to be laid out in a way that the client would know which payload refers to which call.
The question is: do these 4 options make sense or is there an idiomatic or safer way to accomplish the task without developing a custom pattern like the above approach does?
question from:
https://stackoverflow.com/questions/65872813/bulk-requesting-possibly-chaining-grpc-calls-via-rest 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…