如何使用新添加的 DB table 字段更新 Saleor 的 Graphql 响应?

How to update Saleor's Graphql responses with newly added DB table fields?

首先,使用 GraphQL 的 Saleor 非常棒。就是喜欢。

我们销售的产品具有我们需要从 Graphql 获取的额外元数据。开箱即用,Graphql 查询工作正常,例如:

{
  product (id: "UHJvZHVjdDo3Mg==") {
    id
    name
    description
  }
}

我需要做的是公开来自我的产品 table 的数据以及额外的列,例如 productInfo1、productInfo2 和 productInfo3。这部分当然很简单。

但是,我正在为如何更新 Saleor Graphql 而苦苦挣扎,以便我可以 运行 如下查询:

{
  product (id: "UHJvZHVjdDo3Mg==") {
    id
    name
    description {
      productInfo1
      productInfo2
      productInfo3
    }
  }
}

我浏览了 Saleor 文档、Stack Overflow 和各种博客...我自己尝试了一些合乎逻辑的方法,但没有成功。

我很想在这里开始处理这些类型的更新以满足我们的需求。非常感谢任何指向 "how to" 个位置的建议或链接!

如果您想在描述中添加子字段,您需要做几件事:

  1. 创建包含您想要的子字段的新描述对象类型,例如:
class ProductDescription(graphene.ObjectType):
    productInfo1 = graphene.String()
    productInfo2 = graphene.String()
    productInfo3 = graphene.String()
  1. Product 类型下使用新类型设置 description 字段:
class Product(CountableDjangoObjectType):
    ...
    description = graphene.Field(ProductDescription)
  1. Product 下为 description 添加解析器类型:
def resolve_description(self, info):
    return ProductDescription(
        productInfo1=self.description,
        productInfo2='Some additional info',
        productInfo3='Some more additional info',
    )

Saleor 的 GraphQL API 基于 Graphene framework. You can find more about resolvers and object types here: https://docs.graphene-python.org/en/latest/types/objecttypes/#resolvers.