Product Metafields don't update with GraphiQL

Solved
Arkadi
New Member
2 0 0

Hi there! I am trying to change metafields data for any given product. 

The request succeeds but data isn't updated.

I have attached a photo to illustrate.

image.png

Here's the text version of this code:

mutation {
  productUpdate(input: {
  id: "gid://shopify/Product/8441259336",
    metafields: [
      {
        namespace: "global",
        key: "wish_count",
        value: "550",
      }
    ]
  }) {
    product {
      metafield(namespace: "global", key: "wish_count") {
         id
          namespace
          key
          value   
      }
    }
  }
}
0 Likes
NevinT
Shopify Staff
Shopify Staff
7 1 7

This is an accepted solution.

Hi Arkadi,

 

It looks like the issue you are running into here is that your mutation is missing the valueType parameter in the input. In this case, you will most likely want to set this to "INTEGER".

 

In the future, I recommend that you try running these mutations in the Shopify GraphiQL App and including the userErrors return field to get the most amount of feedback. For your mutation, including the userErrors would have given you the information to help fix the issue. I've included an example below.

 

mutation productUpdate($input: ProductInput!) {
  productUpdate(input: $input) {
    product {
      metafield(namespace: "global", key: "wish_count") {
        id
        namespace
        key
        value
      }
    }
    userErrors {
      field
      message
    }
  }
}

 

Variables:

 

 

{
  "input": {
  	"id": "gid://shopify/Product/8441259336",
    "metafields":  {
        "namespace": "global",
        "key": "wish_count",
        "value": "550",
        "valueType": "INTEGER"
      }
  }
}

 

If you run into any other issues, please reach out!

 

NevinT | Developer @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes
rchenx
New Member
2 0 0

Thanks ntan

Tried it too, still does not work.

we get similar response even when adding the valyeType as you suggested

meaybe its a matter of permissions? the get request works well but we cannot update, create or delete

 

and this is the reply we get

{
    "data": {
        "productUpdate": {
            "product": {
                "metafield": {
                    "id""gid://shopify/Metafield/11975849934931",
                    "namespace""global",
                    "key""wish_count",
                    "value""549"
                }
            },
            "userErrors": [
                {
                    "field": [
                        "metafields",
                        "0",
                        "key"
                    ],
                    "message""Key must be unique within this namespace on this resource"
                }
            ]
        }
    },

 

0 Likes
NevinT
Shopify Staff
Shopify Staff
7 1 7

Hi rchenx,

 

I realize now that what you're trying to do is update the value of an existing metafield on a product, which cannot actually be done with the mutation we are using here. Instead, we should be using the privateMetafieldUpsert mutation instead. Take a look at the tutorial I have linked here. That should give you the information you need to update the product.

 

I recognize that this is a bit confusing and not very intuitive, so I will speak to the team here about what we can do to make the documentation clearer on how to handle this use case. Please let me know if you run into any other issues.

NevinT | Developer @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes
rchenx
New Member
2 0 0

Hello ntan,

We are interested in changing PUBLIC metafield and not private metafield becouse we need the shopify liquid code to utize those values

how that should be addressed?

Best

Ronen

0 Likes
Arkadi
New Member
2 0 0

I would also like to add that in this tutorial:

https://shopify.dev/tutorials/manage-metafields-with-graphql-admin-api

updating metafields only exists for private metafields.

How about the public ones too? we need to access those fields in shopify liquid... We can't use private metafields.

the POST of new public metafields works, but we need to UPDATE as well. When we try to do this with the public metafields this is thew error returned:

"Key must be unique within this namespace on this resource".

 

 

0 Likes
NevinT
Shopify Staff
Shopify Staff
7 1 7

Hi folks,

 

My mistake for suggesting the privateMetafieldUpsert mutation. You are correct in that it is the wrong thing to be using here. Instead, we should be using the productUpdate mutation as you originally thought, except instead of passing in the key as the identifier, we should use the actual id of the metafield itself. I have included a sample of what the mutation should look like below:

 

mutation productUpdate($input: ProductInput!) {
  productUpdate(input: $input) {
    product {
      metafield(namespace: "global", key: "wish_count") {
        id
        namespace
        key
        value
      }
    }
    userErrors {
      field
      message
    }
  }

Variables:

 

 

{
  "input": {
     "id": "gid://shopify/Product/8441259336",
        "metafields":  {
           "id": "gid://shopify/Metafield/11975849934931",
           "value": "550",
           "valueType": "INTEGER"
     }
  }
}

 

Please let me know if this works for you.

 

NevinT | Developer @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

bzbzh
New Member
5 0 0

I get a "ProductUpdate access denied" when trying your method.

Is it possible to update public product metafield?

0 Likes
Greg_Kujawa
Shopify Partner
965 80 204

Access denied typically indicates that your API credentials don't have the access scope permissions to perform the operation. If you edit the app permissions in the Shopify web admin and reauth your app then it should work. 

0 Likes
bzbzh
New Member
5 0 0

I got this error using the Shopify GraphiQL App from my shop page.

(At the end I don't mean to use it through an app but directly in a lambda function)

 

0 Likes