Re: Product Metafields don't update with GraphiQL

Solved

Product Metafields don't update with GraphiQL

Arkadi
Tourist
4 0 4

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   
      }
    }
  }
}
Accepted Solution (1)

NevinT
Shopify Staff (Retired)
7 1 11

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!

 

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 19 (19)

NevinT
Shopify Staff (Retired)
7 1 11

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!

 

To learn more visit the Shopify Help Center or the Community Blog.

rchenx
Visitor
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"
                }
            ]
        }
    },

 

NevinT
Shopify Staff (Retired)
7 1 11

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.

To learn more visit the Shopify Help Center or the Community Blog.

rchenx
Visitor
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

Arkadi
Tourist
4 0 4

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".

 

 

NevinT
Shopify Staff (Retired)
7 1 11

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.

 

To learn more visit the Shopify Help Center or the Community 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?

Gregarican
Shopify Partner
1033 86 292

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. 

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)

 

Gregarican
Shopify Partner
1033 86 292

So if you go into the Shopify web admin for this shop and go into your logged-in user account. See what permissions are listed there. If you are using the Shopify GraphiQL app then the permissions flow from the logged-in user. 

bzbzh
New Member
5 0 0

It's the main root account.

Gregarican
Shopify Partner
1033 86 292

Mind providing a full screen shot of the GraphiQL app query that's failing? 

bzbzh
New Member
5 0 0

Capture d’écran 2020-11-28 à 2.10.00 PM.jpg

Gregarican
Shopify Partner
1033 86 292

I just ran this for a ProductVariantUpdate using the GraphiQL app in my test store. It worked fine. See below. I didn't have an exiting product with a defined metafield so it was against a variant child, but seemed to go okay. And you've verified that this particular Shopify account has the appropriate permissions? I know you said that it's the root account, but double-checking might not hurt!

 

Untitled.jpg 

bzbzh
New Member
5 0 0

Well, you were right! Thx for insisting. 🙂

I wasn't able to review full rights of the application (or at least they were misleading) but eventually I reinstalled it and it worked.

Side note: it seems to me that the example here: https://shopify.dev/tutorials/manage-metafields-with-graphql-admin-api#updating-the-owning-resource

Lack with the field

"id": "gid://shopify/Metafield/XXX",         

 

Otherwise you get a:  "message": "Key must be unique within this namespace on this resource"

superstoredev
Shopify Partner
3 0 0

in the latest api version namespace and key fields are mandatory, so I continue to get 

message""Key must be unique within this namespace on this resource"
. Unable to update public metafield using productUpdate mutant.
{
  "query""mutation productUpdate($input: ProductInput!) {  productUpdate(input: $input) {    product {      id handle    }    userErrors {      field      message    }  }}",
  "variables": {
    "input": {
      "id""gid://shopify/Product/XXXXX967638075",
       "metafields"
        {
         "id""gid://shopify/Metafield/XXXXX2443",
         "namespace":"my_fields",
         "key":"product_extra_info",
      "type":"json"    ,
          "value""{\r\n  \"$id\": \"1\",\r\n  \"colour\": \"Rose Gold\"}"
        }        }  }}
tomasdelaveau
Tourist
16 0 1

hey mate, I'm sending this mutation:

mutation {
  productUpdate(input: {
    id: "gid://shopify/Product/6653113303204",
    title: "zinc test",
    metafields: [
      {
        namespace: "global",
        key: "title_tag",
        value: "zinc test",
        valueType: STRING
      }
    ]
  }) {
    product {
      id
      metafields(first: 10, after: null) {
        edges {
          node {
            key
            namespace
            value
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

and getting this response:

{
  "data": {
    "productUpdate": {
      "product": {
        "id": "gid:\/\/shopify\/Product\/6653113303204",
        "metafields": {
          "edges": []
        }
      },
      "userErrors": []
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 22,
      "actualQueryCost": 12,
      "throttleStatus": {
        "maximumAvailable": 1000.0,
        "currentlyAvailable": 988,
        "restoreRate": 50.0
      }
    }
  }
}

 I'm trying to create the metafield

Iipa
Visitor
1 0 0

Hi @tomasdelaveau 

In your query, metafield valueType is not in quotes. It should be "STRING" instead of STRING.

Could that help?

tomasdelaveau
Tourist
16 0 1

Hey @Iipa , that's incorrect. It has to be without quotes. The problem was that the gobal title tag can't have the same value as the product's title.