-
Notifications
You must be signed in to change notification settings - Fork 320
Support Callable as a return value from controller methods for asynchronous execution #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm not sure I understand what you're missing. You can return Flux, Mono from controller methods. |
@rstoyanchev Thanks for your response. Will spring-graphql support 'concurrent way' for the convenience of developers who prefer concurrent api. reactor way in spring-graphqlI rewrite GreetingController#greeting to support fetching field asynchronously. If this is what your mean, (and this seems to be the only way for supporting execution asynchronously). Developers must be familiar with reactor api. public class GreetingController {
@QueryMapping
public Mono<String> greeting() {
return Mono.fromSupplier(() -> {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return "Hello " + attributes.getAttribute(RequestAttributeFilter.NAME_ATTRIBUTE, SCOPE_REQUEST);
}).subscribeOn(Schedulers.parallel());
}
} concurrent way in graphql-javaBinding field with the async dataFetcher and assigning the public class AsyncDataFetcher<T> implements DataFetcher<CompletableFuture<T>> {
/**
* @param wrappedDataFetcher the data fetcher to run asynchronously
* @param executor the executor to run the asynchronous data fetcher in
*/
public static <T> AsyncDataFetcher<T> async(DataFetcher<T> wrappedDataFetcher, Executor executor) {
return new AsyncDataFetcher<>(wrappedDataFetcher, executor);
}
@Override
public CompletableFuture<T> get(DataFetchingEnvironment environment) {
return CompletableFuture.supplyAsync(() -> {
//
})
}
} concurrent way in spring-graphql I expectedBe similar to mvc-ann-async:
|
Thanks for elaborating. I see now the perspective you're coming from. A In short, we've no plans to rebuild such purpose-built asynchronous facilities as we have in Spring MVC. These days both Spring MVC and Spring WebFlux support Reactor return values and that is the approach in Spring GraphQL from the start to make Reactor a core dependency, so you'll encounter it in all areas including client API, server side (interception, exception resolvers, etc), and testing (subscription streams).. That said I see no reason why you couldn't reach a similar level of convenience with Reactor while also retaining the ability to compose multiple async operations together, deal with timeouts, and so on: public class GreetingController {
@QueryMapping
public Mono<String> greeting() {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
String name = attributes.getAttribute(RequestAttributeFilter.NAME_ATTRIBUTE, SCOPE_REQUEST);
return executeAsync(() -> "Hello " + name);
}
private <T> Mono<T> executeAsync(Callable<T> callable) {
return Mono.fromCallable(callable).subscribeOn(Schedulers.parallel());
}
} |
Can we use |
@rstoyanchev Yes, I realize Reactor is a powerful and convenient tool. It is the first time that I spend lots of time studying Reactor, because I do encounter it in all areas of spring-graphql , as well as other spring document. In fact, I am also familiar with graphql-java,(I used graphql-java for two years and fed back lots of code to graphql-java repository). This proposal is based on the reality of the use of graphql(java) and Reactor in China:
Thanks for your elaborating. Maybe this issue should be discussed after spring-graphql has been more widely used and more feedback. |
I think the currently implemetation is pretty well, as our internal GraphQL implementation which named TQL is the same which only need the deverper retun an So the question is how you manage blocking? |
@dugenkui03 think we could consider some simple steps to assist a data fetching controller method to become asynchronous. For example considering that GraphQL Java expects However, I would draw the line there. For anything more, e.g. want a timeout, or a different executor, just use What do you think? |
@hepin1989 if we support @pinguinjkeke you can use suspend but Reactor is still a required dependency and used in our APIs where contracts need to be asynchronous or where we need to compose asynchronous logic. This is comparable to WebFlux. |
@rstoyanchev @hepin1989 Thanks for your replay.
Simple but useful method, you may think that it is not elegant, such as timeout problem and thread management. But in brief, I think support |
@rstoyanchev thanks for the answer. It will be a good push for |
Use GraphQLContext as input instead of ExecutionInput, DataFetchingEnvironment and BatchLoaderEnvironment. See gh-316
Asynchronous execution is key point to ensure high performance of query, details in asynchronous-execution.
It seems that all the invoketion of @controller handler method is serial by default. Will spring graphql support asynchronous execution of fetching field (probably similar to mvc-ann-async)?
The text was updated successfully, but these errors were encountered: