strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
MIT License
394 stars 115 forks source link

`DjangoPermissionExtension` does not support Channels requests. #330

Open nrbnlulu opened 11 months ago

nrbnlulu commented 11 months ago

On Channels requests the context is a dict so you'll get an AttributeError. https://github.com/strawberry-graphql/strawberry-graphql-django/blob/3a3a66e236acbc54ad7079ecfa2b92508f3ad60c/strawberry_django/permissions.py#L330

Upvote & Fund

Fund with Polar

bellini666 commented 11 months ago

That's actually a general strawberry issue. Subscriptions doesn't support field extensions currently :(

nrbnlulu commented 11 months ago

Just ran some tests, I think it is supported with raw strawberry. Based on what do you say that?

i.e

class FooExt(FieldExtension):
    async def resolve_async(
        self, next_: AsyncExtensionResolver, source: Any, info: Info, **kwargs: Any
    ) -> Any:
        async for res in next_(source, info, **kwargs):
            yield res + 20

@strawberry.type
class Subscription:
    @strawberry.subscription(extensions=[FooExt()])
    async def subscribe_requires_auth(self) -> AsyncGenerator[int, None]:
        for i in range(5):
            yield i
            await asyncio.sleep(0.01)
bellini666 commented 11 months ago

Hrm, I remember it not being supported, but maybe that changed? =P

Will take a look at the PR you opened

nrbnlulu commented 11 months ago

I think you are right resolve_async must return a coroutine and for subscriptions you need to return an async generator. I'll need to dig dipper to tell.

bellini666 commented 11 months ago

I think you are right resolve_async must return a coroutine and for subscriptions you need to return an async generator. I'll need to dig dipper to tell.

The extension can probably check the "before" permissions (i.e. the global and root permissions), but the "after" ones (i.e. retval) are more complicated because there isn't a single return value for those.