blb-ventures / strawberry-django-plus

Enhanced Strawberry GraphQL integration with Django
MIT License
178 stars 47 forks source link

Got an unexpected keyword argument 'filters' #245

Open he0119 opened 1 year ago

he0119 commented 1 year ago

Argument filters shows up in GraphiQL, but still got an error. It works fine before upgrading to v3.

image image

Related code: https://github.com/he0119/smart-home/commit/9922c788a2d79761bd6c100c3bd4b13c31cfb4d6#diff-af3602ede1befa32df28d961add5b48aaf07992465fb6d0f1dbb50e4a0568cdbR79-R85

@gql.django.type(models.Device, filters=DeviceFilter, order=DeviceOrder)
class Device(relay.Node):
    name: auto
    device_type: auto
    location: auto
    created_at: auto
    edited_at: auto
    is_online: auto
    online_at: auto
    offline_at: auto
    token: auto

    # FIXME: Device.autowatering_data() got an unexpected keyword argument 'filters'
    @gql.django.connection(
        gql.django.ListConnectionWithTotalCount[AutowateringData],
        filters=AutowateringDataFilter,
        order=AutowateringDataOrder,
    )
    def autowatering_data(self, info) -> Iterable[models.AutowateringData]:
        return models.AutowateringData.objects.all()
bellini666 commented 1 year ago

Hey @he0119 ,

I don't think we are considering this corner case yet... I might try to take a look at that in the future.

In the mean time you can do the following:

from strawberry_django.ordering import apply as apply_order
from strawberry_django.filters import apply as apply_filters

class Device(relay.Node):
    ...

    @gql.django.connection(
        gql.django.ListConnectionWithTotalCount[AutowateringData],
    )
    def autowatering_data(
        self,
        info,
        filters: AutowateringDataFilter | None = UNSET,
        order: AutowateringDataOrder | None = UNSET,
    ) -> Iterable[models.AutowateringData]:
        qs = models.AutowateringData.objects.all()
        if filters is not UNSET:
            qs = apply_filters(qs, info=info)
        if order is not UNSET:
            qs = apply_ordering(qs)
        return qs
he0119 commented 1 year ago

Thanks, it works now.