strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
416 stars 121 forks source link

get_queryset sends additional parameter self of type Field which it shouldnt #187

Closed deshk04 closed 2 years ago

deshk04 commented 2 years ago

when we have custom get_queryset classmethod we should receive 2 parameters queryset and info but instead we receive Field, queryset and info

Bug can described with an exmple from https://github.com/blb-ventures/strawberry-django-plus/blob/main/demo/schema.py

@gql.django.type(UserModel)
class StaffType(relay.Node):
    username: gql.auto
    email: gql.auto
    is_active: gql.auto
    is_superuser: gql.auto
    is_staff: gql.auto

    id_attr = "username"

    @classmethod
    def get_queryset(cls, queryset: QuerySet[AbstractUser], info: Info) -> QuerySet[AbstractUser]:
        return queryset.filter(is_staff=True)

@gql.type
class Query:
    """All available queries for this schema."""

    node: Optional[gql.Node] = gql.django.node()
    staff: Optional[StaffType] = gql.django.node()
    staff_list: List[StaffType] = gql.django.node()
    staffs: List[StaffType] = gql.django.field()
    staff_conn: relay.Connection[StaffType] = gql.django.connection()

added a new query staffs: List[StaffType] = gql.django.field() in to the demo example now, we have 'staffs' which is List and 'staff_conn' which is of type relay.Connection so when we call staffs

{
  staffs{
    id
    username
    isStaff
  }
}

we get TypeError: StaffType.get_queryset() takes 3 positional arguments but 4 were given

issue seems to be in https://github.com/strawberry-graphql/strawberry-graphql-django/blob/main/strawberry_django/fields/field.py

def get_queryset(self, queryset, info, order=UNSET, **kwargs):
        type_ = self.type or self.child.type
        type_ = utils.unwrap_type(type_)
        get_queryset = getattr(type_, "get_queryset", None)
        if get_queryset:
            queryset = get_queryset(self, queryset, info, **kwargs)
        return super().get_queryset(queryset, info, order=order, **kwargs)

queryset = get_queryset(self, queryset, info, kwargs) get_queryset is passing self which it shouldn't, it should be queryset = get_queryset(queryset, info, kwargs)

I will raise a PR soon.

Upvote & Fund

Fund with Polar

moritz89 commented 2 years ago

I guess this issue can be closed?

bellini666 commented 2 years ago

Yes! :)