yezyilomo / django-restql

Turn your API made with Django REST Framework(DRF) into a GraphQL like API.
https://yezyilomo.github.io/django-restql
MIT License
616 stars 43 forks source link

Suggestion: make query impact queryset to reduce django sql selection #237

Open fturhan opened 3 years ago

fturhan commented 3 years ago

Hi, i'm using this module for a while and i thinks it would be awesome if querys can impact the django queryset selection (with only and/or defer) to significantly reduce the amount of information being selected. But i do not know how to achieve this. Maybe this can be a feature to add to the module.

yezyilomo commented 3 years ago

I'm not sure if I have understood what you described, but isn't it what's performed by EagerLoadingMixin?.

fturhan commented 3 years ago

I'm currently using Django debug panel and when i use restql to reduce the amount of information, i can see that i fetch the whole objects (nested objects too) in the sql part, however i only need some precise informations.

yezyilomo commented 3 years ago

Are you using EagerLoadingMixin in your views?.

fturhan commented 3 years ago

Yes i'm using it

yezyilomo commented 3 years ago

I’ve never tested this lib on Django debug panel but tests suggests otherwise take this below as an example https://github.com/yezyilomo/django-restql/blob/0d2d372b7fac225758f4683434d3c279ba62841d/tests/test_views.py#L747-L818 You can see there that the number of queries due to nested fields went from 5 to 2 after using EagerLoadingMixin.

fturhan commented 3 years ago

I'm sorry but i have some difficulties to explain my issue. for example when i query {course{books{title}} and then i check what was fetch in the sql part, i found out that all fields were fetch

yezyilomo commented 3 years ago

for example when i query {course{books{title}} and then i check what was fetch in the sql part, i found out that all fields were fetch

Ooh!, now I got you. Djang RESTQL doesn't support this for now, but since you brought it we might think of a way to implement it, I think it could help boosting the speed of database queries. I know one way to make ORM select specific fields is applying .only([field1, field2, ...]) on your queryset, since the list of fields selected is dynamic we could find a way to pass them before serialization is done.

resurrexi commented 3 years ago

This would be a nice addition, but I can foresee issues where there is no underlying field in the database table, even though it is represented in the serializer such as a SerializerMethodField.

Now that I think about this suggestion, I wonder if Django RESTQL limits the querying of fields if there is an explicit fields argument passed into a NestedField, e.g:

work_order = NestedField(
    WorkOrderSerializer,
    read_only=True,
    fields=["work_order_id"]  # will the query planner only pull `work_order_id`?
)