I ran into this issue using the discountAutomaticAppUpdate mutation, but I suspect this might be happening globally with all *update mutations.
Expected behavior
If the mutation accepts a metafields parameter, I expect it to update the existing parameters defined in the mutation.
Actual behavior
Instead of updating the exisiting metafields on the resource, the Shopify Admin API instead attempts to create new ones resulting in uniqueness errors:
field:
0: automaticAppDiscount
1: metafields
2: 0
3: key
message: must be unique within this namespace on this resource
Thanks for reporting this. I’ve looked over the other threads you shared and this does appear to be different, unless the metafields are in the global namespace.
To help me test, are you sending identical metafield values and getting the error, or are you trying to update or add new metafield values?
Ah ok, I thought these other cases were attempting to update metafields on a resource like Products.
To help me test, are you sending identical metafield values and getting the error, or are you trying to update or add new metafield values?
In this case it’s a mixture. I have a form that’s accepting the metafield values. So I had some metafields were the key,namespace and value are exactly the same.
Then I had “dirty” fields where the key, namespace were the same, but the value was modified.
In both cases I observed the “key must be unique within this namespace on this resource”.
Specifically I was testing with an discountAutomaticAppUpdate mutation.
I don’t have direct access to the underlying GraphQL client in this case, so I’m trying to see how I can hook in response header logging to find that.
Usually there’s a requestId in the response body on 500’s which makes it simple. In this case I triple checked, there’s no requestId at all.
I did some further testing to see how admin UI handles metafield updates and updating the metafield values there worked as expected without any errors.
That led me back to our documentation for a closer look at what’s expected with metafields. I found that the metafields parameter in discountAutomaticAppUpdate is specifically designed for adding additional metafields, not updating existing ones.
“Additional metafields to associate to the discount.”
The solution here is when updating existing metafields via the GraphQL API, use the metafieldsSet mutation instead. This mutation handles both creating and updating metafields without the constraint issues: