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

使用Node.js GraphQL调用productCreate时出现productTaxonomyNodeId无效ID错误

解决Shopify GraphQL API创建产品时的invalid id错误(productCategory字段问题)

问题背景

使用Shopify API的GraphQL接口创建产品时,添加productCategory字段后触发invalid id错误;移除该字段后,产品可正常在后台创建。当前代码使用2023-04版本API,分类ID取自2024-07版本的产品分类库。

解决方案

1. 对齐API版本与产品分类库版本

Shopify的产品分类节点ID会随API版本迭代更新,跨版本使用会导致ID不兼容。你当前使用的API版本是2023-04(April23),但分类ID取自2024-07版本的分类库,这是核心矛盾。

修复方式二选一:

  • 升级API版本到2024-07,与分类库版本匹配:
    修改代码中的API配置:
    apiVersion: ApiVersion.July24,
    // 同步更新restResources到对应版本
    import { restResources } from "@shopify/shopify-api/rest/admin/2024-07";
    
  • 改用2023-04版本对应的产品分类ID,从Shopify官方对应版本的分类库中获取有效ID。

2. 修正分类ID格式

你使用的gid://shopify/TaxonomyCategory/fr格式错误,Shopify分类节点ID并非国家代码(如fr),而是每个分类对应的唯一数字ID,正确格式应为gid://shopify/TaxonomyCategory/100001这类形式。

验证有效ID的方法:
先通过GraphQL查询获取店铺可用的分类节点列表,提取有效ID:

const queryData = await shopifyClient.query({
  data: `{
    productTaxonomyNodes(first: 10) {
      edges {
        node {
          id
          name
        }
      }
    }
  }`,
});
console.log(queryData.body.data.productTaxonomyNodes.edges);

将返回结果中的有效ID替换到productCategory字段中。

3. 确认分类节点适配店铺

部分分类节点仅对特定地区或店铺类型开放,需确保所选分类节点在你的店铺所在地区可用。

修复后代码示例

import "@shopify/shopify-api/adapters/node";
import { shopifyApi, ApiVersion, Session } from "@shopify/shopify-api";
import { restResources } from "@shopify/shopify-api/rest/admin/2024-07";

const shopify = shopifyApi({
    apiSecretKey: Shopify_App_API_secret_key,
    apiVersion: ApiVersion.July24,
    isCustomStoreApp: true,
    adminApiAccessToken: Shopify_Admin_API_Access_Token,
    isEmbeddedApp: false,
    hostName: Shopify_Host_Name,
    restResources,
});

const shopifySession = shopify.session.customAppSession(Shopify_Host_Name);
const shopifyClient = new shopify.clients.Graphql({session:shopifySession});

// 替换为查询到的有效分类ID
const data = await shopifyClient.query({
    data: `mutation {
        productCreate(input: {
            handle:  "test-product",            
            title: "Sweet new product",
            descriptionHtml: "test",
            vendor: "JadedPixel",           
            productCategory: {
                productTaxonomyNodeId: "gid://shopify/TaxonomyCategory/100001"
            },
            productType: "Snowboard",
            tags: "new",
            status: DRAFT,
            templateSuffix: "ltl-shipping"
            }) {
        product {
            id  
            }
        }
    }`,
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 20:03:24