-
Notifications
You must be signed in to change notification settings - Fork 36
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
N+1 queries with cache_fragment and GraphQL Ruby Dataloader #126
Comments
Hey! Thanks for the reproduction steps, and you're right, it turned out that the gem does not play well with Dataloader. The problem is that Fiber eventually returns the value, but when you call I guess we need to find a way to fix it (somewhere around this line) but I'm not sure I will be able to work on that in the near future. |
@DmitryTsepelev |
Good point! I tried to move the block call later, when we can check if we already have the value in the cache. Need to polish specs a bit before it's ready for another round |
Looks like I fixed the issue (not for free—we'll have to perform an additional |
Thanks again! |
It will happen once, but might be still an overhead. In this sense batch approach was easier, because we could easily tell if it was already resolved or not, but in case of fibers we have to check that we don't have it in cache and, if not — make sure we let it resolve by itself ( |
When |
Summary
When using graphql-ruby (v2.4.8) together with graphql-fragment_cache (v1.20.5) on Rails 8.0.1, I encounter an unexpected N+1 query problem if I wrap a Dataloader-based field resolver with cache_fragment. Without cache_fragment, Dataloader batches the queries correctly. With cache_fragment, each user (in this example) triggers its own query, leading to N+1 behavior.
I have written an implementation and a test to illustrate the issue. Specifically:
Environment
Rails: 8.0.1
Ruby: 3.3.2
graphql-ruby: 2.4.8
graphql-fragment_cache: 1.20.5
The text was updated successfully, but these errors were encountered: