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

Django LazyObject is not considered in mutation resolvers

Open ryanprobus opened this issue 11 months ago • 1 comments

Describe the Bug

The user from Django's request.user is set to a LazyObject in the AuthenticationMiddleware. I believe a LazyObject is always an Iterable even if the underlying object isn't because LazyObject implements iter. When passing the request.user to mutations resolvers.update, it will think its an Iterable and call list(instance). However, this causes an error because the user object isn't actually an iterable.

This could be resolved by doing a check at the beginning of the resolvers similar to how its done in django:

if isinstance(instance, LazyObject):
  instance = instance.__reduce__()[1][0]

Though for me to fix this, I just passed user._wrapped to the resolver.

This would also affect the delete mutation resolver as well.

However, I believe this exact scenario is the only case where this would happen. I didn't see references to LazyObject wrapping models except for this case. So since its obscure, maybe its not worth considering? Just creating this issue to track what I ran into and see if y'all have any thoughts.

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

ryanprobus avatar Aug 08 '23 18:08 ryanprobus

Hey @ryanprobus,

Interesting... I don't think I have any use case for those in my projects, so I've never stumbled upon this.

I think it is fine for us to add an if isinstance(instance, LazyObject) there and in other places that makes sense. Do you want to try to open a PR for that? I'll gladly review it :)

bellini666 avatar Aug 13 '23 15:08 bellini666