You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please consider adding support of Future return type for DataFetcher API.
Actually scratched that. We just noticed it is possible to provide a custom executor service per fetcher. But it will still block for each fetcher.
Is it actually possible to go async all the way?
Across all fetchers
Make top level graphql execute() async as well.
Reacted by Arnold Schrijver, Dorian Martin, Roland Tepp, Paulo Lopes and Jonathan D'Orleans
I would also like to second @ybayk's request to go async all the way (via fetchers and execute()). @tberman's work-around still leaves something to be desired.
I'm new to GraphQL and have only looked at the Java implementation thus far, but it seems to me that this would more closely align with the Javascript specification, which states that ExecutionResults should be returned via a Promise, which are async.
If others agree with this sentiment, I'd be happy to start working or collaborate on a pull request to make this happen.
Reacted by Arnold Schrijver, Roland Tepp, Sebastien Dubois and aosorio1
@dminkovsky I'd integrate some sort of callback interface as the primary method of returning data from execute() and fetchers.
Query executions would probably end up looking something like:
new GraphQL(schema).execute("{hello}", new GraphQLCallback() {
@Override
public void onComplete(ExecutionResult result) {
// Handle result
}
});
and the DataFetcher interface would need to change to something like:
public interface DataFetcher {
void get(DataFetchingEnvironment var1, DataFetchCallback callback);
}
// Sample async DataFetcher
new DataFetcher() {
@Override
public void get(DataFetchingEnvironment environment, DataFetchCallback callback) {
// Start async operation with callback, such as web or DB lookup
async(new AsyncCallback() {
@Override
public void onComplete(Object result) {
// Return value to ExecutionStrategy via callback to be collated
callback.onComplete(value); // Can also be called directly for sync operations
}
});
}
};
This implementation seems simple enough, although it does have the slight problem of requiring the DataFetchCallback to be manually called when implementing DataFetchers, and I'm not sure how to enforce that at the moment.
As for the callback interfaces themselves, I imagine a few simple classes would suffice, although I'm sure we can also use a more established callback library such as JDeferred (actual Promise functionality).
That would mean the original get() method would have to be deprecated. Why not add the callback reference to the environment? That way one can add a separate handling mechanism for the when the callback has been set in the environment.
You have a good point: we shouldn't be deprecating methods lightly.
It seems to me that in order to avoid deprecating the get() method, the ExecutionStrategy must decide to either wait for the DataFetchCallback or process the serial return value of get(). One workaround for this ambiguous situation would be to create a separate AsyncExecutionStrategy and perhaps an executeAsync() function to handle the callback methods. The drawbacks to this would be some likely code duplication, at the very least.
My point also remains that per GraphQL-JS specification, the library should be async capable by default, and I believe callbacks of some sort are the way to go about this. Perhaps the best solution for something this drastic would be to break backwards compatibility and roll it out as v3.0.0?
@hepin1989 I was looking at CompletableFutures as well, although I haven't personally used it before. It does seem to have the potential of being more elegant than simple callbacks. Does it address @IamCornholio's concern about backwards compatibility though?
@akhahaha I mean that this code seems to be using futures already and I wondered if that essentially represents the AyncExecutionStrategy that you were talking about:
Map<String, Future<ExecutionResult>> futures = new LinkedHashMap<String, Future<ExecutionResult>>();
for (String fieldName : fields.keySet()) {
final List<Field> fieldList = fields.get(fieldName);
Callable<ExecutionResult> resolveField = new Callable<ExecutionResult>() {
@Override
public ExecutionResult call() throws Exception {
return resolveField(executionContext, parentType, source, fieldList);
}
};
futures.put(fieldName, executorService.submit(resolveField));
}
try {
Map<String, Object> results = new LinkedHashMap<String, Object>();
for (String fieldName : futures.keySet()) {
ExecutionResult executionResult = futures.get(fieldName).get();
results.put(fieldName, executionResult != null ? executionResult.getData() : null);
}
This code is in that file ExecutorServiceExecutionStrategy.
Your point about it not being the default async promise still remains.
@IamCornholio Ah gotcha. From what I can tell, that particular ExecutionStrategy is used to parallelize field resolution to speed up execution and is not related to the asynchronous functionality I'm looking to enable here.
@akhahaha I am sorry about my delayed responses. I am glad to hear it and am glad that my suggestion makes sense. I wasn't sure whether I was addressing your question, but I think we are on the same page. Yes—we definitely want to improve the docs wherever possible. Where in the README do you think adding this would have greatest impact?
@hepin1989 @oexza I am not sure I understand why the executor service strategy doesn't provided the flexibility you are looking for. Can't you still use your own thread pool / executor service in the data fetchers as they currently are?
I should also emphasize that Java 6 compatibility is an important goal for this project that we are seeking to maintain for as long as possible. We have Java 6 users running this in production, and that is pretty cool in my opinion!
@dminkovsky Yes,thanks,but I implement it my self:)you know ,close source project,have much adoption.GraphQL is used at taobao.com for all the content service.
Maybe this project should get an official branch with Java 8 and async-all-the-way targeted for a future 3.0.0 release. And an additional roadmap document.
So I've spent the past few weeks attempting to enlighten myself on this issue. Here's what I've found:
At this time, there are too many disparate async libraries/mechanisms in the Java world for this library to adopt one particular approach. The JS implementation is simple: there are no types, and @leeb just tests results for .then() to determine if the result is a Promise. Very simple!
But no such thing in Java. Before Java 8 you had RxJava Observable and Netty's ChannelPromise. Now Java 8 adds another mechanism: CompletionStage/CompletableFuture. There are all different, and to me it doesn't make sense to bake any of them into this library as the canonical way to be async.
Can we just have execution strategies for each of these? @pcarrier has one for RxJava; @akhahaha showed how one could be written for CompletionStage/CompletableFuture.
Yes, that's an issue. I am working with Vert.x, yet another way to go asynchronously. In Vert.x the fetchers should be async as well. I am looking into using the above async branch, but with a Vert.x implementation of CompletableFuture.
Maybe the project could be improved to facilitate various extensions more easily (have a more defined SPI). Maybe support the most basic callback handler overloads (similar to described above).
Then extension projects can build on top of it.
(Talking about extension: IMO the Relay code should be pushed out to an extension as well. It's just one client library. I intend to switch to Apollo Client, and no special node/edge server schema needed)
Maybe the project could be improved to facilitate various extensions more easily
So my conclusion has been that implementing an ExecutionStrategy is simple enough. You have to implement execute() and you'll probably have to override resolveField, but between those two methods I think you can make anything happen. That's my opinion anyway.
I'm in the process of writing an strategy for CompletableFuture and will share when it's finished.
Perhaps someone should add "Coming soon" to the "Asynchronous resolvers" section of this page on the official docs? It seems to be saying that this capability is baked into GraphQL. Especially that last line...
During execution, GraphQL will wait for Promises, Futures, and Tasks to complete before continuing and will do so with optimal concurrency.
@benhead thanks for your post. That page is about the reference, JS implementation. The asynchronous execution strategy I am working on uses Java 8's CompletableFuture interface...
...which I just PR-ed. If anyone is interested in this, please comment there.
I found https://github.com/karthicks/graphql-java-async and it offers an non blocking (for vertx) possibility to fetch data from remote/slow systems in a async way. Maybe you can take a look and it fullfill all your needs. It wraps graphql-java and based on it.
Activity
tberman commentedon Oct 6, 2015
I have an implementation of an ExecutionStrategy that uses rxjava that I will be publishing as open source soon.
ybayk commentedon Oct 6, 2015
That would be exactly what we are looking for. Any hint on how soon? :)
tberman commentedon Nov 8, 2015
https://github.com/nfl/graphql-rxjava is available now, readme coming soon, and trying to get it put into central ASAP
akhahaha commentedon Sep 6, 2016
I would also like to second @ybayk's request to go async all the way (via fetchers and execute()). @tberman's work-around still leaves something to be desired.
I'm new to GraphQL and have only looked at the Java implementation thus far, but it seems to me that this would more closely align with the Javascript specification, which states that ExecutionResults should be returned via a Promise, which are async.
If others agree with this sentiment, I'd be happy to start working or collaborate on a pull request to make this happen.
dminkovsky commentedon Sep 6, 2016
@akhahaha Yeah I am interested in that too. How do you envision it would work, a bit more specifically? Thank you.
akhahaha commentedon Sep 6, 2016
@dminkovsky I'd integrate some sort of callback interface as the primary method of returning data from execute() and fetchers.
Query executions would probably end up looking something like:
and the DataFetcher interface would need to change to something like:
This implementation seems simple enough, although it does have the slight problem of requiring the DataFetchCallback to be manually called when implementing DataFetchers, and I'm not sure how to enforce that at the moment.
As for the callback interfaces themselves, I imagine a few simple classes would suffice, although I'm sure we can also use a more established callback library such as JDeferred (actual Promise functionality).
Thoughts?
JeSuisNikhil commentedon Sep 6, 2016
That would mean the original get() method would have to be deprecated. Why not add the callback reference to the environment? That way one can add a separate handling mechanism for the when the callback has been set in the environment.
akhahaha commentedon Sep 6, 2016
You have a good point: we shouldn't be deprecating methods lightly.
It seems to me that in order to avoid deprecating the get() method, the ExecutionStrategy must decide to either wait for the DataFetchCallback or process the serial return value of get(). One workaround for this ambiguous situation would be to create a separate AsyncExecutionStrategy and perhaps an executeAsync() function to handle the callback methods. The drawbacks to this would be some likely code duplication, at the very least.
My point also remains that per GraphQL-JS specification, the library should be async capable by default, and I believe callbacks of some sort are the way to go about this. Perhaps the best solution for something this drastic would be to break backwards compatibility and roll it out as v3.0.0?
He-Pin commentedon Sep 6, 2016
Go with CompletableFuture ,Task,Source or something else that compose,no callback.
eg:
akhahaha commentedon Sep 6, 2016
@hepin1989 I was looking at CompletableFutures as well, although I haven't personally used it before. It does seem to have the potential of being more elegant than simple callbacks. Does it address @IamCornholio's concern about backwards compatibility though?
JeSuisNikhil commentedon Sep 6, 2016
The file ExecutorServiceExecutionStrategy seems to be doing a bit already..
akhahaha commentedon Sep 6, 2016
@IamCornholio what do you mean?
JeSuisNikhil commentedon Sep 6, 2016
@akhahaha I mean that this code seems to be using futures already and I wondered if that essentially represents the AyncExecutionStrategy that you were talking about:
This code is in that file ExecutorServiceExecutionStrategy.
Your point about it not being the default async promise still remains.
akhahaha commentedon Sep 6, 2016
@IamCornholio Ah gotcha. From what I can tell, that particular ExecutionStrategy is used to parallelize field resolution to speed up execution and is not related to the asynchronous functionality I'm looking to enable here.
He-Pin commentedon Sep 7, 2016
In fact I am doing the parallelism thing currently ,I am going to be with https://github.com/monixio/monix and https://github.com/akka/akka/.
We should use something that monadic as the return type.just as what sangria choose scala's Future.
17 remaining items
dminkovsky commentedon Sep 11, 2016
@akhahaha I am sorry about my delayed responses. I am glad to hear it and am glad that my suggestion makes sense. I wasn't sure whether I was addressing your question, but I think we are on the same page. Yes—we definitely want to improve the docs wherever possible. Where in the README do you think adding this would have greatest impact?
@hepin1989 @oexza I am not sure I understand why the executor service strategy doesn't provided the flexibility you are looking for. Can't you still use your own thread pool / executor service in the data fetchers as they currently are?
I should also emphasize that Java 6 compatibility is an important goal for this project that we are seeking to maintain for as long as possible. We have Java 6 users running this in production, and that is pretty cool in my opinion!
dminkovsky commentedon Sep 11, 2016
@hepin1989 by the way, if you are using Akka/Scala, have you checked out Sangria?
He-Pin commentedon Sep 11, 2016
@dminkovsky Yes,thanks,but I implement it my self:)you know ,close source project,have much adoption.GraphQL is used at
taobao.comfor all the content service.aschrijver commentedon Oct 22, 2016
Maybe this project should get an official branch with Java 8 and async-all-the-way targeted for a future 3.0.0 release. And an additional roadmap document.
dminkovsky commentedon Oct 24, 2016
So I've spent the past few weeks attempting to enlighten myself on this issue. Here's what I've found:
At this time, there are too many disparate async libraries/mechanisms in the Java world for this library to adopt one particular approach. The JS implementation is simple: there are no types, and @leeb just tests results for
.then()to determine if the result is a Promise. Very simple!But no such thing in Java. Before Java 8 you had RxJava
Observableand Netty'sChannelPromise. Now Java 8 adds another mechanism:CompletionStage/CompletableFuture. There are all different, and to me it doesn't make sense to bake any of them into this library as the canonical way to be async.Can we just have execution strategies for each of these? @pcarrier has one for RxJava; @akhahaha showed how one could be written for
CompletionStage/CompletableFuture.aschrijver commentedon Oct 24, 2016
Yes, that's an issue. I am working with Vert.x, yet another way to go asynchronously. In Vert.x the fetchers should be async as well. I am looking into using the above async branch, but with a Vert.x implementation of CompletableFuture.
Maybe the project could be improved to facilitate various extensions more easily (have a more defined SPI). Maybe support the most basic callback handler overloads (similar to described above).
Then extension projects can build on top of it.
(Talking about extension: IMO the Relay code should be pushed out to an extension as well. It's just one client library. I intend to switch to Apollo Client, and no special node/edge server schema needed)
dminkovsky commentedon Oct 24, 2016
So my conclusion has been that implementing an
ExecutionStrategyis simple enough. You have to implementexecute()and you'll probably have to overrideresolveField, but between those two methods I think you can make anything happen. That's my opinion anyway.I'm in the process of writing an strategy for
CompletableFutureand will share when it's finished.benhead commentedon Nov 16, 2016
Perhaps someone should add "Coming soon" to the "Asynchronous resolvers" section of this page on the official docs? It seems to be saying that this capability is baked into GraphQL. Especially that last line...
dminkovsky commentedon Nov 16, 2016
@benhead thanks for your post. That page is about the reference, JS implementation. The asynchronous execution strategy I am working on uses Java 8's
CompletableFutureinterface......which I just PR-ed. If anyone is interested in this, please comment there.
a-marcel commentedon May 19, 2017
I found https://github.com/karthicks/graphql-java-async and it offers an non blocking (for vertx) possibility to fetch data from remote/slow systems in a async way. Maybe you can take a look and it fullfill all your needs. It wraps graphql-java and based on it.
andimarek commentedon May 22, 2017
We will finally try to tackle this issue in the next release. See #283
He-Pin commentedon May 22, 2017
Yes, Go For Async, and we could rebuild the sync from the Async again.
andimarek commentedon Jun 19, 2017
I am closing this issue: the issue will be handled in #324