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

Optimise manually prefetched field #244

@adammsteele

Description

@adammsteele

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions