ProductUpdate - Create an update metafield doesn't work like in documentation example

Topic summary

A user encountered errors when attempting to update product metafields using the ProductUpdate GraphQL mutation, despite following the documentation example.

Initial Problem:

  • Including a metafield key resulted in “Duplicate keys found” error
  • Using only metafield ID and value triggered “Key is too short” error
  • Multiple validation errors appeared: namespace and key can’t be blank, type can’t be blank, minimum 3 characters required

Attempted Solution:
Another user suggested including all metafield properties (id, namespace, key, value, type) in the input, even when only updating an existing metafield.

Root Cause Identified:
The original poster discovered they were using the metafield definition ID from the definition URL instead of the actual metafield instance ID. Once corrected with the proper metafield ID, the mutation worked as intended.

Remaining Question:
The user notes confusion about the documentation stating only ID and value are mandatory when updating metafields, yet the system required additional fields (namespace, key, type) to avoid errors.

Summarized with AI on November 16. AI used: claude-sonnet-4-5-20250929.

The documentation on the mutation ProductUpdate gives this example which sounds very useful:

In theory, I should be able to update a product, create metafields, and update metafield values all in the same call.

In practice, when I try to perform such an operation (updating a product and its metafield values), I get an error:
-If I included a key for the metafield: Dupplicate keys found

-If I included a metafield ID instead and just a value: Key is too short

mutation updateProductMetafields($input: ProductInput!) {
  productUpdate(input: $input) {
    product {
      id
      metafields(first: 25) {
        edges {
          node {
            id
            namespace
            key
            value
          }
        }
      }
    }
    userErrors {
      message
      field
    }
  }
}
{
  "input": {
    "metafields": [
      {
        "id": "gid://shopify/Metafield/2337571046",
        "value": "Rubber"
      }
    ],
    "id": "gid://shopify/Product/8269738410214"
  }
}
"userErrors": [
        {
          "message": "Type can't be blank",
          "field": [
            "metafields",
            "0",
            "type"
          ]
        },
        {
          "message": "Namespace can't be blank",
          "field": [
            "metafields",
            "0",
            "namespace"
          ]
        },
        {
          "message": "Namespace is too short (minimum is 3 characters)",
          "field": [
            "metafields",
            "0",
            "namespace"
          ]
        },
        {
          "message": "Key can't be blank",
          "field": [
            "metafields",
            "0",
            "key"
          ]
        },
        {
          "message": "Key is too short (minimum is 3 characters)",
          "field": [
            "metafields",
            "0",
            "key"
          ]
        }
      ]
    }

I literally copy and pasted the example from the documentation, only replacing ID values. Am I doing something wrong, or is the documentation incorrect?

Thank you

Hi William,

Can you try with these input variables?

{
  "input": {
    "metafields": [
      {
        "id": "gid://shopify/Metafield/2337571046",
        "namespace": "your_namespace",
        "key": "your_key",
        "value": "Rubber",
        "type": "single_line_text_field"
      }
    ],
    "id": "gid://shopify/Product/8269738410214"
  }
}

Remember to replace "your_namespace" and "your_key" with your own namespace and key. The "type" field should be a valid metafield type. In this case, "single_line_text_field" is used, but you should replace it with your own type if it’s different.

Let me know if this still results in an error.

1 Like

Duh, turns out I was taking the metafield ID from the definiton URL, but that’s the ID of the definition and not the metafield itself…

It works as intended with the proper ID.

Thank you for your response. The docs say that only mandatory fields when updating are ID and Value. So I shouldn’t have to input those.

See my error below…