我尝试使用以下代码使用metafieldsSet变异来更新Shopify中的metafield:
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: {
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)
}但是,上面的突变返回以下错误:
Expected value to not be null
Variable $metafields of type [MetafieldsSetInput!]! was provided invalid value我假设变量metafields没有传递到突变中,因为当我在Shopify Admin API GraphiQL explorer中运行完全相同的突变时,没有错误Shopify Admin API GraphiQL explorer mutation result
我还研究了@shopify/shopify-api的github repo。在我看来,变量是添加到query对象中的。
我遗漏了什么?
谢谢,
霍华德
环境: Next js 11.1.2,
依赖项:@shopify/shopify-api 1.4.1
发布于 2021-10-05 16:52:39
原来语法是不正确的。变量应该放在variables对象中,而突变语句应该放在query对象中。
下面的代码现在可以工作了:
const metafields = await client.query({
data: {
query: `mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
userErrors {
field
message
}
metafields {
key
value
}
}
}`,
variables: {
metafields: [
{
key: 'cb_inventory',
namespace: 'my_fields',
ownerId: 'gid://shopify/ProductVariant/40576138313890',
type: 'number_integer',
value: '25',
},
],
},
},
})要使用预期的API版本,您需要首先使用以下代码初始化上下文:
Shopify.Context.initialize({
API_KEY,
API_SECRET_KEY,
SCOPES: ['read_products', 'write_products'],
HOST_NAME: HOST,
API_VERSION: '2021-10',
})https://stackoverflow.com/questions/69449574
复制相似问题