-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
info.context.user is not a real user model #23
Comments
@caesar4321 In order to understand the root of the problem please answer a couple of questions:
|
@prokher I use ordinary
My routing configuration is like this:
|
@prokher When I use the recommended way of login, an error occurs.
The error occurs because I use |
I guess it became an issue because I did not use Or if I call a new User model instance every time I need to return it straightly to graphql query like this:
will there be not a performance issue because I have to additionally access to db to get a creator (a User model instance) instead of just directly creating a new Message model like this: |
@caesar4321 If you have already authenticated thought HTTP by regular Django mechanisms, then when you establish WebSocket connection you do not need to authenticate again. The What is interesting here is where the object returned by your Anyway, I would avoid requesting DB when I do not need to. As a workaround you can extract real Django model from user = getattr(info.context.user, "_wrapped", None) or info.context.user while it shall work for both HTTP and WebSocket, it is definitely a hack which I do not like. Let's understand what is wrong with the Another issue you have touched is that you try to extract something from BTW this is an example of Channels {'client': ['127.0.0.1', 58726],
'cookies': {'csrftoken': 'mpy8zqEviiot1ugLXqJqLY0HT30GM20tMZ6IhLJikMDHkAofn7sNhuFuKLyodbtu',
'sessionid': 't0auyp41awaqo8y90mvkgscwzusdlizf'},
'headers': [(b'upgrade', b'websocket'),
(b'connection', b'Upgrade'),
(b'host', b'127.0.0.1:4242'),
(b'origin', b'http://127.0.0.1:4242'),
(b'sec-websocket-protocol', b'graphql-ws'),
(b'pragma', b'no-cache'),
(b'cache-control', b'no-cache'),
(b'sec-websocket-key', b'3Wvfsqb4vvQYQZtyb1T0jQ=='),
(b'sec-websocket-version', b'13'),
(b'sec-websocket-extensions', b'x-webkit-deflate-frame'),
(b'user-agent',
b'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/'
b'605.1.15 (KHTML, like Gecko) Version/12.1.2 Safari/605.1.15'),
(b'cookie',
b'csrftoken=mpy8zqEviiot1ugLXqJqLY0HT30GM20tMZ6IhLJikMDHkAofn7'
b'sNhuFuKLyodbtu; sessionid=t0auyp41awaqo8y90mvkgscwzusdlizf')],
'path': '/graphql/',
'path_remaining': '',
'query_string': b'',
'server': ['127.0.0.1', 4242],
'session': <django.utils.functional.LazyObject object at 0x109196f50>,
'subprotocols': ['graphql-ws'],
'type': 'websocket',
'url_route': {'args': (), 'kwargs': {}}, |
I think I eventually realized how to handle existent HTTP-based authentication properly. You can simply set class MyGraphqlWsConsumer(channels_graphql_ws.GraphqlWsConsumer):
async def on_connect(self, payload):
self.scope["user"] = await channels.auth.get_user(self.scope) This will properly initialize Added this to the example, please check. Closing this, cause it seems to be addressed. Feel free to reopen if there is still an issue. |
@prokher We are using the recommended way to set the user on scope described above but could notice on production that We've attributed this to Is there anything else we could do instead? I should mention that we are initializing multiple objects on the scope so there is a benefit to doing it once on connection. Thanks! |
is that the right way to solve this? does this solution have some implications? |
UPDATE: (I found this to be easy. Didn't realise this option could be set as boolean on the class attributes) I am using channels 2.4 and wondering if this is possible with newer versions of channels. @SebasWilde, I am facing the same issue when setting the |
Hi! If you had progress that's great! Can you explain what you you fixed and how you did it? |
This has been continued in #75 |
There has been no issue when I query or mutate through grahiql or ordinary HTTP. info.context.user is not a real user model and provokes an issue only when I connect through a websocket using DjangoChannelsGraphqlWs. An error occurs when I return some thing like:
So I had to write like this:
There was also an issue on a mutation when I returned a model connected to info.context.user. For example, this does not work.
It is because I used info.context.user when I made a Message model instance. To make it work, I have to write like this:
Why does info.context.user work differently only when I use websocket? Is there anything I am missing?
What is the right way to return user model right after using info.context.user?
The text was updated successfully, but these errors were encountered: