Repository navigation
going full async - #545
going full async#545andimarek wants to merge 3 commits into
Conversation
| throw e; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
This is painfully missing in Java :(
The ability to wait for a Collection support should be present
There was a problem hiding this comment.
What is painfully missing? Waiting for an async collection to finish?
| } | ||
| } | ||
| return new ExecutionResultImpl(results, executionContext.getErrors()); | ||
| return CompletableFuture.completedFuture(new ExecutionResultImpl(results, executionContext.getErrors())); |
There was a problem hiding this comment.
Notice here how we have a materialised ExecutionResult and then we wrap into a completed Future
so that to the outside it looks like asynch promise?
I am not sure of the value here. It allows thenApply style composition but it nots async all the way down.
That said I guess it allows other strategies to be more or less asynch and hence the signature is in the right shape.
Actually as I type this, it makes me see how this is useful
| return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).thenApply((ignored) -> { | ||
| Map<String, ExecutionResult> realResults = new LinkedHashMap<>(); | ||
| for (String field : results.keySet()) { | ||
| realResults.put(field, results.get(field).join()); |
There was a problem hiding this comment.
The join will throw the NonNull exceptions and so here.
There was a problem hiding this comment.
We will want a specific NonNullNull field test in the equivalence tests say
| } else { | ||
| result = queryStrategy.execute(executionContext, parameters); | ||
| result = queryStrategy.execute(executionContext, parameters).toCompletableFuture().join(); | ||
| } |
There was a problem hiding this comment.
One decision we need to make is whether to use CompletionStage or CompletableFuture (or a mix of both)
Standard Java best practice says use the interface CompletionStage. Except you can never get the value of a CompletionStage and so you have all these .toCompleteableFuture() calls
Since this code base is all about materialising values (eventually) perhaps we should use CompletableFuture?
There was a problem hiding this comment.
Perhaps they will evolve the CompletionStage interface to make it useful. toCompletableFuture().get() is not horrible imo.
|
FWIW, I few months I played with porting graphql-java over to kotlin (https://github.com/LarsKrogJensen/graphql-kotlin) just for the fun of it and learning. Kotlin is such a beautiful language. One of the features that I really wanted to implement was async data fetchers all the way without any usage a threads or thread pools. I went with CompletionStage, which I regret - it didn't provide any real value as one of the comments above also concluded.
What I really would like is the datafetcher to return a union type (not available i java or kotlin yet) of |
|
@LarsKrogJensen Our plan is indeed to handle the returned value from a |
dminkovsky
left a comment
There was a problem hiding this comment.
Thanks for getting this rolling!
With these interface changed, the async strategy I wrote should be easy enough to integrate: https://github.com/dminkovsky/graphql-java/blob/async/src/main/java/graphql/execution/async/AsyncExecutionStrategy.java
You'll see it supports returning leaves and non-leaves from the data fetchers, synchronously and asynchronously: https://github.com/dminkovsky/graphql-java/blob/async/src/test/groovy/graphql/execution/async/AsyncExecutionStrategyTest.groovy#L36-L47
| } else { | ||
| result = queryStrategy.execute(executionContext, parameters); | ||
| result = queryStrategy.execute(executionContext, parameters).toCompletableFuture().join(); | ||
| } |
There was a problem hiding this comment.
Perhaps they will evolve the CompletionStage interface to make it useful. toCompletableFuture().get() is not horrible imo.
| throw e; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
What is painfully missing? Waiting for an async collection to finish?
|
By the way, in my playing with this, I experimented with fetchers that return:
...and various other permutations. I think there should be limited support for which of these we support, as it gets weird and complicated pretty fast. Most importantly, given the recursive bottom-up resolution of GraphQL, I think only supporting async collections that you expect will be fully resolved made sense: https://github.com/dminkovsky/graphql-java/blob/async/src/test/groovy/graphql/execution/async/AsyncExecutionStrategyTest.groovy#L45. Also, while we're talking about resolving future collections, this library is very useful until Java 9 comes out: https://github.com/spotify/completable-futures |
|
I have implemented a reactive version of ExecutionStrategy that works with RxJava 2, and on the happy path it works beautifully. If your DataFetcher is returning any of the Rx reactive streams, a Publisher, Futures/CompletableFutures or even a CompletionStage, it works as expected. Parallelism is up to the DataFetcher and the reactive stream object it returns - if the reactive object is on different scheduler then it's going to be fetched async. This hasn't needed any changes to the ExecutionStrategy so far, and the only problem left at the moment is merging errors from async and non-async sources. |
|
closing this issue: it was a spike and never intended to be merged. the real implementation is PR #590 |
just a spike, not intended to be merged, doesn't even compile.