blb-ventures / strawberry-django-plus

Enhanced Strawberry GraphQL integration with Django
MIT License
180 stars 47 forks source link

Optimizer throws an exception for union queries #139

Open eloff opened 1 year ago

eloff commented 1 year ago

You should be able to detect that it's a union query and use prefetch_related instead of select_related.

File "/app/.heroku/python/lib/python3.10/site-packages/strawberry_django_plus/type.py", line 318, in <lambda>
     lambda *args, **kwargs: resolve_connection(
File "/app/.heroku/python/lib/python3.10/site-packages/strawberry_django_plus/utils/resolvers.py", line 533, in resolve_connection
     nodes = ext.optimize(nodes, info=info)
File "/app/.heroku/python/lib/python3.10/site-packages/strawberry_django_plus/optimizer.py", line 639, in optimize
    return optimize(qs, info, config=config, store=store)
File "/app/.heroku/python/lib/python3.10/site-packages/strawberry_django_plus/optimizer.py", line 375, in optimize
     qs = store.apply(qs, info=info, config=config)
File "/app/.heroku/python/lib/python3.10/site-packages/strawberry_django_plus/optimizer.py", line 530, in apply
    qs = qs.select_related(*self.select_related)
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/query.py", line 1049, in select_related
    self._not_support_combined_queries('select_related')
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/query.py", line 1398, in _not_support_combined_queries
    raise NotSupportedError(

django.db.utils.NotSupportedError: Calling QuerySet.select_related() after union() is not supported.
bellini666 commented 1 year ago

Hey @eloff ,

Very interesting corner case. Would you like to try to open a PR to fix this? Otherwise I'll try to take a look at it next week