-
Notifications
You must be signed in to change notification settings - Fork 423
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
How do I implement efficient data fetches for nested objects? #167
Comments
Generally Facebook suggests using something like https://github.com/cksac/dataloader-rs, which is what they do internally |
@LegNeato Ah, thank you. Yeah, I saw that Data Loader seems to be common in GraphQL implementations from other languages. If this is the right way, it might be helpful to have an example of its integration with Juniper — given heavy reliance on futures, etc., it's somewhat non-trivial to integrate. |
There are essentially two approaches to this. One is a futures and dataloader style async approach, tracked in #2. The other option is to inspect the requested schema and smartly determining what to fetch in a root resolver. |
Closing this issue here, feel free to discuss further in one of the two other issues. |
AKA the GraphQL N+1 Problem #387 . |
Hello, great work with the project. I have a question pertaining to how to build an efficient implementations given nested objects.
To demonstrate, take a sample blog schema where you have articles that have comments, and comments that have favorites. Favorites are their own relation with an associated
user_id
to the user that favorited that comment.A query to fetch all the data you need to render an article would go two relations deep like this:
The system is implemented on a relational store, and a simplified version of your Juniper article object looks like this:
Comments are similar, with a nested
favorites
field:The trouble is that if we execute the query above, it will resolve successfully, but it will do so in a way that's degenerately inefficient. To get all our favorites we'll execute N + 1 total queries (1 to get comments, and then N to get favorites for each comment), and every further level of nesting will multiply the total number of queries by another N.
What we'd do ideally is when fetching favorites, use this naive implementation above if we're only fetching them for a single comment, but when we're fetching them for a set of comments, have a higher level execute only a single query:
And then partition the results and distribute them to the underlying "favorites" objects being resolved.
Do you have any recommendations for how to make this sort of pattern possible in a relatively performant and sustainable (in the sense of code complexity) way? I've found some references to "context switching", and looking the source code suggests that it seems like something that might be what I'm looking for, but the documentation for it is light. Is that what I should be using?
Thanks!
The text was updated successfully, but these errors were encountered: