question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reviving the BatchedExecutionStrategy through DataLoaders?

See original GitHub issue

I 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:

Excerpt of the README:

You know how BatchLoaders 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:closed
  • Created 5 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
bbakermancommented, Oct 15, 2018

@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

    def blogData = [
            new Blog(id: 1, name: "Java", purpose: "Java stuff"),
            new Blog(id: 2, name: "Reactive", purpose: "Rx stuff")
    ]

    def articleData = [
            new Article(id: 101, blogId: 1, title: "Changes in the LTR policy of the JDK"),
            new Article(id: 102, blogId: 1, title: "CPU impact of the JVM Memory Model"),

            new Article(id: 200, blogId: 2, title: "So you want to use less CPU"),
            new Article(id: 201, blogId: 2, title: "Why reactive systems use less CPU")
    ]

and a query of

        query {
          blogs {
            id
            name
            articlesWithCPUInTitle: articles(whereTitleContains: "CPU") {
                blogId
                title
            }
            articlesWithReactiveInTitle: articles(whereTitleContains: "reactive") {
                blogId
                title
            }
          }
        }

it returns a result of

[blogs:
   [[id:1, name:Java, 
              articlesWithCPUInTitle:[[blogId:1, title:CPU impact of the JVM Memory Model]], 
              articlesWithReactiveInTitle:[]], 
   [id:2, name:Reactive, 
               articlesWithCPUInTitle:[[blogId:2, title:So you want to use less CPU], [blogId:2, title:Why reactive systems use less CPU]], 
                articlesWithReactiveInTitle:[[blogId:2, title:Why reactive systems use less CPU]]]]]

This only calls the BatchLoader function once for all blog instances (2) and for each sub field (2 x 2)

2reactions
andimarekcommented, Oct 3, 2018

About the first question: Please have a look here: https://www.graphql-java.com/blog/moving-projects/ for details about moving projects.

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Batching - GraphQL Java
Using java-dataloader will help you to make this a more efficient process by both caching and batching requests for that graph of data...
Read more >
GraphQL Java: Using @Batched DataFetcher - Stack Overflow
WARNING: The original BatchedExecutionStrategy has been deprecated and will get removed. The current preferred solution is the Data Loader ...
Read more >
2021年08月_weixin_39614657的博客_CSDN博客
Crypto library Not available in google-app-engine environment. 2021-01-12 ... Reviving the BatchedExecutionStrategy through DataLoaders? 2021-01-11 ...
Read more >
Batching and Caching With Dataloader - Telerik
In this article, we're going to cover what Dataloader is and how it can help us with database requests and reduce our database...
Read more >
Data Loaders | GraphQL Kotlin
Data Loaders are a popular caching pattern from the JavaScript GraphQL implementation. graphql-java provides support for this pattern using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found