django - 如何限制草莓-graphql(Django实现)中的查询深度?
问题描述
我的草莓-graphql 模式解析器实现中有一个嵌套结构。关于如何限制草莓-graphql(Django 实现)中的查询深度的任何建议?
解决方案
这将很快成为草莓中的一个功能:https ://github.com/strawberry-graphql/strawberry/pull/1021
您将能够定义一个可以传递给execute
命令的验证器:
import strawberry
from strawberry.schema import default_validation_rules
from strawberry.tools import depth_limit_validator
# Add the depth limit validator to the list of default validation rules
validation_rules = (
default_validation_rules + [depth_limit_validator(3)]
)
# assuming you already have a schema
result = schema.execute_sync(
"""
query MyQuery {
user {
pets {
owner {
pets {
name
}
}
}
}
}
""",
validation_rules=validation_rules,
)
)
assert len(result.errors) == 1
assert result.errors[0].message == "'MyQuery' exceeds maximum operation depth of 3"
推荐阅读
- angular - Angular - FormGroup 上的验证器会破坏 FormControl 上的单个验证器
- github - Github:如何在合并时更新拉取请求失败
- pyspark - 如何在数据框列中获取下一个更高的日期列值
- python - 读取带有熊猫条件的文本文件
- symfony - 如何获取定义为handleRequest方法使用的闭包函数的表单验证组?
- microsoft-graph-api - BadRequest:无法执行 Skype 后端请求 GetThreadS2SRequest
- json - 如何在包含接口类型集合的类上应用 JSON 补丁文档
- python - 插入新行并重新启动获取代码时如何从 SQL Server 数据库中获取数据?
- ios - SwiftUI:如何在搜索栏的文本更改时触发 api 调用以检索数据源
- angular - 由于异常,Angular 防护未重定向到特定页面