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

Nested annotations and optimizations break when using strawberry_django.auth.current_user #594

Open
pfcodes opened this issue Jul 22, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@pfcodes
Copy link

pfcodes commented Jul 22, 2024

Describe the Bug

Annotations and optimizations don't work on fields created with strawberry_django.auth.current_user()

With the following code:

@strawberry.type
class Query:
    user: User = strawberry_django.node(extensions=[IsAuthenticated()])
    current_user: User = strawberry_django.auth.current_user()
@strawberry_django.type(UserModel)
class User(Actor, relay.Node):
    friend_count: int = strawberry_django.field(annotate=Count("friends"))

friend_count will work on the user field just fine, but attempting to access it on current_user and you get this error:

{
 "data": null,
 "errors": [
   {
     "message": "'User' object has no attribute 'friend_count'",
     "locations": [
       {
         "line": 3,
         "column": 5
       }
     ],
     "path": [
       "currentUser",
       "friendCount"
     ]
   }
 ]
}

It's not just annotations, it happens for all the optimizations too.

System Information

  • Strawberry version (if applicable): 0.47.0

Additional Context

There is another similar issue here. They might be related.

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
@pfcodes pfcodes added the bug Something isn't working label Jul 22, 2024
@bellini666
Copy link
Member

annotate support still have some issues, like this case

The issue here is similar: The current_user resolver will return a user instead of a queryset containing the user, meaning the optimizer can't "annotate" it anymore.

I'm going to try to see if we can change current_user to return a queryset instead to support those cases, but for now what you can do is to define your own custom resolver like this:

from strawberry_django.auth.queries import resolve_current_user

@strawberry.type
class Query:
    @strawberry_django.field
    def current_user(self, info: Info):
        user = resolve_current_user(info)
        return User.objects.filter(pk=user.pk)

This is going to cause an extra DB query, but that one can be annotated.

Annoter option would be to not use annotate and instead rely on a data loader for that friend_count field (which IMO is better)

@erwinfeser
Copy link

annotate support still have some issues, like this case

The issue here is similar: The current_user resolver will return a user instead of a queryset containing the user, meaning the optimizer can't "annotate" it anymore.

I'm going to try to see if we can change current_user to return a queryset instead to support those cases, but for now what you can do is to define your own custom resolver like this:

from strawberry_django.auth.queries import resolve_current_user

@strawberry.type
class Query:
    @strawberry_django.field
    def current_user(self, info: Info):
        user = resolve_current_user(info)
        return User.objects.filter(pk=user.pk)

This is going to cause an extra DB query, but that one can be annotated.

Annoter option would be to not use annotate and instead rely on a data loader for that friend_count field (which IMO is better)

Could you show an example about relying on a data loader for that friend_count?

@bellini666
Copy link
Member

Hey @erwinfeser ,

Sure, there's an example in this comment here: #549 (comment)

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

3 participants