strawberry-graphql / strawberry-django

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

ListConnectionWithTotalCount and filter custom resolver #511

Closed tasiotas closed 6 months ago

tasiotas commented 6 months ago

version 0.35

I am migrating to new Filtering and Ordering, so far so good (Type's filter/order) but now I reached relay connections and cannot get Custom Resolver working.

@strawberry.django.type(User, filters=UserFilter, order=UserOrder)
class UserType(strawberry.relay.Node):

    @strawberry.django.connection(ListConnectionWithTotalCount[SaleType], filters=PurchaseFilter)
    def purchases(self) -> List["SaleType"]:
        print("getting purchases")
        return self.purchases

@strawberry.django.filter(Sale)
class PurchaseFilter:
    @strawberry.django.filter_field()
    def search(self, value: str, prefix: str) -> Q:
        print("hello")
        return Q(name__contains=value)
query MyQuery {
  user(slug: "tas") {
    purchases(filters: {search: "dd"}) {
      edges {
        node {
          id
          price
        }
      }
    }
  }
}

When I guery purchases, custom resolver's search function never gets called. I can see filters showing up properly in the schema via graphiql.

Am I doing something wrong when defining Filter on ListConnection?

thank you

Upvote & Fund

Fund with Polar

bellini666 commented 6 months ago

Hey @tasiotas ,

I wrote this test to try to replicate your issue: https://github.com/strawberry-graphql/strawberry-django/commit/5757a20b525b8216bd1fc5ceced048a9be378f8f

The test seems to be passing fine.

Is there anything different that you are doing? If so, can you help me to reproduce the issue?

tasiotas commented 6 months ago

Hi,

Thank you for adding tests and confirming that its all good on your end. During preparation of repro I found an issue in my code.

self.purchases in field's resolver was returning List of objects, instead of QuerySet. New filters just quietly ignore it without any error, so I was confused why filters are not being triggered at all.

@strawberry.django.connection(ListConnectionWithTotalCount[SaleType], filters=PurchaseFilter)
 def purchases(self) -> List["SaleType"]:
     print("getting purchases")
     return self.purchases

My bad, thanks!