Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Optimise manually prefetched field #244

Open
adammsteele opened this issue Jun 16, 2023 · 1 comment
Open

Optimise manually prefetched field #244

adammsteele opened this issue Jun 16, 2023 · 1 comment

Comments

@adammsteele
Copy link

In this comment you say that we should be able to optimise the queryset of a manually prefetched field using optimize, however this does not appear to work.

The following code in the demo directory shows that the only optimisation is not applied to my_issues:

@gql.django.type(Milestone, filters=MilestoneFilter, order=MilestoneOrder)
class MilestoneType(relay.Node):
    name: gql.auto
    due_date: gql.auto
    project: ProjectType
    issues: List["IssueType"]

    @gql.django.field(
        prefetch_related=[
            lambda info: Prefetch(
                "issues",
                queryset=optimize(
                    Issue.objects.filter(
                        Exists(
                            Assignee.objects.filter(
                                issue=OuterRef("pk"),
                                user_id=info.context.request.user.id,
                            ),
                        ),
                    ),
                    info,
                ),
                to_attr="_my_issues",
            ),
        ],
    )
    def my_issues(self) -> List["IssueType"]:
        return self._my_issues  # type: ignore

    @gql.django.field
    async def async_field(self, value: str) -> str:
        await asyncio.sleep(0)
        return f"value: {value}"

Is it possible to use optimize in this way?

Thanks for the library!

@bellini666
Copy link
Member

Hey @adammsteele ,

Yes, it should indeed work (if it doesn't, then we have a bug =P)

You said that the only optimization is not working. But are the other optimizations working for it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants