Howtographql: graphql-python authentication issue

Created on 29 Apr 2019  路  4Comments  路  Source: howtographql/howtographql

Hello,

I was going through the graphql-python Authentication part and I got an error when I reached the "Configuring django-graphql-jwt" part.

Internal Server Error: /graphql/
Traceback (most recent call last):
  File "/Users/***/django-gql/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/***/django-gql/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/***/django-gql/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/***/django-gql/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/***/django-gql/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 62, in view
    self = cls(**initkwargs)
  File "/Users/***/django-gql/venv/lib/python3.7/site-packages/graphene_django/views.py", line 88, in __init__
    self.middleware = list(instantiate_middleware(middleware))
  File "/Users/***/django-gql/venv/lib/python3.7/site-packages/graphene_django/views.py", line 48, in instantiate_middleware
    yield middleware()
TypeError: __init__() missing 1 required positional argument: 'get_response'

The way I was able to get the demo working was to change "MIDDLEWARE" to "MIDDLEWARES" in file "hackernews/hackernews/settings.py":

GRAPHENE = {
    'SCHEMA': 'hackernews.schema.schema',
    'MIDDLEWARES': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}

Let me know if you want me to make a PR.

Cheers,
Stan

Most helpful comment

In settings.py

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'graphql_jwt.middleware.JSONWebTokenMiddleware', ### <---Add this line
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

All 4 comments

Thanks for the fix! I ran into the same issue, too, and adding the "S" to "MIDDLEWARE" helps.

I think this is due to the old version of django_graphql_jwt package that the tutorial use. I updated to the latest version and both 'MIDDLEWARES' and 'MIDDLEWARE' in the settings work just fine. The recommended setting in their documentation suggests "MIDDLEWARE" though.

This fix will remove the TypeError but when you try to authenticate using jwt authentication, it will always raise "not logged in" Exception.

The correct solution would be to put "graphql_jwt.middleware.JSONWebTokenMiddleware" after "django.contrib.auth.middleware.AuthenticationMiddleware" in django middleware settings.

In settings.py

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'graphql_jwt.middleware.JSONWebTokenMiddleware', ### <---Add this line
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

@sthristov You should definitely create a pull request with that fix.

It seems though, that this project is currently in trouble with finding maintainers.

Was this page helpful?
0 / 5 - 0 ratings