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.

How to use batch loaders in SPQR

See original GitHub issue

Hi! My question is connected with solving N+1 problem. I have a query: @GraphQLQuery public List<Contact> contactList(@GraphQLContext User user, @GraphQLArgument(name = "offset", defaultValue = "0") int offset, @GraphQLArgument(name = "limit", defaultValue = "50") int limit) { return ....<some database logic> } and it’s ok if i just want to retrieve one user, cause it costs me only 2 queries. But when I want to retrieve lets say 100 Users it becomes harder to compute. I found GraphQL DataLoader that potentially is able to solve the issue, but can’t make it work. I was tring to base my implementation on this but it didn’t work.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
slavapcommented, Nov 27, 2019

Just for reference if someone finds this issue by googling 😃 This solution works: https://github.com/graphql-java-kickstart/graphql-spring-boot/tree/master/example-request-scoped-dataloader

no need for CustomGraphQLContextBuilder, just enough the following (with graphql-spqr-spring-boot-starter):

    @Bean
    public DataLoaderRegistryFactory dataLoaderRegistryFactory() {
        return new DataLoaderRegistryFactory() {
            @Override
            public DataLoaderRegistry createDataLoaderRegistry() {
                return buildDataLoaderRegistry();
            }
        };
    }

In case no starter used, see https://github.com/graphql-java/graphql-java/issues/1254

The only thing you have to use:

@GraphQLEnvironment ResolutionEnvironment env
env.dataFetchingEnvironment

for getting fetching environment.

1reaction
kaqqaocommented, Dec 11, 2018

The latest graphql-java has a better organization. DataLoaderRegistry now goes into ExecutionInput and DataLoaderDispatcherInstrumentation is always registered. Also, DataFetchingEnvironment gives access to ``DataLoaderRegistry` directly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

leangen/graphql-spqr - Gitter
That is to say, you should only re-use BatchLoader instances across requests, nothing else. And create a new DataLoader and DataLoaderRegistry instance each ......
Read more >
Batch loader file syntax and sample - IBM
A batch loader list file is typically a text (.txt) file that contains a list of the XML loader files for batch processing...
Read more >
How to resolve graphql n+1 issue with graphql-jpa java?
This option is simple to understand and use, but ties you to ... the data loaders and the registry on each request (batch...
Read more >
Assessing Jena and Sesame - SPQR
We can now populate the triple store. TDB provides a tdbloader loader tool that bulk loads RDF files into TDB triple store. Since...
Read more >
org.dataloader.DataLoader Java Examples - ProgramCreek.com
The following examples show how to use org.dataloader. ... newDataLoader(batchLoader, options); loader.load("keyA", "contextForA"); loader.load("keyB", ...
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