Views unable to get a multi row output without returns stream

Hi, I ran into a situation that I can not seem to fix.
I’ve got a view defined as

message GroupName {
  string name = 1;
}

message UserName{
  string username = 1;
}
  rpc GetUsersInGroup(UserName) returns (stream GroupName) {
    option (akkaserverless.method).view.query = {
      query: "SELECT name from groups where username = :username"
    };
  }

Which allows me to query all groups where a username is part of.
But using this, does not allow me to use the GetUsersInGroup through components() (Allow deferred stream-out calls · Issue #674 · lightbend/akkaserverless-java-sdk · GitHub)

I read here I could also define it like this:

message GroupName {
  repeated string name = 1;
}
  rpc GetUsersInGroup(UserName) returns (GroupName) {
    option (akkaserverless.method).view.query = {
      query: "SELECT name from groups where username = :username"
    };
  }

But then I get internal errors when there is a single result:
java.lang.IllegalArgumentException: Wrong object type used with protocol message reflection.\n\tat com.google.protobuf.FieldSet.setField(FieldSet.java:279)
From what I could find this means that it is trying to push something that is not a list into a list.

Removing both stream and repeated results in More than one row found for a single row query

I need the component so I can access the view from an action, but also need the view to be able to return multiple result. Which means I’m a bit stuck and I hope that someone has a solution for me.

Edit: I’m using an integration test, have not deployed it yet

Regards,
Richard

I think you are almost there with the second try, with a single message with a repeated field for the results, however it is not currently possible to both pull all results into a single message field ( SELECT * AS field_name) and transform the view data type into something else, like extracting a single field from it (SELECT name FROM...).

This means your result message type will currently need to have a repeated field of the message type that is stored in the view to be able to return the full result in a unary method that is accessible from other services/components.

1 Like

Thanks for replying! Based on your suggestion I got it to work! :smiley:

My view is build from

message UserGroup {
  string name = 1;
  string username = 2;
}

And based on the suggestion I made

message UserGroups {
  repeated UserGroup usergroup = 1;
}

  rpc GetUsersInGroup(UserName) returns (UserGroups) {
    option (akkaserverless.method).view.query = {
      query: "SELECT * as usergroup from groups where username = :username"
    };
  }

And now I’m able to query the view with multiple results.
Thanks!

1 Like