使用graphql Playground来测试Sale或productCreate,添加产品描述的正确语法是什么?

发布于 2025-01-15 09:12:52 字数 792 浏览 4 评论 0原文

在下面的代码示例中,如果我排除描述字段,则产品创建成功。描述字段到位后,我收到 GraphQL 错误。

代码:

    productCreate(
    input: {
      category: "Q2F0ZWdvcnk6MQ==", # Category ID
      name: "Delete Me!", # Product name
      productType: "UHJvZHVjdFR5cGU6MQ==", # Product Type ID
      chargeTaxes: true,
      weight: "0.3", # in Kg
      rating: 5,
      description: {text:"some text"}, # nope
    }
  )

错误:

graphql.error.base.GraphQLError: Argument \"input\" has invalid value {category: \"Q2F0ZWdvcnk6MQ==\", name: \"Delete Me!\", productType: \"UHJvZHVjdFR5cGU6MQ==\", chargeTaxes: true, weight: \"0.3\", rating: 5, description: {text: \"some text\"}}.",
              "In field \"description\": Expected type \"JSONString\", found {text: \"some text\"}."

In the code example below, if I exclude the description field the product is created successfully. With the description field in place I get a GraphQL error.

The code:

    productCreate(
    input: {
      category: "Q2F0ZWdvcnk6MQ==", # Category ID
      name: "Delete Me!", # Product name
      productType: "UHJvZHVjdFR5cGU6MQ==", # Product Type ID
      chargeTaxes: true,
      weight: "0.3", # in Kg
      rating: 5,
      description: {text:"some text"}, # nope
    }
  )

The error:

graphql.error.base.GraphQLError: Argument \"input\" has invalid value {category: \"Q2F0ZWdvcnk6MQ==\", name: \"Delete Me!\", productType: \"UHJvZHVjdFR5cGU6MQ==\", chargeTaxes: true, weight: \"0.3\", rating: 5, description: {text: \"some text\"}}.",
              "In field \"description\": Expected type \"JSONString\", found {text: \"some text\"}."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉黛浅 2025-01-22 09:12:52

整理描述语法并不简单。从我这里的问题:

Saleor on Github

我得到了这个答案:

{
  "id": "UHJvZHVjdDo3Mg==",
  "description": "{\"blocks\":[{\"type\":\"paragraph\",\"data\":{\"text\":\"New description\"}}]}"
}

然后我像这样实现了this:

    query = gql(
        """
        mutation (
            $slug: String!,
            $product_title: String!,
            $description: JSONString!,
            $weight_grams: WeightScalar!,
            )
            {
            productCreate(
                input:{
                    category: "Q2F0ZWdvcnk6NQ==",
                    name: $product_title,
                    productType: "UHJvZHVjdFR5cGU6MQ==",
                    slug: $slug,
                    description: $description,
                    weight: $weight_grams,
                }
            )
            {
                errors {
                    field
                    message
                }
                product {
                    id
                    name
                    productType {
                        id
                    }
                    slug
                }
            }
        }
        """
    )
    params = {
        "product_title": str(product_title),
        "description": '{"blocks":[{"type":"paragraph","data":{"text":"'
        + str(product_title + " (" + csv_product_id + ")")
        + '"}}]}',
        "slug": str(csv_sku_code),
        "weight_grams": str(weight_grams),
    }
    result = client.execute(query, variable_values=params)

这对我们来说效果很好。

Sorting out the description syntax wasn't straightforward. From my question here:

Saleor on Github

I got this answer:

{
  "id": "UHJvZHVjdDo3Mg==",
  "description": "{\"blocks\":[{\"type\":\"paragraph\",\"data\":{\"text\":\"New description\"}}]}"
}

which I then implemented like this:

    query = gql(
        """
        mutation (
            $slug: String!,
            $product_title: String!,
            $description: JSONString!,
            $weight_grams: WeightScalar!,
            )
            {
            productCreate(
                input:{
                    category: "Q2F0ZWdvcnk6NQ==",
                    name: $product_title,
                    productType: "UHJvZHVjdFR5cGU6MQ==",
                    slug: $slug,
                    description: $description,
                    weight: $weight_grams,
                }
            )
            {
                errors {
                    field
                    message
                }
                product {
                    id
                    name
                    productType {
                        id
                    }
                    slug
                }
            }
        }
        """
    )
    params = {
        "product_title": str(product_title),
        "description": '{"blocks":[{"type":"paragraph","data":{"text":"'
        + str(product_title + " (" + csv_product_id + ")")
        + '"}}]}',
        "slug": str(csv_sku_code),
        "weight_grams": str(weight_grams),
    }
    result = client.execute(query, variable_values=params)

This works well for us.

小嗲 2025-01-22 09:12:52

它是一个字符串,对于富文本,它使用 https://editorjs.io/

您可以检查网络选项卡在仪表板中了解 API 的使用方式

It is a string, for rich text it is using https://editorjs.io/

You can inspect the network tab in the dashboard to learn how APIs are being used

后eg是否自 2025-01-22 09:12:52

JSON字符串意味着提供转换为字符串的JSON文本。这可以通过转义 JSON 中的引号来实现。

例如,

{ "text": "some text" }

可以将此 JSON 转换为字符串,如下所示:

"{\"text\":\"sometext\"}"

正如您所注意到的,封装在引号内的文本是一个有效的字符串。

您可以使用 https://jsontostring.com/ 进行转换

您的最终代码应如下所示:

mutation {
  productCreate(
    input: {
      category: "Q2F0ZWdvcnk6MQ==" # Category ID
      name: "Delete Me!" # Product name
      productType: "UHJvZHVjdFR5cGU6MQ==" # Product Type ID
      chargeTaxes: true
      weight: "0.3" # in Kg
      rating: 5
      description: "{\"text\":\"sometext\"}" # nope
    }
  ){
    product{
      id
    }
  }
}

JSON string means providing a JSON text converted to a string. This can be achieved by escaping quotation marks within the JSON.

For example, this JSON

{ "text": "some text" }

can be converted to String as below:

"{\"text\":\"sometext\"}"

As you notice that the text encapsulated inside quotation marks, to be a valid String.

You can use https://jsontostring.com/ for the conversion

Your final code should be like this:

mutation {
  productCreate(
    input: {
      category: "Q2F0ZWdvcnk6MQ==" # Category ID
      name: "Delete Me!" # Product name
      productType: "UHJvZHVjdFR5cGU6MQ==" # Product Type ID
      chargeTaxes: true
      weight: "0.3" # in Kg
      rating: 5
      description: "{\"text\":\"sometext\"}" # nope
    }
  ){
    product{
      id
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文