Honour null generic wrapper results and await plain futures - #836
Open
oryan-block wants to merge 1 commit into
Open
oryan-block wants to merge 1 commit into
oryan-block wants to merge 1 commit into
Conversation
A generic wrapper transformer that returned null was ignored, because transformWithGenericWrapper fell back to the original object with an elvis operator. Option-like wrappers could therefore never map their empty case to null: a scalar field serialized the wrapper's toString and an object or list field failed at runtime. The transformer result is now returned as is, including null, and the original object is only kept when no wrapper matches. The default Future wrapper only unwraps the type at scan time. At runtime graphql-java only awaits a CompletionStage, so any other Future (a FutureTask, or the observer returned by RxJava's toFuture()) reached graphql-java as the field value and child fields failed with a source type mismatch. When a resolver method is declared to return Future, a returned future that is not a CompletionStage is now waited on with a blocking get() on the fetching thread, as if the resolver had called get() itself. The cause of an ExecutionException is rethrown so the resolver's own error is reported, and the interrupt flag is restored before an InterruptedException is rethrown. The check uses the method's declared raw return type, so a returned object that merely implements Future is left alone. Because the wait blocks, a plain future that only completes after graphql-java moves on, such as one backed by a DataLoader load, now hangs instead of failing with a type mismatch. Such resolvers should return a CompletionStage. A plain future returned by a custom generic wrapper transformer is still not waited on unless the method itself is declared to return Future. Fixes #371 Fixes #203 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #371
Fixes #203
Checklist
Description
A generic wrapper transformer can't map a value to null. #371 registers wrappers for Arrow's
Option,NoneandSome, withNonetransformed to null, and a nullableerrors: [Error!]field returningNonefails withCan't resolve value (/user/register/errors) : type mismatch error, expected type LIST got class arrow.core.None. Every other case works, only the empty one doesn't.transformWithGenericWrapperended with?.transformer?.invoke(this, env) ?: this, so when the transformer returned null the elvis fell back to the original wrapper object. A scalar field then serialized the wrapper'stoString, and an object or list field failed with a type or source mismatch. The transformer result is now returned as is, null included, and the original object is only kept when no wrapper matches.MethodFieldResolverDataFetcher(suspend path included) andLightMethodFieldResolverDataFetcherboth go through it.In #203 a resolver declared as
Future<Organization>returns RxJava'sSingle#toFuture(), and its child fields fail withExpected source object to be an instance of '...Organization' but instead got 'io.reactivex.internal.observers.FutureSingleObserver'. The test in the thread only passed because it returned null or aCompletableFuture. The defaultGenericWrapper(Future::class, 0)is an identity transformer, so it only unwraps the type at scan time. At runtime graphql-java only awaits aCompletionStage, so any otherFuturebecame the field value. Now when a method's declared raw return type is exactlyFuture(the Kotlin return type for suspend functions, same lookup the scan uses, moved intogetReturnType()), a returned future that isn't aCompletionStageis waited on with a blockingget()on the fetching thread after the generic wrapper transformer runs. That's the same as the resolver callingget()itself. The cause of anExecutionExceptionis rethrown so the error shows the resolver's own exception, same asinvokedoes withInvocationTargetException, and the interrupt flag is restored before anInterruptedExceptionis rethrown.CompletionStages are left alone. A subscription declaredFuture<Publisher<Event>>that returns a plain future works now too (it threw on master). The new tests are inMethodFieldResolverTestandReactiveTest, and all four fail on master.The check is on the declared type rather than
isInstanceon purpose. I first put the wait into the defaultFuturewrapper's transformer, but wrappers match withisInstanceat runtime, so a domain object that happens to implementFuture(fun job(): Jobwithclass Job : Future<String>) gotget()called on it. Its result replaced the source, or the request hung if it wasn't done yet. So the default wrapper is untouched. I went with blocking over something likesupplyAsyncbecause a plainFuturehas no completion callback, so some thread has to block either way. BlockingcommonPoolthreads could starve everything else on that pool (batch loaders included), and a dedicated executor would need new API. If we want that it's a separate change.tfadsilva's comment on #203 still fails: a custom transformer that itself returns a plain future (
Flowable.toList().toFuture()) getstype mismatch error, expected type LIST got class ...FutureSingleObserver. Transformer output isn't passed through the wrappers again and its declared type isn't known, so waiting on it would bring back the problem above. Returning aCompletionStagefrom the transformer, or blocking inside it, works. There's no timeout onget(). A return type that's a type variable bound toFuture(fun item(): RinBase<R>withQ : Base<Future<Item>>), or a subscription declared as aFuturesubtype likeFutureTask<Publisher<Event>>, still passes the scan without being waited on, same as master.I also looked at #254 (
List<Try<Payload>>with a VavrTrywrapper) alongside these, and it's not fixed here. It still fails withExpected source object to be an instance of ...Payload but instead got ...Try$Success. The scanner accepts any registered wrapper under a list, defaults included, soList<CompletableFuture<T>>passes the scan and fails the same way at runtime. Fixing it needs either a runtime transform driven by the declared type that walks lists, arrays and nested lists and goes throughCompletionStageandDataFetcherResult, or rejecting nested wrappers at scan time like nestedOptionalalready is, which would break schemas that build today. That's a call for a maintainer, so I left it open.Behaviour change: a generic wrapper transformer that returns null now makes the field null. Before, the original wrapper was passed through, so anyone relying on null to mean "leave it alone" now gets null. Nothing changes when no wrapper matches. A resolver declared to return
Futurethat returns a plain future now blocks the fetching thread until it's done (a coroutine dispatcher thread for suspend functions). Before, the field always failed. So a plain future that only completes after graphql-java moves on, like one backed by a DataLoader load, now hangs instead of failing right away with a type mismatch. Those should return aCompletionStage.CompletableFutureand otherCompletionStages behave the same as before.🤖 Generated with Claude Code