【问题标题】:How do I send a graphQL query that contains an extended query?如何发送包含扩展查询的 graphQL 查询?
【发布时间】:2023-04-05 23:47:01
【问题描述】:

我的一个服务上有一个 graphene.ObjectType 模型,它扩展了另一个服务中的定义

@extend(fields="id")
class ExtendedProtectedObject(graphene.ObjectType):
    id = external(graphene.ID(required=True))
    additional_color = graphene.String()

    class Meta:
        interfaces = (relay.Node,)

    def resolve_additional_color(self, info):
        return "blue"

    @staticmethod
    def _ExtendedProtectedObject__resolve_reference(root, info):
        # root is an instance of self class with id set to the query id value
        return root

我们运行 Apollo Federation 以将我们所有的 graphql 模式组合在一个位置并路由查询/突变等。 我可以在我的服务中添加什么查询作为单元测试,这反映了我在附加颜色字段上请求时 Apollo 所做的事情? 例如这个查询:

query getExtendedProtectedObject($id: ID!) {
    extendedProtectedObject(id: $id) {
        additionalColor
    }
}

联合将什么查询转发到我的服务? 我想在我的服务中的一个单元测试中使用该查询,因为它会在访问任何字段解析器之前调用 ExtendedProtectedObject 方法 _ExtendedProtectedObject__resolve_reference。

【问题讨论】:

    标签: python-3.x graphql apollo-server graphene-python


    【解决方案1】:

    可以用这个查询来做到这一点:

    query ($representations:[_Any!]!) {
        _entities(representations:$representations) {
            ...on ExtendedProtectedObject {
                additionalColor
            }
        }
    }
    

    并使用这些查询变量:

    variables=dict(
        representations=[{"__typename": "ExtendedProtectedObject", "id": to_global_id("ExtendedProtectedObject", 1)}],
    )
    

    the Apollo docs

    上述查询返回客户端验证错误

    请注意,当我尝试从客户端提交该查询时,Apollo 服务器验证步骤失败。当发送需要解析扩展 ObjectType 中的字段的普通查询时,在后台联邦会将此查询发送到您的服务。

    如果验证关闭 + 查询可以命中服务的安全含义

    因此,如果您关闭了验证,则可以使用此查询将响应定位为仅针对您的一项服务。如果关闭验证,对于像 Me 这样的类型,如果客户端可以成功执行此查询,则这可能是一个安全漏洞。这是因为它允许查询设置外部全局 id 并直接访问服务,绕过 Me get_node 和 Me.resolve_id 的来源,而只访问 Me._Me__resolve_reference 和任何特定于服务的解析器。这假定 id 是唯一的关键字段。

    【讨论】:

      猜你喜欢
      • 2013-01-19
      • 2017-04-28
      • 2020-06-03
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 2017-07-20
      相关资源
      最近更新 更多