-
Notifications
You must be signed in to change notification settings - Fork 108
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
[question] Using inverse ActiveRecord associations to avoid N+1 queries? #123
Comments
One of the advantages of AssociationLoader in the examples is that it actually loads the association. Your example seems more like the The danger of doing too much in the way of filtering in the query is that it could result in multiple queries being performed on the same data in the database, where it could be more appropriate to do the additional filtering client-side to avoid doing an extra query. We use a separate loader for calculations (i.e. aggregations), which is something we could probably add an example. I don't think it makes sense to combine that with the RecordLoader to make it more complicated by adding the internally divergent code path.
This makes the example less general and adds an additional dependency for the example that makes it harder to use. I would prefer to leave this sort of customization to the library user.
Custom loaders have the advantage of adding a layer of abstraction, which can be useful to decouple the code using the loader from the underlying implementation when it is expected to change. When an abstraction isn't needed, then re-using more general associations makes sense. |
@dylanahsmith Thank you for your extensive answer! |
Hi @dylanahsmith @martinliptak ! Thank you for your explanation. Can you explain me why loading the association using the preloader has any advantage?
From my point of view, the only important thing is to calculate the value and populate the |
I've built a smart loader that can load any association using inverse ActiveRecord associations.
Then I can do this:
All explained in detail here:
https://medium.com/@martinliptak/loading-activerecord-associations-with-graphql-210ec315da0b
Do you think this approach has any downsides I haven't noticed? Do you load more complex associations using something similar or do you implement a loader per every association? Would it be useful to put this into
examples/
? The current association loader fromexamples/
can preload associations, but doesn't allow you to specify custom scopes or run an aggregation.The text was updated successfully, but these errors were encountered: