首页 > 解决方案 > 如何在 Shopify 中使用 GraphQL 更新 Shop Metafields

问题描述

如何在 shopify 中使用 GraphQL 创建/更新商店元字段?我可以使用以下内容轻松创建集合元字段:

mutation {
  collectionUpdate(
    input: {
      id: "gid"
      metafields: [
        {
          namespace: "testNamespace"
          key: "testKey"
          value: "someValue2"
          valueType: STRING
        }
        {
          id: "gid"
          value: "value"
        }
      ]
    }
  ) {
    collection {
      id
      metafields(first: 10) {
        edges {
          node {
            id
            value
          }
        }
      }
    }
    userErrors {
        field
        message
      }
  }
}

但是这家商店似乎没有等价物。此外,可以使用以下方法检索商店元字段:

{
  shop {
    metafield(namespace:"namespace",key:"key") {
          id
          value
        }
  }
}

标签: graphqlshopifyshopify-api

解决方案


要更新任何元字段,您可以使用突变“ metafieldsSet

mutation setMetafield($metafields: [MetafieldsSetInput!]!) {
  metafieldsSet(metafields:$metafields){
    metafields{
      id
      key
      value
    }
    userErrors{
      code
      message
    }
  }
}

MetafieldsSetInput需要您可以从商店查询中获取的所有者 ID

query shopDetails{
  shop{
    id
  }
}

推荐阅读