Type safety when a Query Handler returns a CompletableFuture<SomeObject>

I’ve ran into an bit of an interesting use case with Axon’s query handlers. I want my query handler to return a completable future of SomeObject.

So then the type interface for that query handler becomes:

public CompletableFuture<SomeObject> query(SomeQueryObject someQueryObject)

But that leads to a problem when actually querying it:

queryGateway.send(new SomeQueryObject(someId), SomeObject.class)

Which leads to a

org.axonframework.queryhandling.NoHandlerForQueryException: No handler found for SomeQueryObject with response type class SomeObject


That makes sense, but from a convenience standpoint it’s not really that nice.

Sure I could write it like:

queryGateway.send(new SomeQueryObject(someId), CompletableFuture.class)

And then manually get to the value inside of it but that’s not really that nice of a solution, as it would require bit of casting and making assumptions about what return type we’ve got exactly.
Whilst losing the ability to have multiple handlers for the same query with different return type.

I’d love for the queryGateway to ‘strip’ the one of the completable futures so that the party issuing the query is completely unaware of if the query handler is returning sync or async, that it just knows what type it will get and that’s it.

Did like to hear what you guys think, am I just missing something? Or is this a weird edge case which might be nice to support in the framework?

Kind regards,


Hi Thomas,

at the moment, we don’t support CompletableFuture as declared return type of Query handlers. For now, you would have to use SomeObject as return value.

It does seem like a sensible return type to support, and we’ll definitely consider adding it.