Trying to update a metafield with graphql and got a "Invalid Id"

Hello everyone,

I am currently in the process of updating metafields for a client’s order. However, I have encountered an issue with GraphQL, which is returning an “Invalid id” error when I attempt to execute my query mutation.

Query :

mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
  metafieldsSet(metafields: $metafields) {
    metafields {
      namespace
    }
    userErrors {
      field
      message
    }
  }
}

variables :

{
    "metafields": {
        "namespace": "[custom_namespace]",
        "key": "documents_1",
        "value": "https://[custom_namespace]/document_1.pdf",
        "ownerId": "gid://shopify/Metafield/xxxxxxxx",
        "type": "url"
    }
}

I have come across another developer who appears to be facing an almost identical issue, as documented in the following discussion thread:

https://community.shopify.com/c/graphql-basics-and/metafieldsset-mutation-returns-quot-invalid-id-quot-for-product/m-p/2024680

Interestingly, I have found that the REST API functions flawlessly in this regard.

Here a request ID : 211c55d9-aa47-4a4b-86a7-5399c1cb1643

I would greatly appreciate your assistance with resolving this matter.
Thank you for your help

Type and ownerId should follow below

Possible types of a metafield’s owner resource.

https://shopify.dev/docs/api/admin-graphql/2024-01/enums/MetafieldOwnerType

1 Like

okay thank you @MastersHub

It appears that you’ve identified the key issue regarding the “OwnerType” for metafields. Understanding the concept of “OwnerType” can indeed be difficulut.
it’s crucial to use “gid://shopify/Order” as the owner type for the metafields are Order.

example: gid://shopify/Order, not gid://shopify/Metafield

{
    "metafields": {
        "namespace": "[custom_namespace]",
        "key": "documents_1",
        "value": "https://[custom_namespace]/document_1.pdf",
        "ownerId": "gid://shopify/Order/xxxxxxxx",
        "type": "url"
    }
}

@lendormi You’re welcome! I’m glad to hear it worked great.