/ graphql的RecursionError。超过最大递归深度

时间:2020-06-10 15:31:59

标签: python django graphql graphene-django django-graphql-jwt

问题定义:

我已经阅读了有关使用this official link中的graphene-jwt包向项目添加JWT身份验证的文章。根据本教程中所说的,应该对项目settings.py文件进行一些更改:

  1. 首先应安装软件包:
pip3 install django-graphql-jwt
  1. 将AuthenticationMiddleware添加到MIDDLEWARE变量中。
MIDDLEWARE = [
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    ...
]
  1. 添加JSONWebTokenBackend后端AUTHENTICATION_BACKENDS变量:
AUTHENTICATION_BACKENDS = [
    'graphql_jwt.backends.JSONWebTokenBackend',
    'django.contrib.auth.backends.ModelBackend',
]
  1. 将JSONWebTokenMiddleware添加到GRAPEHENE变量中。
GRAPHENE = {
    'SCHEMA': 'apps.schema.schema.schema', # Where your Graphene schema lives
    'MIDDLEWARE': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}

执行第四步后,对localhost:8000/graphql的每个请求都具有以下堆栈跟踪:

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:

项目结构:

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

截屏: grpahql maximum recursion depth exceeded screenshot

我不知道这是错误还是我的错。 (在添加上述配置之前,该API可以正常工作,并且已经进行了很多次检查)我已经在互联网上进行搜索,但是没有找到正确的答案。如果有人能帮助我,我将不胜感激。

PS1:与我的最著名的问题是this one,主要讨论python中的最大递归深度错误。但我无法理解此错误的原因。

PS2:我还向settings.py添加了以下几行,但仍然无法正常工作!

import sys
sys.setrecursionlimit(10000)

1 个答案:

答案 0 :(得分:1)

看看这个问题: https://github.com/joke2k/django-environ/issues/60

我认为您的密钥以$开头,这就是问题所在。