Open vecchp opened 3 months ago
this may be a more performant workaround
@strawberry_django.field(
annotate={
"_private_details": lambda info: Case(
When(
Exists(
filter_for_user(
models.Note.objects.all(),
info.context.request.user,
[PrivateNotePermissions.VIEW],
)
),
then=F("private_details"),
),
default=Value(None),
),
}
)
def private_details(self, root: models.Note) -> Optional[str]:
return root._private_details
Hey @vecchp ,
The main difference between the 2 codes is that in the first one you are checking for HasSourcePerm
, which will check permissions on top of the root object itself when resolving private_details
, and in the second one you are doing the prefetch with the permissions.
This is a similar issue to https://github.com/strawberry-graphql/strawberry-django/issues/337 in which the optimizer is not currently able to optimize some more complex nested lists/connections, specially when they are using pagination, filtering, permissioning (like this), etc
Thanks for reporting this! I'll soon try to take a look at that nested issue and will try to solve this issue together :)
Seems like a similar library for graphene has worked around this issue. At least with nested queries maybe unrelated to perms.
https://github.com/MrThearMan/graphene-django-query-optimizer/issues/62 https://github.com/MrThearMan/graphene-django-query-optimizer/commit/355b50783cc239c76ca1dbeb11ab515d87197c2c
Seems like a similar library for graphene has worked around this issue. At least with nested queries maybe unrelated to perms.
MrThearMan/graphene-django-query-optimizer#62 MrThearMan/graphene-django-query-optimizer@355b507
Hrmm thanks for linking those :), I'll take a look at that for inspiration!
Describe the Bug
Nesting permissions i.e
private_details
checked byHasSourcePerm
yields unnecessary queries that could be optimized. Current code yields 6 queries, while the workaround below results in 2.Additional Context
Workaround
Upvote & Fund