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

need for an async resolvers.update #233

@djstein

Description

@djstein

hey all. I'm trying to make a update_user mutation that verifies that the ID passed via the input is the same as the request user.

thankfully I found the node_id.aresolve_node function to get the user instance via the GlobalID. However on the resolvers.update this is a sync operation and it is unfortunate that it must be wrapped with sync_to_async

from asgiref.sync import sync_to_async
from django.contrib.auth import get_user_model
from strawberry.types import Info
from strawberry_django_plus import gql
from strawberry_django_plus.mutations import resolvers

from authentication.types import (
    User,
    UserInputPartialInput,
)

UserModel = get_user_model()

@gql.type
class Mutation:
    @gql.mutation
    async def update_user(self, info: Info, input: UserInputPartialInput) -> User:
        data = vars(input)
        node_id: gql.relay.GlobalID = data.pop("id")
        instance = await node_id.aresolve_node(info, ensure_type=UserModel)
        request_user = await info.context.request.auser()
        if instance != request_user:
            raise PermissionError("You can only modify objects you own.")

        return await sync_to_async(resolvers.update)(
            info, instance, resolvers.parse_input(info, data)
        )

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