How to forward /convert responses?

Hi,
I have the following situation:

  • An action which implements a http-endpoint
  • An (eventsourced) entity which implements the domain-logic.

I want to separate the (public http) API from the internal entity API.
I use the forwarding of a command as described here: here, that works fine.
This results in the entity sending the reply directly to the caller (=http client). That is fine is it’s a empty reply.
But what if I want to convert the response from the entity (domain-state) into another type (a http-response)?

I’ve added a handler which does the convertion, but how do i add it to the ‘forward chain’? The documentation states that ‘Forwards can be chained arbitrarily long’. How do I do that (with the java-API).
This is basically what I want to achieve:

receive request → convert request to command → forward command to entity → entity replies with domain-object → convert domain-object into response.

Thank you

I can work around it like this:
In the API-description:

rpc GetDomainState(GetStateRequest) returns (StateResponse) {
option (google.api.http) = {
get: “/state/{id}”
};
}

rpc ConvertDomainStateToResponse(StationState) returns (StateResponse);

The API-implementation converts the Request to a command and forwards it to the entity.
The entity produces the state and forwards it (back) to the ConvertDomainStateToResponse, which convert it to a response.

Eventhough this works, it feels dirty.
For example: the entity-API now looks like this:

rpc GetState (GetStationStateCommand) returns (google.protobuf.Empty);

The implementation looks like this:

var state = //…
var converter = ctx.serviceCallFactory().lookup(API_SERVICE,“ConvertDomainStateToResponse”,StationState.class);
return Reply.forward(converter.createCall(state));

I would ratherhave the entity-API look like this:

rpc GetState (GetStationStateCommand) returns (StationState);

This work-around also makes that the now entity is dependant on the API-service, which is a bit backwards (wrt. clean-architecturally).
I could create a separate action so that the convertion is outside of the API-service and use that to forward the domain-objects to. But that that’s not really solving the problem. The API-service should be the one reponsible to convert data from-and-to the external/internal datamodel.

Is there any other way to convert the ‘domain-response’ into a API-response without a workaround like this?
Thanks!

ok, nevermind. I suppose I have to use views, I guess that is what they are for :slight_smile:

We will probably add possibility of response transformation where you do the first forward. Then the entity can reply with its own domain protocol and the action takes care of transforing that to the external api response.

Thanks for the reply.
Yes, I think there is value in that. Although, a lot of usecases for this can probably also be solved by means of a view.