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
The schema that I'm working with often has nested field types with prefetch_related optimizer hints. I noticed that if one field resolver has a prefetch_related defined, nested field resolvers would not get optimized, which resulted in N+1s.
Example schema:
@strawberry_django.type(Project, filters=ProjectFilter, pagination=True)classProjectType(relay.Node, Named):
@strawberry_django.field(pagination=True,prefetch_related=["milestones"], )defmilestones(self) ->list["MilestoneType"]:
# This resolver is contrived for this example. In actuality more would# be happening other than returning .all(), such as extra filtering, etc.returnself.milestones.all()
@strawberry_django.type(Milestone, filters=MilestoneFilter, order=MilestoneOrder, pagination=True)classMilestoneType(relay.Node, Named):
issues: list["IssueType"] =strawberry_django.field(
filters=IssueFilter,
order=IssueOrder,
pagination=True,
)
@strawberry_django.type(Issue)classIssueType(relay.Node, Named):
priority: strawberry.auto
milestones is prefetched appropriately, but then the optimizer doesn't continue with optimizing the issues queries. Changing the milestones field resolver back to milestones: list["MilestoneType"] = strawberry_django.field(pagination=True) resulted in these expected db queries with issues prefetched:
I'm unsure if this is an actual bug or expected behavior based on the schema setup. I know there were two different changes that skip optimizations early:
This check targets prefetches with filters/annotations, but I think it's also having the side effect of skipping nested optimizations if prefetches are querysets without filters/annotations
So I believe nested fields aren't reaching _get_hints_from_django_relation / _get_model_hints due to those early returns. I tried removing these early return blocks locally out of curiosity, and that reduced the number of issue queries back to one (though I don't believe that's the right fix, it was purely for testing).
If this is an issue due to schema setup, is there general guidance on how to set up resolvers to ensure that all prefetch_related optimizer hints are successfully handled?
Thanks in advance!
System Information
Operating system: macOS
Strawberry version (if applicable): 0.55.0
The text was updated successfully, but these errors were encountered:
That's a case where it is very hard to properly workaround...
Before those PRs that you mentioned, the optimizer would try to get hints based on the name of the resolver, but that resulted in a lot of issues (like #559)
The idea of those fixes are: If you are defining a resolver, you are responsible for how it will be resolved, and the maximum that the optimizer will do is to use the hints you can pass to strawberry_django.field.
I'm not really sure how we can workaround this one... will think about it, but open to suggestions!
Describe the Bug
The schema that I'm working with often has nested field types with
prefetch_related
optimizer hints. I noticed that if one field resolver has aprefetch_related
defined, nested field resolvers would not get optimized, which resulted in N+1s.Example schema:
Test query:
Resulting DB queries from test:
milestones
is prefetched appropriately, but then the optimizer doesn't continue with optimizing theissues
queries. Changing themilestones
field resolver back tomilestones: list["MilestoneType"] = strawberry_django.field(pagination=True)
resulted in these expected db queries with issues prefetched:Additional Context
I'm unsure if this is an actual bug or expected behavior based on the schema setup. I know there were two different changes that skip optimizations early:
prefetch_related
is in the field's storeSo I believe nested fields aren't reaching
_get_hints_from_django_relation
/_get_model_hints
due to those early returns. I tried removing these early return blocks locally out of curiosity, and that reduced the number of issue queries back to one (though I don't believe that's the right fix, it was purely for testing).If this is an issue due to schema setup, is there general guidance on how to set up resolvers to ensure that all
prefetch_related
optimizer hints are successfully handled?Thanks in advance!
System Information
The text was updated successfully, but these errors were encountered: