strawberry-graphql / strawberry-django

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

QS for django field #307

Open patrick91 opened 1 year ago

patrick91 commented 1 year ago

Similar to what we do for filters, we could allow people to add a static qs to fields:

@strawberry.type
class Query:
    posts: list[PostType] = strawberry_django.field(Post, queryset=Post.objects.published())
    all_posts: list[PostType] = strawberry_django.field(Post, queryset=Post.objects.all()) # with permission
CleanShot 2023-07-14 at 21 33 43@2x

Upvote & Fund

Fund with Polar

bellini666 commented 1 year ago

I like this! :)

My only suggestion here would be to receive the queryset as a callable instead of the queryset directly. The queryset has some stateful behaviours, meaning that if we are using the same one all the time it might product some weird issues.

That could either be a queryset: Callable[[], QuerySet] or queryset: Callable[[Info], QuerySet]. Probably the second one is better, which allows some common dynamic usage, like filtering it by the current user, etc

devkral commented 1 year ago

this is currently be done with the resolver and afterward is get_queryset of in my case ContentNode (the graphql pendant to Content) applied. What is the advantage of the new way?

e.g.


    @strawberry_django.connection(strawberry.relay.ListConnection[ContentNode])
    def contents(
        self,
        info: Info,
    ):
        return Content.objects.all()
patrick91 commented 1 year ago

Passing a callable would be the same as using a resolver, so what @devkral mentioned 😊

I think we could keep it simple and only allow static querysets, as a convenience feature :)