如何发送包含扩展查询的 graphQL 查询?

Posted

技术标签:

【中文标题】如何发送包含扩展查询的 graphQL 查询?【英文标题】:How do I send a graphQL query that contains an extended query? 【发布时间】:2020-11-12 14:38:13 【问题描述】:

我的一个服务上有一个 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。

【问题讨论】:

【参考方案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 是唯一的关键字段。

【讨论】:

以上是关于如何发送包含扩展查询的 graphQL 查询?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中发送带有片段的 graphql 查询

apollo (graphQL) - 如何在查询中发送对象数组

如何将graphql查询的结果作为文件发送?

如何在 graphql 查询中按文件扩展名过滤

如何从命令行向 AWS AppSync 发送 GraphQL 查询?

如何将 GraphQL 查询从 Node.js 发送到 Prisma