【问题标题】:How to limit query depth in strawberry-graphql (Django implementation)?如何限制草莓-graphql(Django实现)中的查询深度?
【发布时间】:2021-09-06 08:46:52
【问题描述】:

我的草莓-graphql 模式解析器实现中有一个嵌套结构。关于如何限制草莓-graphql(Django 实现)中的查询深度的任何建议?

【问题讨论】:

    标签: django graphql depth


    【解决方案1】:

    这将很快成为 Strawberry 的一项功能: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"
    

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 2020-03-31
      • 2020-05-10
      • 2018-12-09
      • 2018-07-24
      • 2012-11-04
      • 1970-01-01
      • 2011-09-27
      • 2019-04-16
      相关资源
      最近更新 更多