-
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
Querying for allSpecies and homeworld executes 38 SQL queries #12
Comments
I generated the list of SQL queries by adding the following configuration to swapi_graphene/settings.py
|
Even better: is it possible to detect the fields being requested and update the SELECT fields in those statements, using the Django .only() QuerySet method? My ideal SQL queries for this GraphQL query would be either this (using prefetch_related):
Or this (using a select_related() join):
|
On closer inspection of the docs, it looks like the solution here is to use a DataLoader - any chance of some example code for that? http://docs.graphene-python.org/en/latest/execution/dataloader/ |
See graphql-python#12 for background.
I got this working using a dataloader: simonw@cea0451 Corresponding SQL:
|
Oops, correct resolver on the Specie class looks like this:
|
I think that's wrong too. The PlanetLoader batch_load_fn(keys) method needs to return objects for the keys in the same order that the keys were provided. |
This function needs to return values in the exact same order as the keys that were passed to the function. See graphql-python#12
OK, I think this is the correct implementation of the DataLoader:
|
This query:
Executes 38 sql queries, looking something like this:
Full list here: https://gist.github.com/simonw/762a98de85a2f113d1d7ec6026f73ee4
Is it possible to detect this case and then use select_related() or prefetch_related() here to join against the relevant table and get everything done in just one or two queries?
If so, an example showing how to do that would be a fantastic illustration of a slightly more complex use-case for graphene-django (I tried myself and couldn't figure out how to do it).
The text was updated successfully, but these errors were encountered: