Update mutations cannot update metafields

Hi there,

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

Potentially related topics

1 Like

Hey @Dylan!

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.

Thanks! I’ll set up some tests to replicate here. Do you by chance have a request id from one of these mutations that returned the error?

Sure, I can try - what’s the header that you need?

The x-request-id is what I would need.

Gotcha, I’ll try to get that for you.

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.

Hey Dylan,

I’ve been able to replicate the error you are getting. I’ll dig in more from here to see what I can find.

Thanks @KyleG-Shopify , sorry I haven’t been able to retrieve the response headers.

The shopify-api-node library isn’t very flexible unless you have access to the constructor.

No worries.

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:

mutation {
  metafieldsSet(
    metafields: [
      {
        ownerId: "YOUR_DISCOUNT_ID"
        namespace: "$app:discount-function-test"
        key: "function-configuration"
        type: "json"
        value: "{\"cartLinePercentage\": 25, \"orderPercentage\": 15, \"deliveryPercentage\": 100, \"collectionIds\": []}"
      }
    ]
  ) {
    metafields {
      id
      namespace
      key
      value
    }
    userErrors {
      field
      message
    }
  }
}

Ah sorry, I misunderstood the capability of this mutation.

Shoot, so my workaround was actually the real solution after all.

Thanks for working through that with me.

1 Like

Glad to! Until now I didn’t have a dedicated app spun up just for discount functions, so this will come in handy going forward.

Have a great rest of your week :rocket: