-
Notifications
You must be signed in to change notification settings - Fork 47
Query optimization based on type hints for non-model fields? #154
Comments
There are.. For now I can work with using |
Hey @blueyed , Hrm, we can probably try to do something like that. But having said that, have you take a look at model properties? You can use it to create a |
Update docs link in README
@bellini666 My workaround now is to use @django_type(models.StageRecipe, filters=StageRecipeFilter, pagination=True)
class Bar:
bar: Bar
@gql.django.field(
select_related=[
"foo__current_version",
],
only=[
"foo__current_version",
],
)
def current_foo(self) -> Foo | None:
return self.foo.current_version
@classmethod
def get_queryset(cls, queryset, info, **kwargs):
qs = queryset.exclude(operational_recipe__status=RecipeStatus.DRAFT)
for selected_field in info.selected_fields:
if selected_field.name != "bars":
continue
for selected_field2 in selected_field.selections:
if selected_field2.name != "currentFoo":
continue
for selected_field3 in selected_field2.selections:
if selected_field3.name == "baz":
qs = qs.prefetch_related("Foo__current_version__baz")
...
return qs @django_type(
models.Bar,
pagination=True,
only=[
"status", # for ConcurrentTransitionMixin.
"current_version",
],
)
class Bar:
baz: list["Bar"] |
Hrmm, that I probably need to find a way to handle those. |
A field that represents a model property will not be handled by the query optimizer:
strawberry-django-plus/strawberry_django_plus/optimizer.py
Lines 163 to 166 in 9a54dd0
It could get derived from the type hint though, couldn't it?
Specifying
field_name
, which gets translated intodjango_name
works.Is this supposed to work like/for this?
Are there unwanted side effects given that it is of type Foo, but not necessarily the actual foreign key / linked object?
As for now I am happy with adding only/select_related manually (related: #152).
The text was updated successfully, but these errors were encountered: