"invalid id" error when trying to update product's metafields

"invalid id" error when trying to update product's metafields

Sprockets
Visitor
1 0 0

Hello, so I created some metafields and have retrieved the ids for them successfully

query {
    metafieldDefinitions(ownerType: PRODUCT, first:10) {
        nodes {
            id
            key
        }
    }
}
 
{
                    "id": "gid://shopify/MetafieldDefinition/XXXXXXXXX",
                    "key": "category"
                },
                {
                    "id": "gid://shopify/MetafieldDefinition/YYYYYYYYYY",
                    "key": "subcategory"
                }
But when I try to update them like below I get an error
 
mutation productUpdate($input: ProductInput!) {
productUpdate(input: $input) {
product {
id
metafields(first: 10) {
edges {
node {
namespace
key
value
}
}
}
}
userErrors {
field
message
}
}
}
 
{
"input": {
"id": "gid://shopify/Product/ZZZZZZZZZZ",
"metafields": [{
"id": "gid://shopify/MetafieldDefinition/XXXXXXXXX",
"value": "Exterior"
},
{
"id": "gid://shopify/MetafieldDefinition/YYYYYYYYYY",
"value": "Bumpers"
}]
}
}
 
"errors": [
{
"message": "invalid id",
"locations": [
{
"line": 3,
"column": 3
}
],
"path": [
"productUpdate"
]
}
]
 
 any ideas why?
Reply 1 (1)

kjchabra
Shopify Partner
26 1 6

@Sprockets you are passing in the Metafield Definition Id which is not the correct id to pass. Each Product's Metafield has a different metafield id for the metafield definition it is associated with.

 

You have to query the Product first and then pass the metafield id which is retrieved from the response of the query.

query {
  product(id: "gid://shopify/Product/1") {
    metafield(namespace: "instructions", key: "wash") {
      id # pass in this id value as your metafields id
      value
    }
  }
}

 

Docs:

1. Query the product

2. Update the product