Open crazyscientist opened 3 years ago
Maybe this is related to #45?
Hi @crazyscientist ! The detection of foreign key id is done manually by using isinstance
(https://github.com/tfoxy/graphene-django-optimizer/blob/e5c57fc8e15691d37efde2af60f70e9e787e4344/graphene_django_optimizer/query.py#L321-L326). You can try to add GenericForeignKey
to the detection. If that works, feel free to create a PR together with a test for the GenericForeignKey
case.
Looking into this a little bit deeper, I think it would require some changes to the code to support this properly. At the time I made this, I only had a few generic relations in the codebase I was applying this library. But if a codebase has many fields like that, I see the value that solving this issue would provide.
If you have the time, feel free to create a PR. Will gladly review it.
Hi there,
first of all, thanks for the project, and I'd like to point out that prefetching works for
ForeignKey
andManyToMany
relations. Somehow onlyGenericForeignKey
is not recognized as optimizable.Versions
Models
Schema
Query
Problem
For non-generic relations the optimization works as expected, but with a generic relation (in the above example:
comments
) it does not. Based on the raw SQL queries executed, it looks like for every incident the comments are queried and for each comment the relatedUser
model is queried.Expected
Generic relations are tough. Django allows prefetching of the model that is a member of the generic relation, but nothing further, e.g.:
It would be unfair to expect that the optimizer overcomes this limitation of Django. It would be really great, if the optimizer could prefetch the comments in this example.