Проверка глубины graphene-django v2.0
Есть ли способ проверить глубину запроса с помощью graphene-django
2.0?
Я пытался использовать этот предложенный backend:
from graphql.backend.core import GraphQLCoreBackend
def measure_depth(selection_set, level=1):
max_depth = level
for field in selection_set.selections:
if field.selection_set:
new_depth = measure_depth(field.selection_set, level=level + 1)
if new_depth > max_depth:
max_depth = new_depth
return max_depth
class DepthAnalysisBackend(GraphQLCoreBackend):
def document_from_string(self, schema, document_string):
document = super().document_from_string(schema, document_string)
ast = document.document_ast
for definition in ast.definitions:
# We are only interested in queries
if definition.operation != 'query':
continue
depth = measure_depth(definition.selection_set)
if depth > 3: # set your depth max here
raise Exception('Query is too complex')
return document
url(r'^api', csrf_exempt(FileUploadGraphQLView.as_view(
schema=schema, middleware=(middleware), backend=DepthAnalysisBackend()))),
но возвращает ошибку FileUploadGraphQLView() received an invalid keyword 'backend'. as_view only accepts arguments that are already attributes of the class.