You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
I just tested your reproduction example and it can be fixed by removing the .order_by from both methods. Although I'll further investigate why this usage caused the issue... It shouldn't because it should have merged both prefetches into one.
Having said that, some tips for you:
Note that if you do self.some_related_items.all(), the optimizer has already that cached as a list. When you call .order_by() on it it will trigger another db request, causing n+1 problems. I would advise to use .all() only and, if you want to define a custom order, use it on your model's meta, which will make the results always be returned in that order unless overridden by another .order_by()
If you use the tip above, you can define the field as workflow_templates: list[Annotated["WorkflowTemplate", gql.lazy("..workflow_template.types")]], without needing to define a resolver, and strawberry-django-plus will resolve it correctly
If you really need to define a custom ordering there, you can use a prefetch object. E.g.:
This is a simple example and would actually not work in your case because you don't have WorkflowTemplate imported. But you can pass a lambda function to prefetch_related and import WorkflowTemplate at runtime to solve that.
Fabulous, thank you for your quick, helpful, and complete answer.
I will probably go for option 1. and 2. in this specific case, but may end up in case 3. for other types/objects
P.S. Oh one quick note on this mini project: if I name the method steps2 instead of steps the query complete (but maybe because the optimizer cannot figure out the link between the prefetch and the field name then). I don’t know if this helps you investigate, but just in case…
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I’m having this error (from
django.db
) with my actual schema for some queries, but only if theDjangoOptimizerExtension
is in place.I narrowed down my project to a minimum reproducible scenario.
My exact error message is:
How to reproduce it ?
/graphql/
to openGraphiQL
and try this query:app/schema.py
file and changewith_optim = False
⇒ the bug disappear.Site nodes
I know the optimizer is not a silver bullet and dynamically prefetching is hard.
But I expect the query to complete, even if not correctly optimized.
If any information is missing, please ask. I also can be contacted directly by email at [email protected].
The text was updated successfully, but these errors were encountered: