Search code examples
graphqlgraphql-java

Does the GraphQL#execute run all DataFetchers on the same thread? Is the asynchronous behaviour delegated to the DataFetcher implementation?


If all my Data Fetchers returned POJOs directly instead of CompletableFuture, will the entire GraphQL execution happen on the same thread?


Solution

  • Yes. Based on what I see from the source codes , if all data fetchers only return POJO, the whole execution will execute on the same thread.

    To configure data fetchers to run in parallel , you have to use the ExecutionStrategy that supports asynchronous processing. The default one used by the query (AsyncExecutionStrategy) supports while the mutation (AsyncSerialExecutionStrategy) does not.

    The data fetcher also has to return a CompletableFuture using CompletableFuture.supplyAsync(). Please note that graphql-java provides a handy data fetcher called AsyncDataFetcher which can wrap a normal synchronous data fetcher to the asynchronous one:

    DataFetcher asyncDataFetcher = AsyncDataFetcher.async(fooDataFetcher);