Transform Uni of one class of object to an Uni of a different one #860
-
Background:I'm trying to build a simple Quarkus prototype app, that provides gRPC endpoints that returns various data pulled from database, all using Mutiny for reactive style code. Guides and tutorials I've found so far, are great, but none goes beyond the very basics, and I'm struck on extremely trivial things. (It doesn't help that I'm completely new to Reactive Java in general, and Munity in particular.) Any help would be greatly appreciated! Details:For query the database, with reactive hibernate, I have the following simple REST endpoint, to return details on customer, base on the id provided. This works fine.
For the gRPC endpoints, as most the underlying code are auto-generated, the code required from me looks just like an REST endpoint. I can populate a dummy reply object, return it as an Uni, as a proof of concept. This also works.
Question:So, how should I transform an Uni<CustomerEntity>, which is returned from database lookup, into an Uni<CustomerReply> object, so it can be used to return result back from the gRPC endpoint? There is a 1:1 map, on the data fields, between CustomerEntity and CustomerReply. Conceptually, I understand I need to convert the result (in an Uni) from data lookup to a gRPC reply (also an Uni), and then passed down stream to gRPC transport. However, I'm having trouble figuring out how to correctly code it. Should I extract the actual CustomerEntity from Uni first, covert it CustomerReply, then wrap it in an Uni with Uni.createFrom()? Or is there some way to chain the 2 streams together? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You probably want to use |
Beta Was this translation helpful? Give feedback.
-
So, here's the (still very simple) gRPC route I came up with, using transform():
I want to mention though, what kinda tripped me earlier on, was that the Uni for data entity has to be declared first. When I initially tried to chain everything together like this:
the compiler could not figure out the actual data entity class type (i.e CustomerEntity) for item in the lambda expression, but resolve it to the super class (in this case, PanacheEntityBase), which leads to errors, because getters like getClientId(), getEmail() etc, can't be found. Maybe there's a better way to code this, without having to create the variable entity, which is only used once? |
Beta Was this translation helpful? Give feedback.
You probably want to use
.onItem().transform(item -> ...)
, see https://smallrye.io/smallrye-mutiny/getting-started/transforming-items so you can react to an item, and map it to something else.