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

DataLoaderRegistry inefficiency #1902

Closed
benmccann opened this issue May 19, 2020 · 4 comments
Closed

DataLoaderRegistry inefficiency #1902

benmccann opened this issue May 19, 2020 · 4 comments

Comments

@benmccann
Copy link
Contributor

The DataLoaderRegistry should be request-scoped. That means that users are creating every DataLoader for every request even if none or only one or two are used.

Right now we would do something like:

    dataLoaderRegistry.register("user", DataLoader.newDataLoader(userBatchLoader));

We could probably fix this by making DataLoaderReigstry a singleton and instead registering a function that would provide a DataLoader:

    dataLoaderRegistry.register("user", () -> DataLoader.newDataLoader(userBatchLoaderProvider.get()));
@bbakerman
Copy link
Member

This makes a lot of sense. DataLoaderRegistry is in the https://github.com/graphql-java/java-dataloader code base.

I have raised this issue there as well

graphql-java/java-dataloader#67

@bbakerman bbakerman added this to the 16.0 milestone May 23, 2020
@andimarek andimarek removed this from the 16.0 milestone Dec 9, 2020
@bbakerman
Copy link
Member

I looked into this and it's really kinda pointless.

We can make it lazy but as soon as you called dispatchAll the it needs to materialise the dataloaders and dispatch them

We would have to write a fancy MemoizingSupplierWithTracking that could decide if its every been asked for and then filter them out from the list. In short I am not sure that its worth it.

I would love to see more evidence now of the costs of creating a DL even if its not needed in a certain graphql request.

@mumutu66
Copy link

I also found it is a problem in real world when the schema may contains thousand business objects and every toOne related query will cause an "n +1" problem and dataloaders is required , if the server has no sense a query will hit how many toOne query , then it should create thousand dataloader which will definitely slow down the server. so one way is to do the lazy loader as @benmccann mentioned and do the left optimization or if there has certain tool can know which dataloader is needed from every query string

@bbakerman
Copy link
Member

org.dataloader.DataLoaderRegistry#computeIfAbsent now exists - you can lazily create them.

Since DataLoaders are stateful, they themselves must be per request otherwise badness will happen.

@dondonz dondonz closed this as completed Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants