top16Dev
top16Dev

Reputation: 50

How to update product metafield value by using Graphql?

I'm trying to update product metafield value but I'm not sure why this doesn't work. const

 const session = await Shopify.Utils.loadCurrentSession(
      req,
      res,
      app.get("use-online-tokens")
    );
    const client = new Shopify.Clients.Graphql(session.shop, session.accessToken);
    
    const data = await client.query({
      data: `{
        mutation{
          productUpdate(input:
            {
              id: "gid://shopify/Product/4532387151956",
              metafield:  {
                  namespace: "custom",
                  key: "limit",
                  value : "550",
                  valueType: "INTEGER"
                }
            }) 
            userErrors {
              field
              message
            }
          }
        }
      }`
    });

Please help me with this. Thanks.

Upvotes: 0

Views: 1890

Answers (2)

superDev
superDev

Reputation: 26

I have ever ran into this problem. Because you don't have metafield id like "gid://Metafield/19201930". So you must get this metafield id. And you should type same code with the same code with your new metafield id. Try this.

const data = await client.query({
  data: `mutation {
    productUpdate(input: {
      id: "yourid",
      metafields:[{
        namespace: "custom",
        key: "limit",
        value: "yourvalue"
      }]
    }) {
      product {
        metafield(namespace:"custom", key:"limit") {
          id,
          namespace,
          key,
          value
        },
        id
      }
    }
  }`,
});

await client.query({
  data: `mutation {
    productUpdate(input: {
      id: "your product id",
      metafields:[{
        namespace: "custom",
        key:"limit",
        value:"your value",
        id: "${data.body.data.productUpdate.product.metafield.id}", 
      }]
    }) {
      product {
        metafield(namespace:"custom", key:"limit") {
          namespace
          key
          value
          type
          id
        }
      }
    }
  }`,
});

Upvotes: 1

David Lazar
David Lazar

Reputation: 11427

Modern Shopify GQL got rid of the valueType and replaced it with a simpler type. Try using the correct setup values for your input data like:

type: "integer"

Perhaps that will work better. Based on confusing documentation, Shopify also lists the new types as "number_integer". Yay for their dedication to precision.

Upvotes: 0

Related Questions