Closed AlphaRishi1229 closed 3 months ago
I think this should be possible fairly easily. If I understand this correctly, here an inspection of the field's prefetches should be added. If a Prefetch
exists with to_attr
being the field, then use that Prefetch
to figure out the actual name of the model field. I will play around with this tomorrow if I can.
Hey @diesieben07 . Thanks for trying to help with this :)
Left me know if you need any help with that. Either ping me here or on our discord community
Thank you for the heads up @bellini666. I got something working, it would be great to get some feedback about whether there are additional things that I have missed, as I am not deeply familiar with the optimizer's workings. I have created a pull request with more information about my changes: https://github.com/strawberry-graphql/strawberry-graphql-django/pull/473
Thanks @diesieben07 :)
Will take a look at it very soon!
@bellini666 and @diesieben07 any update on this?
@Sanyambansal76 I will hopefully have time to look at the remaining work on my pull request on the weekend.
The strawberry django optimiser is not working as expected when we provide hints (like custom prefetch) for a field in a type. It results in n+1 queries when we do that
Describe the Bug
Suppose I have 2 models Model and ModelVariables, ModelVariables has a foreign key reference to Model. So, in django we can refer ModelVariables from Model using the
related_name
. Consider my models to be something like this:Since,
ModelVariable
has a foreign key toModel
. I can get all theModelVariable
for aModel
using ->Model.model_variables.all()
Now, for the above models I created strawberry django types in the following way
Now, in my ModelType, I want
editable_model_variables
field to return all ModelVariables that are editable, so I added a prefetch_related for it and updated the query. Which works. Now, when I execute a query and mentionrelated_field
in the query. It causes n+1 queries. The obvious reason is that, in my prefetch related query I did not select_related therelated_field
.AFAIK the optimiser takes these hints and not just override the optimised queries.
I also tried passing
field_name="model_variables"
to the@strawberry_django.field
decorator for hinting but that didn't work too.System Information
Upvote & Fund