strawberry-django-plus icon indicating copy to clipboard operation
strawberry-django-plus copied to clipboard

Got an unexpected keyword argument 'filters'

Open he0119 opened this issue 1 year ago • 2 comments

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()

he0119 avatar Jun 17 '23 02:06 he0119

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

bellini666 avatar Jun 17 '23 16:06 bellini666

Thanks, it works now.

he0119 avatar Jun 18 '23 00:06 he0119