首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将变量传递给Shopify Api Node js Grahql客户端中的metafieldsSet突变?

如何将变量传递给Shopify Api Node js Grahql客户端中的metafieldsSet突变?
EN

Stack Overflow用户
提问于 2021-10-05 11:26:06
回答 1查看 156关注 0票数 2

我尝试使用以下代码使用metafieldsSet变异来更新Shopify中的metafield:

代码语言:javascript
复制
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)
    }

但是,上面的突变返回以下错误:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-05 16:52:39

原来语法是不正确的。变量应该放在variables对象中,而突变语句应该放在query对象中。

下面的代码现在可以工作了:

代码语言:javascript
复制
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',
             },
           ],
       },
   },
})

参考:https://github.com/Shopify/shopify-node-api/blob/main/src/clients/graphql/test/graphql_client.test.ts

要使用预期的API版本,您需要首先使用以下代码初始化上下文:

代码语言:javascript
复制
Shopify.Context.initialize({
    API_KEY,
    API_SECRET_KEY,
    SCOPES: ['read_products', 'write_products'],
    HOST_NAME: HOST,
    API_VERSION: '2021-10',
})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69449574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档