-
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
Look ahead and N+1 query problem #387
Comments
Hey there. This is more of a general SQL question rather than Juniper specific. The easiest solution here would be:
You'd also need to to the same thing for the nested That way you can limit the whole request to 3 DB queries. LookaheadThe lookahead functionality only comes in if you either want to limit the loaded fields to the fields requested by the user, or if you have additional arguments on the nested selections. In that case you would use the information from look ahead to determine the requested fields and the filter arguments, so you can alter your database query accordingly. Restricting the fields is somewhat awkward with Diesel, so you should probably stick to loading all fields. Also, you don't seem to have any arguments, so you would not need to use lookahead at all here. |
Hi @theduke this is awesome! Thank you for the clarification. That's what I thought it should be done more on the db level but thought that |
PS: you might also want to take a look at https://github.com/davidpdrsn/juniper-eager-loading. I haven't used it yet, and it is still new and experimental, but is aims to make exactly this problem easier. I'm closing this, but feel free to ask more questions here. |
@takeit If so you might also enjoy https://github.com/davidpdrsn/graphql-app-example. It is a complete example app I maintain that uses the exact stack you mention plus https://github.com/davidpdrsn/juniper-from-schema. The setup is very close to what we do at @tonsser and its working well for us. |
@davidpdrsn this looks great! Thanks! It should be linked to the readme file in the Juniper repository. |
Hi, I'm a beginner to Rust and I try to build a simple GraphQL API using Juniper, Rocket and Diesel (postgres).
I can't quite figure out how this
look ahead
thing works and how to use it to solve the N+1 query problem as described here #234, here #136 and here #167. I just can't figure it out. The below code works fine but it makes a lot of queries.I have added the comments in the code with questions.
my
schema.rs
:my
models.rs
:my
graphql.rs
This is the query I run:
I would really appreciate your feedback.
The text was updated successfully, but these errors were encountered: