Solved

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

WilliamDumontRT
Shopify Partner
3 1 0

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

WilliamDumontRT_0-1695237312852.png

 

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

Accepted Solution (1)

WilliamDumontRT
Shopify Partner
3 1 0

This is an accepted solution.

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.

View solution in original post

Replies 3 (3)

Liam
Shopify Staff
2731 299 769

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.

Liam | Developer Advocate @ 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

WilliamDumontRT
Shopify Partner
3 1 0

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

WilliamDumontRT
Shopify Partner
3 1 0

This is an accepted solution.

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.