Hello everyone,
I’ve been running into a strange issue with the metafieldsSet mutation, and I wanted to see if anyone else has encountered this or knows a reliable workaround.
Problem Summary:
When I use the metafieldsSet mutation to create multiple metafields with similar keys (including trailing spaces), the response I get indicates that all metafields have been created successfully, with each having its own unique value and the same id. However, when I query the metafields afterwards, I get a different set of metafields, with a different id, and the key-value pairs do not match the initial response from metafieldsSet.
Api version: 2024-10
Mutation Request Example:
mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
id
key
namespace
value
}
}
}
Variables:
{
"metafields": [
{
"ownerId": "gid://shopify/Page/82760171623",
"key": "test",
"type": "single_line_text_field",
"namespace": "the_same",
"value": "without spaces"
},
{
"ownerId": "gid://shopify/Page/82760171623",
"key": "test ",
"type": "single_line_text_field",
"namespace": "the_same",
"value": "WITH single space"
},
{
"ownerId": "gid://shopify/Page/82760171623",
"key": " test ",
"type": "single_line_text_field",
"namespace": "the_same",
"value": "WITH two spaces"
}
]
}
Response:
"metafieldsSet": {
"metafields": [
{
"id": "gid://shopify/Metafield/28307977502823",
"key": "test",
"namespace": "the_same",
"value": "without spaces"
},
{
"id": "gid://shopify/Metafield/28307977502823",
"key": "test ",
"namespace": "the_same",
"value": "WITH single space"
},
{
"id": "gid://shopify/Metafield/28307977502823",
"key": " test ",
"namespace": "the_same",
"value": "WITH two spaces"
}
],
"userErrors": []
}
}
The response from Shopify indicates that all three metafields were successfully created, and they all have the same ID (28307977502823), but with different keys and values.
Verification:
When I subsequently run a query to verify what metafields are present, the results do not match what I expected:
query selectMetafield {
page(id: "gid://shopify/Page/82760171623") {
metafields(first: 10) {
nodes {
key
namespace
value
id
}
}
}
}
Response:
{
"page": {
"metafields": {
"nodes": [
{
"key": "test",
"namespace": "the_same",
"value": "WITH single space",
"id": "gid://shopify/Metafield/28307977470055"
},
{
"key": " test ",
"namespace": "the_same",
"value": "WITH two spaces",
"id": "gid://shopify/Metafield/28307977502823"
}
]
}
}
}
It turns out that:
- There are only two metafields.
- The first metafield has a key of “test” and value “WITH single space”, but a new id (28307977470055), which was not returned during the initial mutation.
- The third metafield seems correct, with key " test " and value “WITH two spaces”.
The issue here is that I can’t trust the response from the initial metafieldsSet mutation, as the actual keys, values, and IDs of the created metafields are different when verified afterwards.
Deletion Issues:
Additionally, when I tried to delete the metafields using the metafieldsDelete mutation (since metafieldDelete is deprecated), I encountered an unexpected error.
Mutation:
mutation metafieldsDelete($metafields: [MetafieldIdentifierInput!]!) {
metafieldsDelete(metafields: $metafields) {
deletedMetafields { ownerId namespace key }
userErrors { field message }
}
}
Variables:
{
"metafields": [
{
"ownerId": "gid://shopify/Page/82760171623",
"namespace": "the_same",
"key": "test"
},
{
"ownerId": "gid://shopify/Page/82760171623",
"namespace": "the_same",
"key": " test "
}
]
}
The response was:
An unexpected error occurred while deleting metafields.
If I split this into two separate deletion requests, it works, but the bulk deletion via this mutation fails consistently.
Request for Help:
Does anyone know if there is a way to ensure that the response from the metafieldsSet mutation matches the actual data being created? The discrepancies between the returned data and what actually gets saved are problematic, especially in automated workflows where we rely on the response data.
Also, if anyone has found a way to successfully delete multiple metafields via metafieldsDelete without splitting them into separate calls, that information would be extremely helpful.
REST API isn’t a viable option for me, as it doesn’t support newer resource types like companies, etc.
Thanks in advance for any insights!