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

Lookup fields not working

Open aareman opened this issue 2 years ago • 8 comments

Thank you for this library. Really helpful, migrating from graphene-django.

i'm not sure how the lookups are supposed to work.

@strawberry.django.filters.filter(models.Submission, lookups=True)
class SubmissionFilter:
    updated_date: date
    updated_date__lte: date
    updated_date__gte: date

It creates the types, but doesn't actually filter by the lookups. The plain singe date filter works as expected. image

aareman avatar Jan 21 '22 14:01 aareman

They are all date types

aareman avatar Jan 21 '22 14:01 aareman

@strawberry.django.filters.filter(models.Submission, lookups=True)
class SubmissionFilter:
    updated_date: strawberry.django.auto
submissions (filters: {updatedDate: {lte: "2020-02-02"}}) {
    submissionId
    updatedDate
}

See https://github.com/strawberry-graphql/strawberry-graphql-django/blob/main/strawberry_django/filters.py

g-as avatar Jan 21 '22 15:01 g-as

That is Sweet! Thank you for the quick response

I was stepping through the code and realized it was being swallowed in the image conditional. Since updated_date__lte didn't exist on the model. I added a re.sub on it to strip the __[a-z]*$ from it, and then it worked in the format i was doing. Do you think that format would be good support as well, so that you can filter, but restrict the lookups to specific types per field.

The current solution hands all the lookups to the user, which may not be what every case warrants, but is really clean IMO.

aareman avatar Jan 21 '22 15:01 aareman

I just tried out using auto as a type, but getting

TypeError: SubmissionFilter fields cannot be resolved. Unexpected type '<class 'strawberry_django.fields.types.auto'>'

aareman avatar Jan 21 '22 15:01 aareman

I updated to the latest version but still same error.

strawberry-graphql-django = {git = "https://github.com/strawberry-graphql/strawberry-graphql-django", rev="v0.3rc1"}

aareman avatar Jan 21 '22 16:01 aareman

After some more playing around, I came up with the following,

  • auto still not working for same reason.
  • Specifying type date does not work as the lookup type gets changed to date in the final schema
  • Manually adding FilterLookup[date] works as expected.

aareman avatar Jan 21 '22 16:01 aareman

Thanks for your feedback. I agree with you that it would be nice to have possibility to define which lookups to include to your filter. I'm not sure yet how would that API look like. Feel free to suggest if you have any ideas :)

Adding support for auto type is good idea!

la4de avatar Feb 19 '22 19:02 la4de

We have a test case for auto type in filters. See https://github.com/strawberry-graphql/strawberry-graphql-django/blob/main/tests/filters/test_filters.py#L12-L15

Could you share the code which is failing?

la4de avatar Feb 23 '22 18:02 la4de

See above code snippets

aareman avatar May 03 '23 18:05 aareman

Hey @aareman ,

It's been a while since you reported this issue, and since then strawberry-django has gone through a major refactor.

This issue should be fixed and working correctly in the current version, so I'm going to close this. If it is not, please tell me so I can reopen the issue

bellini666 avatar Jul 30 '23 16:07 bellini666