datadvance / DjangoChannelsGraphqlWs

Django Channels based WebSocket GraphQL server with Graphene-like subscriptions
MIT License
281 stars 84 forks source link

Cannot Login User The Way The Docs Advise #76

Closed rossm6 closed 3 years ago

rossm6 commented 3 years ago

I'm trying to login in my user but getting an error - see traceback below.

class SignIn(DjangoFormMutation):
    profile = graphene.Field(ProfileNode)

    @classmethod
    def get_form_kwargs(cls, root, info, **input):
        kwargs = super().get_form_kwargs(root, info, **input)
        kwargs["request"] = info.context
        return kwargs

    class Meta:
        form_class = AuthenticationForm
        # username is in fact email for user
        fields = ('username', 'password',)

    @classmethod
    def perform_mutate(cls, form, info):
        # form is valid
        user = form.get_user()
        profile = user.profile
        async_to_sync(channels.auth.login)(info.context.__dict__, user)
        info.context.session.save()
        # change_session_and_login(info.context, user)
        return cls(errors=[], profile=profile)

Notice I'm using dict because the _asdict is not on the context object like the docs suggest (incidentally, why is this?)

Anyway this is the traceback -

Traceback (most recent call last):
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/promise/promise.py", line 489, in _resolve_from_executor
    executor(resolve, reject)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/promise/promise.py", line 756, in executor
    return resolve(f(*args, **kwargs))
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
    return next(*args, **kwargs)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/graphene/relay/mutation.py", line 70, in mutate
    result = cls.mutate_and_get_payload(root, info, **input)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/graphene_django/forms/mutation.py", line 51, in mutate_and_get_payload
    return cls.perform_mutate(form, info)
  File "/home/ross/Desktop/web_dev/projects/chat/user/schema.py", line 219, in perform_mutate
    async_to_sync(channels.auth.login)(info.context.__dict__, user)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/sync.py", line 223, in __call__
    return call_result.result()
  File "/usr/lib/python3.8/concurrent/futures/_base.py", line 437, in result
    return self.__get_result()
  File "/usr/lib/python3.8/concurrent/futures/_base.py", line 389, in __get_result
    raise self._exception
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/sync.py", line 292, in main_wrap
    result = await self.awaitable(*args, **kwargs)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/sync.py", line 444, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "/usr/lib/python3.8/asyncio/tasks.py", line 455, in wait_for
    return await fut
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/current_thread_executor.py", line 22, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/channels/db.py", line 13, in thread_handler
    return super().thread_handler(loop, *args, **kwargs)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/asgiref/sync.py", line 486, in thread_handler
    return func(*args, **kwargs)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/channels/auth.py", line 93, in login
    session.cycle_key()
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py", line 344, in cycle_key
    self.create()
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/contrib/sessions/backends/db.py", line 51, in create
    self._session_key = self._get_new_session_key()
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py", line 196, in _get_new_session_key
    if not self.exists(session_key):
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/contrib/sessions/backends/db.py", line 47, in exists
    return self.model.objects.filter(session_key=session_key).exists()
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/models/query.py", line 808, in exists
    return self.query.has_results(using=self.db)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/models/sql/query.py", line 552, in has_results
    return compiler.has_results()
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1145, in has_results
    return bool(self.execute_sql(SINGLE))
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1173, in execute_sql
    cursor = self.connection.cursor()
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/graphene_django/debug/sql/tracking.py", line 41, in cursor
    return state.Wrapper(connection._graphene_cursor(), connection, panel)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/backends/base/base.py", line 237, in _cursor
    return self._prepare_cursor(self.create_cursor(name))
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/backends/base/base.py", line 237, in _cursor
    return self._prepare_cursor(self.create_cursor(name))
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/ross/.local/share/virtualenvs/chat-A0fEktPe/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 236, in create_cursor
    cursor = self.connection.cursor()
graphql.error.located_error.GraphQLLocatedError: connection already closed
rossm6 commented 3 years ago

Based on a misunderstanding (I think). I was making a http request and this example assumes a websocket request.

Anthony-J-Garot commented 3 years ago

I found this post because I ran into the same exact error condition.

I, too, used dict instead of _asdict().

Unit tests worked fine with SQLite3, but failed when I migrated to PosgresQL.

My solution is to just pull all this code out and rely only upon graphql_jwt for authentication.