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
{{ message }}
Repository navigation
Allow Asynchronous Execution And Fetching In GraphQL Java #324
Currently, the GraphQL.execute(request) method blocks until the result is ready, unlike graphql-js, which lets you handle the result in a callback.
Here, we propose an asynchronous rendition of graphql-java, which will allow you to not only execute a GraphQL request in a non-blocking manner, but also permit the data fetcher to wrap it's results in a CompletableFuture.
Since our implementation relies on a Java 8 feature, it is being published as a separate project called graphql-java-async, so as to not impinge on graphql-java's Java 6 compatibility requirements.
A non-blocking version of the famous "hello world" can be found in the README file. A more involved scenario involving publishing and subscribing to news articles can be seen in this groovy test case.
Is this something that could go as a "related project" under graphql-java?
Just got a chance to digest the change and comments in #283, and noticed a few differences, some of which may be minor:
Your change, as it stands, requires a force upgrade to Java 8. However, one of your follow-up comments notes that it'd be a separate project, so that distinction is probably moot.
There's a dependency on spotify's completable futures library, which my change doesn't require.
The async version of GraphQL described here only works with the AsyncExecutionStrategy, whereas the version in your change accepts any kind of ExecutionStrategy, which to me looks more flexible. Does your GraphQLAsync class work with a non-AsyncExecutionStrategy?
I made a concerted effort to handle the exceptional cases in every CompletableFuture, and surface the error in the ExecutionContext. Your change may have neglected to handle a few error cases, if I'm not mistaken.
With your change, the AsyncExecutionStrategy can be run in either serial or parallel mode. In my case, it's always parallel, which probably means that it can't be used as a mutation strategy. On a related note, I was able to get the parallel mode working without having to define a new (user-provided?) interface such as CompletableFutureFactory.
The version of AsyncExecutionStrategy here tries to reuse as much of the code in it's super class, viz., ExecutionStrategy, as humanly possible. In your change, I noticed that you're not really using the super class' execute method at all.
I'll keep looking at your change, but I think I nailed down the key differences. All in all, this change is a tad simpler (smaller?). And, it is something that we plan to deploy to production.
Thank you for this analysis and your implementation. I am sorry for my slow reply and my short initial reply to you. I am just very busy at this time. I've looked at your implementation and have some thoughts too, but I cannot lay them out at this time. I will update when I can get back to this.
Thanks for the update. Please take your time - there's no rush. By the way, I just added a way to get a parallel and serial flavor of the asynchronous strategy, for the query and mutation paths, respectively.
Neat, non-intrusive solution. There is an issue with exception propagation to the ExecutionFuture resulting in a loss of ExceptionWhileDataFetching exceptions though
I have been thinking on how we can introduce this without creating a completely separate graphql.GraphQL structure. if we can leave the current graphql.GraphQL in place and yet offer asynch behaviour then we can preserve most of the code out there already.
We will have to break the ExecutionStrategy SPI however because the serial ExecutionResult is used as a way to resolve fields. But that wont work in an async and serial manner.
I was thinking we leave graphql.GraphQL as it but add a new executeAsync() method that returns a CompletableFuture in addition to the current ones
We would then require ExecutionStrategy to return CompletableFuture<ExecutionResult> rather than the current plan ExecutionResult. Since all the current serial implementations can be made to return CompletableFuture<ExecutionResult> and still be serial it works for low effort.
By re-using the current API we can introduce async execution while preserving the current code investment. Only ExecutionStrategys would be required to be changed and most people do NOT write them but rather use them
I will try to put together a PR to demonstrate the API shape (but not the implementation) since these exist already.
Note: GraphQL-JS waits for all promises in a list to be resolved/rejected before it calls the next level of resolve functions.
I have been looking into the reference -js code and it seems that they only descend when all the fields at one level are resolved. I need to investigate this further since my ability to deeply read complicated node.js is...limited
So I have been looking further into this and to me it seems that the current call pattern in ExecutionStrategy is incompatible with the reference implementation BUT does conform to spec
A valid GraphQL executor can resolve the four fields in whatever order it chose (however of course birthday must be resolved before month, and address before street).
So in this sense the current code allows for spec.
Today the execution in SimpleExecutionStrategy / ExecutionStrategy is depth first.
It calls
execute() - with a list of fields
resolveField() - for 1st field
completeValue() - for that object
execute() - if its an object type
resolveField() - for 1st sub field
completeValue() - stops at scalar
resolveField() - for 2nd field
completeValue() - for that object
execute() - if its an object type
resolveField() - for 2nd sub field
completeValue() - stops at scalar
And so on in a depth first manner.
So given:
{
gods {
name
invitedBy {
name
}
}
wizards {
name
invitedBy {
name
}
}
}
It will descend all of "gods" first right down to the sub objects before moving onto "wizards".
But when writing an async implementation the current resolveField calling completeValue inside itself means you can do parallel operations easily.
The "fetch" inside resolveField is the part that is likely to take the most time and hence firing off multiple fetches at once is where the win is.
The reference implementation in JS does this.
However it waits for all asynch fetchs to complete together before moving onto the complete phase.
But with the depth first approach we have today it means that we don get a "batch" of possible fields in order to 1) fire off at once and 2) use delayed "batching" to combine the requests.
I propose we change the structure of ExecutionStrategy so that resolveField does not call completeValue.
This will allow another async implementation to make breadth first data fetch requests (in parallel) and then complete them afterwards.
The alternative for another implementation is to "copy" all the code in resolveField and take out the complete call.
Another way would be to refactor it into
resolveField
fetchField
completeField
and then a new implementation can re-use the fetchField and completeField methods and not the resolveField (but rather write its own)
The reason I am really interested in this is to make async work better AND to make it possible for https://github.com/bbakerman/java-dataloader to work efficiently like in graphql-js
Activity
dminkovsky commentedon Feb 9, 2017
Hi @karthicks. Thank you for posting! Have you seen #283?
karthicks commentedon Feb 9, 2017
Hi, @dminkovsky ,
Just got a chance to digest the change and comments in #283, and noticed a few differences, some of which may be minor:
I'll keep looking at your change, but I think I nailed down the key differences. All in all, this change is a tad simpler (smaller?). And, it is something that we plan to deploy to production.
-Karthick
dminkovsky commentedon Feb 11, 2017
Hi Karthick,
Thank you for this analysis and your implementation. I am sorry for my slow reply and my short initial reply to you. I am just very busy at this time. I've looked at your implementation and have some thoughts too, but I cannot lay them out at this time. I will update when I can get back to this.
Dmitry
karthicks commentedon Feb 15, 2017
@dminkovsky ,
Thanks for the update. Please take your time - there's no rush. By the way, I just added a way to get a parallel and serial flavor of the asynchronous strategy, for the query and mutation paths, respectively.
-Karthick
stikkos commentedon Mar 30, 2017
Neat, non-intrusive solution. There is an issue with exception propagation to the ExecutionFuture resulting in a loss of ExceptionWhileDataFetching exceptions though
bbakerman commentedon Apr 16, 2017
I have been thinking on how we can introduce this without creating a completely separate graphql.GraphQL structure. if we can leave the current graphql.GraphQL in place and yet offer asynch behaviour then we can preserve most of the code out there already.
We will have to break the ExecutionStrategy SPI however because the serial ExecutionResult is used as a way to resolve fields. But that wont work in an async and serial manner.
I was thinking we leave
graphql.GraphQLas it but add a newexecuteAsync()method that returns a CompletableFuture in addition to the current onesWe would then require
ExecutionStrategyto returnCompletableFuture<ExecutionResult>rather than the current planExecutionResult. Since all the current serial implementations can be made to returnCompletableFuture<ExecutionResult>and still be serial it works for low effort.By re-using the current API we can introduce async execution while preserving the current code investment. Only
ExecutionStrategys would be required to be changed and most people do NOT write them but rather use themI will try to put together a PR to demonstrate the API shape (but not the implementation) since these exist already.
bbakerman commentedon Apr 17, 2017
Also one thing that I think needs some thought is how far deep for we make the CompletableFutures inside ExecutionResults?
We could end up with a CompletableFuture which contains CompletableFuture which is in fact a List of CompletableFuture and so on
At some point we need to decide how deep the promises should go?
The outer most one is the easiest. That is a we let the consumer decide WHEN to start consuming the result.
However the inner ones are more problematic as they wont inherently be resolved promises unless they are individually unpacked.
So should the outer promise be a simple delaying async mechanism or should it complete the inner objects when it itself is completed?
bbakerman commentedon Apr 18, 2017
I put up a PR to add the API interfaces that will allow a proper async implementation to be put in place.
The PR does NOT add an async implementation, but rather the API signature to allow it.
andimarek commentedon Jun 19, 2017
Please have a look at #380: This is the first PR we will merge.
andimarek commentedon Jun 19, 2017
We will use this issue to track the overall topic: PR #380 is just first step
bbakerman commentedon Jun 29, 2017
One important design aspect to capture is outlined here
https://dev-blog.apollodata.com/graphql-explained-5844742f195e
And this important point
I have been looking into the reference -js code and it seems that they only descend when all the fields at one level are resolved. I need to investigate this further since my ability to deeply read complicated node.js is...limited
bbakerman commentedon Jun 30, 2017
So I have been looking further into this and to me it seems that the current call pattern in ExecutionStrategy is incompatible with the reference implementation BUT does conform to spec
The spec says
http://facebook.github.io/graphql/#sec-Normal-and-Serial-Execution
So in this sense the current code allows for spec.
Today the execution in SimpleExecutionStrategy / ExecutionStrategy is depth first.
It calls
execute() - with a list of fields
resolveField() - for 1st field
completeValue() - for that object
execute() - if its an object type
resolveField() - for 1st sub field
completeValue() - stops at scalar
And so on in a depth first manner.
So given:
It will descend all of "gods" first right down to the sub objects before moving onto "wizards".
But when writing an async implementation the current resolveField calling completeValue inside itself means you can do parallel operations easily.
The "fetch" inside resolveField is the part that is likely to take the most time and hence firing off multiple fetches at once is where the win is.
The reference implementation in JS does this.
However it waits for all asynch fetchs to complete together before moving onto the complete phase.
This then allows for libraries such as https://github.com/facebook/dataloader to "batch" together requests for similar objects.
But with the depth first approach we have today it means that we don get a "batch" of possible fields in order to 1) fire off at once and 2) use delayed "batching" to combine the requests.
I propose we change the structure of ExecutionStrategy so that
resolveFielddoes not callcompleteValue.This will allow another async implementation to make breadth first data fetch requests (in parallel) and then complete them afterwards.
The alternative for another implementation is to "copy" all the code in resolveField and take out the complete call.
Another way would be to refactor it into
and then a new implementation can re-use the fetchField and completeField methods and not the resolveField (but rather write its own)
The reason I am really interested in this is to make async work better AND to make it possible for https://github.com/bbakerman/java-dataloader to work efficiently like in graphql-js
A start to graphql-java#324 that provides a promise interface to exec…
A start to #324 that provides a promise interface to executing querie
andimarek commentedon Aug 28, 2017
this is done with #590: it will be included in the next release