Skip to content

Asynchronous DataFetcher? #37

Closed
Closed

Description

@ybayk

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?

  1. Across all fetchers
  2. Make top level graphql execute() async as well.

Activity

  1. tberman commented on Oct 6, 2015

    @tberman
    Contributor

    I have an implementation of an ExecutionStrategy that uses rxjava that I will be publishing as open source soon.

  2. ybayk commented on Oct 6, 2015

    @ybayk
    Author

    That would be exactly what we are looking for. Any hint on how soon? :)

  3. tberman commented on Nov 8, 2015

    @tberman
    Contributor

    https://github.com/nfl/graphql-rxjava is available now, readme coming soon, and trying to get it put into central ASAP

  4. akhahaha commented on Sep 6, 2016

    @akhahaha

    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.

  5. dminkovsky commented on Sep 6, 2016

    @dminkovsky
    Contributor

    @akhahaha Yeah I am interested in that too. How do you envision it would work, a bit more specifically? Thank you.

  6. akhahaha commented on Sep 6, 2016

    @akhahaha

    @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).

    Thoughts?

  7. JeSuisNikhil commented on Sep 6, 2016

    @JeSuisNikhil
    Contributor

    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.

  8. akhahaha commented on Sep 6, 2016

    @akhahaha

    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?

  9. He-Pin commented on Sep 6, 2016

    @He-Pin

    Go with CompletableFuture ,Task,Source or something else that compose,no callback.
    eg:

    AsyncDataFetcher{
      Source<T> get(..);
    }
    
  10. akhahaha commented on Sep 6, 2016

    @akhahaha

    @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?

  11. JeSuisNikhil commented on Sep 6, 2016

    @JeSuisNikhil
    Contributor

    The file ExecutorServiceExecutionStrategy seems to be doing a bit already..

  12. akhahaha commented on Sep 6, 2016

    @akhahaha

    @IamCornholio what do you mean?

  13. JeSuisNikhil commented on Sep 6, 2016

    @JeSuisNikhil
    Contributor

    @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.

  14. akhahaha commented on Sep 6, 2016

    @akhahaha

    @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.

  15. He-Pin commented on Sep 7, 2016

    @He-Pin

    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.

  16. 17 remaining items

  17. dminkovsky commented on Sep 11, 2016

    @dminkovsky
    Contributor

    @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!

  18. dminkovsky commented on Sep 11, 2016

    @dminkovsky
    Contributor

    @hepin1989 by the way, if you are using Akka/Scala, have you checked out Sangria?

  19. He-Pin commented on Sep 11, 2016

    @He-Pin

    @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.

  20. aschrijver commented on Oct 22, 2016

    @aschrijver
    Contributor

    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.

  21. dminkovsky commented on Oct 24, 2016

    @dminkovsky
    Contributor

    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.

  22. aschrijver commented on Oct 24, 2016

    @aschrijver
    Contributor

    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)

  23. dminkovsky commented on Oct 24, 2016

    @dminkovsky
    Contributor

    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.

  24. benhead commented on Nov 16, 2016

    @benhead

    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.

  25. dminkovsky commented on Nov 16, 2016

    @dminkovsky
    Contributor

    @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.

  26. a-marcel commented on May 19, 2017

    @a-marcel

    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.

  27. added this to the milestone on May 22, 2017
  28. andimarek commented on May 22, 2017

    @andimarek
    Member

    We will finally try to tackle this issue in the next release. See #283

  29. He-Pin commented on May 22, 2017

    @He-Pin

    Yes, Go For Async, and we could rebuild the sync from the Async again.

  30. andimarek commented on Jun 19, 2017

    @andimarek
    Member

    I am closing this issue: the issue will be handled in #324

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions