★ wanayoo — archive 1999 https://github.com/graphql-java-kickstart/graphql-java-tools/issues/419Nouvelle recherche | Portail wanayoo
Skip to content
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

Kotlin coroutine suspend fun resolver hangs when awaiting a data loader #419

Open
arian opened this issue Jul 16, 2020 · 2 comments
Open

Kotlin coroutine suspend fun resolver hangs when awaiting a data loader #419

arian opened this issue Jul 16, 2020 · 2 comments

Comments

@arian
Copy link
Contributor

@arian arian commented Jul 16, 2020

We're using java-graphql batch loaders (https://www.graphql-java.com/documentation/v15/batching/)

Instead of returning CompletableFutures in our resolver methods, I tried using kotlin suspend functions, like this:

    suspend fun loadUsers(env: DataFetchingEnvironment): List<UserGQL> {
        val users = Context.getDataLoaders(env)
            .usersLoader
            .loadMany(listOf(1, 2, 3))
            .await()
        return users.map { UserGQL(it) } 
    }

However it seems to be hanging on the await. I think it's blocking there, before java-graphql called the dispatch on the registered data loaders (in DataLoaderRegistry).

What would be the best way to solve this? I couldn't really find examples in the documentation or the original pull request (#201)

@arian
Copy link
Contributor Author

@arian arian commented Aug 4, 2020

Might it be because this example in the graphql-java docs: https://www.graphql-java.com/documentation/v15/batching/

        BatchLoader<String, Object> batchLoader = new BatchLoader<String, Object>() {
            @Override
            public CompletionStage<List<Object>> load(List<String> keys) {
                return CompletableFuture.completedFuture(getTheseCharacters(keys));
            }
        };

        DataLoader<String, Object> characterDataLoader = DataLoader.newDataLoader(batchLoader);

        // .... later in your data fetcher

        DataFetcher dataFetcherThatCallsTheDataLoader = new DataFetcher() {
            @Override
            public Object get(DataFetchingEnvironment environment) {
                //
                // Don't DO THIS!
                //
                return CompletableFuture.supplyAsync(() -> {
                    String argId = environment.getArgument("id");
                    DataLoader<String, Object> characterLoader = environment.getDataLoader("characterLoader");
                    return characterLoader.load(argId);
                });
            }
        };

and with MethodFieldResolver.kt:190

            environment.coroutineScope().future(options.coroutineContextProvider.provide()) {
                invokeSuspend(source, resolverMethod, args)?.transformWithGenericWrapper(environment)
            }

it might be equivalent to the supplyAsync example the docs tells us not to do?

@williamboman-pp
Copy link

@williamboman-pp williamboman-pp commented Sep 26, 2020 •

I think the problem is that you're wrapping the CompletionStage provided by the DataLoader API with the coroutine's CompletableFuture representation created by graphql-java-tools. As you've pointed out already, this disconnects graphql-java from your dataloader, and so it is unable to dispatch it.

My understanding is that .loadMany will not actually call your data loader method directly - it'll only create a "request to fetch these users at some point", which you need to return in your data fetcher so that graphql-java can orchestrate when to actually call the data loader (maybe there'll be more data fetchers making use of the same data loader - then all those keys will be aggregated by graphql-java and the data loader will only be called once, when graphql-java sees fit).

I haven't worked with dataloader + graphql-java-tools yet, but I think you need something like this

fun loadUsers(env: DataFetchingEnvironment): Any {
  return Context.getDataLoaders(env)
    .usersLoader
    .loadMany(listOf(1, 2, 3))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.