Skip to content

A generic wrapper whose transformer returns a future loses a nested future #938

Closed

Description

@oryan-block

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

  1. Register a generic wrapper whose transformer returns a CompletableFuture of its content.
  2. Return that wrapper around a CompletableFuture<T> from a resolver method.
  3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions