GraphiQL 显示一个对象,但请求发回“无法将值强制转换为字符串”

问题描述 投票:0回答:1

我正在使用 Shopify Admin 的 GraphiQL 编辑器 来测试将产品推送到商店的突变。这是突变:

mutation createProduct($input: ProductInput!) {
      productCreate(input: $input) {
        product {
          title
          descriptionHtml
          vendor
          handle
          tags
          variants(first: 50)  {
              nodes  {
                  inventoryItem {
                      tracked
                      sku
                  }
                  price
                  sku
              }
          }
          options { << problem seem to occur here
              name
              position
              values
          }
        }
      }
    }

input
被声明为 ProductInput! 并且
input.options
应该是 ProductOption 的数组,其中包含
name
position
values
等字段。 GraphiQL 编辑器说得很清楚。

当我尝试将任何选项作为对象数组传递时,例如:

...
"options": [
   {
     "name": "Size",
     "position": 1,
     "values": [ "Small", "Medium", "Large" ]
   }
]
...

Shopify API 向我抛出此错误消息:

"message": "Variable $input of type ProductInput! was provided invalid value for options.0 (Could not coerce value {name:\"Taille\",position:1,values:[\"S\",\"M\",\"L\"]} to String)"

我错过了 API 中的某些内容吗?这就是我们过去使用 REST API 输入产品选项的方式,如何在 GraphQL 中获得相同的结果(例如,如果我想要两个选项,

size
color
,具有不同的选择值怎么办)?

文档和实际实施之间是否存在差异?

graphql shopify shopify-api graphiql coerce
1个回答
0
投票

productInput
类型旨在创建单一产品。如果您想在单个突变中创建 >1 个产品,您可以在单个请求中gang您的突变:

mutation createProduct($p1: ProductInput!, $p2: ProductInput!) {
  productCreate(input: $p1) {
    product {
      …product fields  
    }
  }
  productCreate(input: $p2) {
    product {
      …product fields
    }
  }
  …and so on
}
© www.soinside.com 2019 - 2024. All rights reserved.