You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GraphQL多Mutation合并调用报错求助:解析错误(Shopify场景)

解决GraphQL合并Mutation的解析错误问题

我帮你排查到了这个解析错误的根源——你在合并两个Mutation时,变量声明的格式不符合GraphQL的规范。

错误原因

在你的原始代码里,你在第二个Mutation字段second: inventoryActivate前面又重复写了变量声明($inventoryItemId: ID!, $locationId: ID!, $available: Int),这是GraphQL不允许的:一个Mutation操作的所有变量必须在操作的最开头统一声明一次,操作内的所有字段都共享这些变量,不能在单个字段前单独声明变量。

修正后的GraphQL代码

mutation productVariantUpdateAndInventoryActivate(
  $input: ProductVariantInput!,
  $inventoryItemId: ID!,
  $locationId: ID!,
  $available: Int
) {
  productUpdate: productVariantUpdate(input: $input) {
    product { id }
    productVariant { id price }
    userErrors { field message }
  }
  inventoryUpdate: inventoryActivate(
    inventoryItemId: $inventoryItemId,
    locationId: $locationId,
    available: $available
  ) {
    inventoryLevel { id available }
    userErrors { field message }
  }
}

变量部分(保持你的原有内容即可)

{
  "inventoryItemId": "gid://shopify/InventoryItem/XXXXXXXXXXX",
  "locationId": "gid://shopify/Location/XXXXXXXXXX",
  "available": 11,
  "input": {
    "id": "gid://shopify/ProductVariant/XXXXXXXXX",
    "price": 55
  }
}

关键修改说明

  1. 把所有需要的变量都移到了mutation关键字后的括号里统一声明,确保整个操作的变量是全局共享的
  2. 给两个Mutation字段分别起了别名(productUpdate和inventoryUpdate),这样在返回结果里能更清晰地区分两个操作的响应
  3. 调整了代码的换行和缩进,让结构更易读(这不是必须的,但能帮你更方便地排查格式问题)

这样修改后,就不会再出现你遇到的Parse error on "$" (VAR_SIGN)解析错误了,你可以在Postman里重新测试这个修正后的脚本,验证没问题后再移植到PHP中。

内容的提问来源于stack exchange,提问作者Dave Hamilton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 11:02:43