strawberry-graphql / strawberry-django

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

Filtering cannot use relational reflection fields #84

Closed star2000 closed 2 years ago

star2000 commented 2 years ago

https://github.com/strawberry-graphql/strawberry-graphql-django/blob/7ec710bf62bd599442601d0a9605e7bedf1dfca1/strawberry_django/filters.py#L90-L92

django_model._meta._forward_fields_map should be changed to

from django.db.models.sql.query import get_field_names_from_opts
get_field_names_from_opts(django_model._meta)

Upvote & Fund

Fund with Polar

la4de commented 2 years ago

Could you describe you problem you are solving by suggesting this change? It would be good to understand the problem so that we can implement proper test case for that.

I agree with you that It's probably good idea to use django API for that.

star2000 commented 2 years ago

.eg

import strawberry_django
from strawberry_django import auto
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission

@strawberry_django.filters.filter(Permission, lookups=True)
class PermissionFilter:
    id: auto
    name: auto
    content_type: 'ContentTypeFilter'  # available
    codename: auto

@strawberry_django.filters.filter(ContentType, lookups=True)
class ContentTypeFilter:
    permission: 'PermissionFilter' # not available
    id: auto
    app_label: auto
    model: auto
star2000 commented 2 years ago

Maybe I didn't show it clearly enough, see this

image