【问题标题】:How to pass variables to metafieldsSet mutation in Shopify Api Node js Grahql client?如何在 Shopify Api Node js Graphql 客户端中将变量传递给元字段设置突变?
【发布时间】:2021-11-25 16:20:35
【问题描述】:

我试图使用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)
    }

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

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 对象中。

我错过了什么?

谢谢,

霍华德

环境:下一个 js 11.1.2,

依赖:@shopify/shopify-api1.4.1

【问题讨论】:

    标签: node.js graphql shopify


    【解决方案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',
                 },
               ],
           },
       },
    })
    

    参考: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',
    })
    

    【讨论】:

      猜你喜欢
      • 2020-05-05
      • 1970-01-01
      • 2022-09-28
      • 2016-05-02
      • 1970-01-01
      • 2021-06-22
      • 2019-11-19
      • 2020-10-17
      • 2016-10-28
      相关资源
      最近更新 更多