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

如何在Shopify Node.js GraphQL客户端中给metafieldsSet mutation传参

问题原因

你对@shopify/shopify-api 1.x版本的GraphQL客户端传参规则理解有误,GraphQL变量需要放在variables属性下,而非你当前使用的query属性。query属性是用来向请求URL追加查询参数的,不会传递给GraphQL语句的变量定义,所以服务端检测到必传的$metafields变量为空,就抛出了对应错误。

修正后代码

const client = new Shopify.Clients.Graphql(
        process.env.SHOP,
        process.env.PASSWORD
    )
    try {
        const metafields = await client.query({
            data: `mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
                metafieldsSet(metafields: $metafields) {
                    userErrors {
                        field
                        message
                    }
                    metafields {
                        key
                        value
                    }
                }
            }           
            `,
            // 把原来的query改成variables即可
            variables: {
                metafields: [
                    {
                        key: 'cb_inventory',
                        namespace: 'my_fields',
                        ownerId: 'gid://shopify/ProductVariant/40576138313890',
                        type: 'number_integer',
                        value: '25',
                    },
                ],
            },
        })
        console.log(metafields)
        res.status(200).json({ values: metafields })
    } catch (error) {
        console.log(error)
        res.status(500).json(error)
    }

额外说明

你当前的元字段配置没有其他问题,number_integer类型要求传字符串格式的数字,你填写的'25'符合格式要求,修改传参键名后即可正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 03:48:03