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

using query kwarg with get_serializer() will not function #241

Closed caesarleong closed 3 years ago

caesarleong commented 3 years ago

I am using the 0.13.0 new feature "query kwarg" and I found if I follow the document way to use it. It can work perfectly.

objs = Course.objects.all()
query = "{name, books{title}}"
serializer = CourseSerializer(objs, many=True, query=query)
print(serializer.data)

But If I modify it with get_serializer(), it will ignored query kwarg and return all field.

objs = Course.objects.all()
query = "{name, books{title}}"
serializer = self.get_serializer(objs, many=True, query=query)
print(serializer.data)
yezyilomo commented 3 years ago

Thank you for reporting this, I think it’s a bug. I think this is the part causing it https://github.com/yezyilomo/django-restql/blob/36f8940e7313d495686c581bbd2b4c4929defa7d/django_restql/mixins.py#L364-L369

caesarleong commented 3 years ago

Thanks for quick response. Please don't mind me report another issue here which also relate to query kwarg. If I use CourseSerializer without query, it will get exception too because no request.

objs = Course.objects.all()
query = "{name, books{title}}"
serializer = CourseSerializer(objs, many=True)
print(serializer.data)
yezyilomo commented 3 years ago

Thanks for quick response. Please don't mind me report another issue here which also relate to query kwarg. If I use CourseSerializer without query, it will get exception too because no request.

objs = Course.objects.all()
query = "{name, books{title}}"
serializer = CourseSerializer(objs, many=True)
print(serializer.data)

I believe this has been fixed too, The order for finding a query string as of now is https://github.com/yezyilomo/django-restql/blob/0d2d372b7fac225758f4683434d3c279ba62841d/django_restql/mixins.py#L411-L423 You can see that if there’s no query kwarg and request that function returns None, when the value of parsed query is None it means the query has not been passed so all fields are returned. You just have to wait for the new release which is going to be out soon.

caesarleong commented 3 years ago

Thanks! looking forward the new release!!!