pythondjangographqlgraphene-djangodjango-graphql-jwt

RecursionError at /graphql. maximum recursion depth exceeded


Problem Definition:

I have read the article about adding JWT authentication my project using graphene-jwt package from this official link. According to what is said in the tutorial, some changes should be applied to the project settings.py file:

  1. First of all the package should be installed:
pip3 install django-graphql-jwt
  1. Add AuthenticationMiddleware to MIDDLEWARE variable.
MIDDLEWARE = [
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    ...
]
  1. Adding JSONWebTokenBackend backend AUTHENTICATION_BACKENDS variable:
AUTHENTICATION_BACKENDS = [
    'graphql_jwt.backends.JSONWebTokenBackend',
    'django.contrib.auth.backends.ModelBackend',
]
  1. Add JSONWebTokenMiddleware to GRAPEHENE variable.
GRAPHENE = {
    'SCHEMA': 'apps.schema.schema.schema', # Where your Graphene schema lives
    'MIDDLEWARE': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}

After doing the 4th step, every request to localhost:8000/graphql has the following stacktrace:

errorInternal Server Error: /graphql
Traceback (most recent call last):
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/django/views/generic/base.py", line 62, in view
    self = cls(**initkwargs)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphene_django/views.py", line 78, in __init__
    schema = graphene_settings.SCHEMA
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphene_django/settings.py", line 116, in __getattr__
    val = perform_import(val, attr)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphene_django/settings.py", line 55, in perform_import
    return import_from_string(val, setting_name)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphene_django/settings.py", line 69, in import_from_string
    module = importlib.import_module(module_path)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/mostafa/Desktop/Work/OOD_Project/back/transportation/apps/schema/schema.py", line 2, in <module>
    import graphql_jwt
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphql_jwt/__init__.py", line 1, in <module>
    from . import relay
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphql_jwt/relay.py", line 3, in <module>
    from . import mixins
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphql_jwt/mixins.py", line 10, in <module>
    from . import exceptions, settings
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphql_jwt/settings.py", line 26, in <module>
    JWT_SECRET_KEY = env('JWT_SECRET_KEY', default=settings.SECRET_KEY)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 123, in __call__
    return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 284, in get_value
    value = self.get_value(value, cast=cast, default=default)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 284, in get_value
    value = self.get_value(value, cast=cast, default=default)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 284, in get_value
    value = self.get_value(value, cast=cast, default=default)
  [Previous line repeated 866 more times]
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 273, in get_value
    value = self.ENVIRON[var]
  File "/usr/lib/python3.6/os.py", line 666, in __getitem__
    value = self._data[self.encodekey(key)]
RecursionError: maximum recursion depth exceeded:

Project Structure:

├── apps
│   └── orders
│       ├── ... other-files
│       ├── schema.py
│   └── schema
│       └── schema.py
│   └── users
│       ├── ... other-files
│       └── schema.py

Screenshot: grpahql maximum recursion depth exceeded screenshot

I don't know whether it is a bug or my fault. (The API worked perfectly before adding above configurations and it has been checked many times ) I have searched on internet but didn't find proper answer to my question . I would be thankful if anyone could help me.

PS1: The most famous question related to mine is this one which mainly talks about the maximum recursion depth error in python. But I can't understand the causation of this error here.

PS2: I have also add the following lines to settings.py but it still doesn't work!

import sys
sys.setrecursionlimit(10000)

Solution

  • Take a look at this issue: https://github.com/joke2k/django-environ/issues/60

    I think your secret key starts with $ and that's causing the problem.