Yiu*_*ard 5 node.js shopify graphql
我试图使用metafieldsSet突变来更新元字段,Shopify代码如下:
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)
}
Run Code Online (Sandbox Code Playgroud)
但是,上述突变会返回以下错误:
Expected value to not be null
Variable $metafields of type [MetafieldsSetInput!]! was provided invalid value
Run Code Online (Sandbox Code Playgroud)
我假设变量metafields未能传递到突变中,因为当我在 中运行完全相同的突变时Shopify Admin API GraphiQL explorer,没有错误
Shopify Admin API GraphiQL explorer 突变结果
我还研究了@shopify/shopify-api 的github 存储库。在我的理解中,变量被添加到query对象中。
我缺少什么?
谢谢,
霍华德
环境:Next js 11.1.2,
依赖项:@shopify/shopify-api1.4.1
结果发现语法不正确。变量应该放在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',
},
],
},
},
})
Run Code Online (Sandbox Code Playgroud)
参考: https: //github.com/Shopify/shopify-node-api/blob/main/src/clients/graphql/test/graphql_client.test.ts
要使用预期的 API 版本,您需要首先使用以下代码初始化上下文:
Shopify.Context.initialize({
API_KEY,
API_SECRET_KEY,
SCOPES: ['read_products', 'write_products'],
HOST_NAME: HOST,
API_VERSION: '2021-10',
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1741 次 |
| 最近记录: |