Bug? productCreate mutation - error updating metafield

I think I encountered a bug with the productCreate mutation.

When creating a new product, I am adding new data into a custom app-data metafield of type single_line_text_field which was defined previously. Here is the structure of the MetafieldInput as part of the call to productCreate, where key3 is the single_line_text_field:

// productCreate mutation,
{
  variables: {
    media: media,
    input: {
      metafields: [
        {
          namespace: "$app:{appName}",
          key: "key1",
          value: JSON.stringify(data1)
        },
        {
          namespace: "$app:{appName}",
          key: "key2",
          value: data2
        },
        {
          namespace: "$app:{appName}",
          key: "key3",
          value: data3
        },
      ],
    },
  },
}

With this call, I receive the following userErrors:

{
  "data": {
    "productCreate": {
      "product": null,
      "userErrors": [
        {
          "field": [
            "metafields",
            "2",
            "type"
          ],
          "message": "Type can't be blank"
        }
      ]
    }
  }

If I add type: "single_line_text_field" to key3, the mutation does not error out, but then there is no data in the app-data metafield. If I remove namespace for key3, the mutation does not fail, and the data gets populated in the app-data metafield. In addition, if I remove namespace from key1 and key2, the mutation fails with similar userErrors :man_shrugging:

According to the documentation, the namespace field for the MetafieldInput object states:

Required when creating a metafield, but optional when updating. Used to help identify the metafield when updating, but can’t be updated itself

In my case, the metafield for key3 has already been defined. Therefore, technically I am updating the metafield, which should mean namespace is optional. However, omitting namespace seems to work for single_line_text_field metafields, but not for other metafield types (namespace required for key1 and key2, but not key3)

Please confirm if this is a bug or a documentation error.

The X-Request-Id for this call is b425c575-073e-4f8c-8812-064af3dc72df-1739377843

UPDATE: I think I found the reason for the error. From my previous post about “INTERNAL_ERROR”, it seems if you omit the namespace when creating an app-data metafield, the namespace used is

app--{appId}

NOT

app--{appId}--{appTitle}

I was assuming the latter, so my call was attempting to update a metafield that did not exist. The metafields for key1 and key2 where created with the $app:{appTitle} syntax, hence why those calls worked correctly.

SUMMARY: If you’re not concerned about the naming convention of app-data metafields, then you can omit the namespace from the metafieldsSet mutation. However, if you are concerned about the naming convention, then use the $app:{appTitle} syntax in your mutation.

Hope this helps other developers!

1 Like