Repository navigation
Description
Description
When a generic wrapper's transformer returns a CompletableFuture, for example a Reactor Mono registered with Mono::toFuture, and the wrapper's type argument is itself a future, graphql-java gets the inner future as the field value instead of what it completes with.
val monoWrapper = GenericWrapper.withTransformer(Mono::class, 0, { mono -> mono.toFuture() })
class Query : GraphQLQueryResolver {
fun item(): Mono<CompletableFuture<Item>> = Mono.just(CompletableFuture.completedFuture(Item("a")))
}The same happens inside lists and other wrappers, e.g. List<Mono<CompletableFuture<Item>>>, and with Future or CompletionStage as the inner type.
Expected behavior
{ item { name } } returns {"item": {"name": "a"}}.
Actual behavior
The schema builds, but the query fails with
Exception while fetching data (/item/name) : Expected source object to be an instance of 'Item' but instead got 'java.util.concurrent.CompletableFuture'
For a scalar field it's worse, the value is serialized as java.util.concurrent.CompletableFuture@26cd19db[Completed normally] with no error.
Steps to reproduce the bug
- Register a generic wrapper whose transformer returns a
CompletableFutureof its content. - Return that wrapper around a
CompletableFuture<T>from a resolver method. - Query the field.
Cause
The schema build unwraps Mono<CompletableFuture<Item>> as two steps, the Mono then the CompletableFuture. At runtime the Mono transformer returns CompletableFuture<CompletableFuture<Item>>, and the CompletableFuture step waits on the transformer's future thinking it's the declared one (the next is Await shortcut in Peel and thenUnlessNextAwaits in ReturnValueUnwrapper). So one future is never waited on.
The tricky part is that the runtime can't tell this apart from a transformer that returns the declared future itself, like a Try<CompletableFuture<Item>> whose transformer returns the CompletableFuture. I prototyped telling them apart by checking whether the transformer's future completes with another future. It fixes most shapes but gets union and interface fields wrong when a member class implements Future, and it adds a thenCompose on every call for any wrapper around a future.
So I think the clean fix is for the wrapper to say it, e.g. a new option on GenericWrapper that marks the transformer as returning a future of the content. Then the unwrapper knows instead of guessing, and other wrappers don't pay for it.
Probably rare in practice since it needs a reactive type wrapping a future, so not urgent.
Activity