Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The get_queryset method is called twice when using relay connections #528

Closed
moritz89 opened this issue May 10, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@moritz89
Copy link
Contributor

moritz89 commented May 10, 2024

When implementing types that override the get_queryset() method as well as using relay connection, the get_queryset() method is called twice. This has the result, that when applying filters to the queryset they are applied twice. Since they are idempotent it does not change the query output, but adds unnecessary overhead.

Describe the Bug

The core of bug appears to be strawberry_django/fields/field.py:283-289 in StrawberryDjangoField:get_queryset(). When not using the relay connection the bug does not appear, but with it the if statement resolves to True

    get_queryset = getattr(type_, "get_queryset", None)
    if get_queryset:
        queryset = get_queryset(queryset, info, **kwargs)

and it is also called on the next line with

    queryset = super().get_queryset(
        filter_with_perms(queryset, info), info, **kwargs
    )

System Information

  • Operating system: Ubuntu 22.04
  • Strawberry version (if applicable): 0.39.2 and 0.227.2

Additional Context

This behavior is reproducible in the example app. If you already have a relay app, simply add a print in the overriding get_queryset method.

# example/django/app/types.py

@strawberry_django.type(
    models.Fruit,
    filters=FruitFilter,
    order=FruitOrder,
    pagination=True,
)
class Fruit(relay.Node):
    name: auto
    # color: Optional["Color"]

    @classmethod
    def get_queryset(
        cls,
        queryset: QuerySet,
        info: Info,
        **kwargs,
    ):
        print("Getting QS")
        return queryset.filter(name__startswith="s")
# example/django/app/schema.py

@strawberry.type
class Query:
    # fruit: Fruit = strawberry_django.field()
    # fruits: List[Fruit] = strawberry_django.field()
    all_fruits: ListConnection[Fruit] = strawberry_django.connection()

Attached is the diff, applied to commit 21c14e4

bug.zip

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@moritz89 moritz89 added the bug Something isn't working label May 10, 2024
@bellini666
Copy link
Member

Hey @moritz89 ,

Thanks for the report.

It wasn't actually that line, as one is the get_queryset from the field and the other from the type, but I found the issue :)

Should be fixed in the next release that I'm going to make in a few minutes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants