Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upKotlin coroutine suspend fun resolver hangs when awaiting a data loader #419
Comments
|
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 environment.coroutineScope().future(options.coroutineContextProvider.provide()) {
invokeSuspend(source, resolverMethod, args)?.transformWithGenericWrapper(environment)
}it might be equivalent to the |
|
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 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))
} |
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 kotlinsuspendfunctions, like this:However it seems to be hanging on the
await. I think it's blocking there, before java-graphql called thedispatchon 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)