Reviving the BatchedExecutionStrategy through DataLoaders?
See original GitHub issueI quite heavily relied on graphql-java-tools
and the BatchedExecutionStrategy
for a project I’m working on, so I got a bit worried when I saw it deprecated (although I fully get the point and I would have done the same, #noblame).
Firstly: why got graphql-java-tools
moved out of https://github.com/graphql-java to https://github.com/graphql-java-kickstart/graphql-java-tools? I couldn’t find any explanation on both repos. What consequence does this have?
Secondly, I’ve been thinking of a way to allow BatchLoader
to actually provide more than “simple keys”, but instead the sources and the arguments to mimic the way the BatchedExecutionStrategy
used to work, and I think I’ve found a possible solution.
I’ve written a fairly small tool to test it and I would really appreciate getting feedbacks frown this community: https://github.com/sp00m/graphql-gom. Although it’s still work in progress, I’ve tried to document it well so that you fully get the idea.
Put simply, these three issues made me start implementing it:
- https://github.com/graphql-java/graphql-java/issues/963
- https://github.com/graphql-java-kickstart/graphql-java-tools/issues/58
- https://github.com/graphql-java/java-dataloader/issues/26
Excerpt of the README:
You know how
BatchLoader
s are supposed to take only keys to allow fetching the corresponding values from your data source? For example:BatchLoader<Integer, Article> articleBatchLoader = new BatchLoader<Integer, Article>() { @Override public CompletionStage<List<Article>> load(List<Integer> keys) { return articleService.getArticles(keys); } };
Well, what GOM does, is basically “enhancing” those keys by passing instances of
DataLoaderKey
instead (internal class):class DataLoaderKey { // the source, got via DataFetchingEnvironment#getSource private Object source; // the arguments, got via DataFetchingEnvironment#getArguments private Object arguments; // the context, got via DataFetchingEnvironment#getContext private Object context; }
This trick then allows a
BatchLoader
to “group the keys by arguments”, and thus call your resolvers as many times as there are distinct arguments, but each time will all the sources.
What do you think of such a solution? Is it viable? Do you see any issues of using DataLoader
with “enhanced keys”?
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (12 by maintainers)
@sp00m
I had a go at trying to implement the example above using DataLoader
The code is in Groovy in Spock unit testing format
https://gist.github.com/bbakerman/2304079e0c87fb198fc348cdb26edf2b
The “keys” here when we call dataLoader.load(key) are in fact the blogId + textToSearchFor. The reason for this is that is the unique dimension being searched for.
So given data
and a query of
it returns a result of
This only calls the BatchLoader function once for all blog instances (2) and for each sub field (2 x 2)
About the first question: Please have a look here: https://www.graphql-java.com/blog/moving-projects/ for details about moving projects.
Thanks