strawberry-django-plus icon indicating copy to clipboard operation
strawberry-django-plus copied to clipboard

Permission Directives when deploying scema through GraphQLHTTPConsumer

Open m4riok opened this issue 1 year ago • 2 comments

Hi there. Thanks for all the work you are putting into keeping GraphQL alive for django. I have run into the following issue. In order to be able to use subscription with Django and Channels I am currently deploying my schema like so:

gql_http_consumer = TokenAuthMiddlewareStack(GraphQLHTTPConsumer.as_asgi(schema=schema))
gql_ws_consumer = GraphQLWSConsumer.as_asgi(schema=schema)

application = ProtocolTypeRouter({
    "http": URLRouter(
        [
            re_path('^graphql', gql_http_consumer),
            re_path('^',django_asgi_app),
        ]

    ),
    "websocket": TokenAuthMiddlewareStack(
        URLRouter([
            re_path(r'graphql' , gql_ws_consumer),
            #path('chat/', ChatConsumer.as_asgi()),
        ])
    ),
})

My TokenAuthMiddlewareStack retrieves the user and injects it into the scope so it is accessible in resolvers through info.context.request.scope['user']. When attempting to use permission directives in a query like gql.django.field(directives=[IsAuthenticated()]) the following exception is thrown:

Traceback (most recent call last):
File "/home/mariok/webappz/pydev/venvs/myfees/lib/python3.10/site-packages/graphql/execution/execute.py", line 521, in execute_field
result = resolve_fn(source, info, **args)
File "/home/mariok/webappz/pydev/venvs/myfees/lib/python3.10/site-packages/strawberry_django_plus/directives.py", line 119, in resolve
return _next(root, info, *args, **kwargs)
File "/home/mariok/webappz/pydev/venvs/myfees/lib/python3.10/site-packages/strawberry_django_plus/permissions.py", line 254, in resolve
user = cast(UserType, context.request.user)
AttributeError: 'GraphQLHTTPConsumer' object has no attribute 'user'

I am guessing you are expecting a request object there that would be available if the schema was exposed through a view. Is there some other way making a request object available that I am missing here ? Are there plans to support this sort of setup? I saw that @bellini666 is using subscriptions together with this package so I am guessing there is either another way to make subscriptions available or something is missing in my current setup.

m4riok avatar Oct 31 '22 22:10 m4riok