Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

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

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

lendormi
Shopify Partner
3 0 0

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-qu...

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

Replies 3 (3)

MastersHub
Shopify Partner
216 21 38

Type and ownerId should follow below

 

Possible types of a metafield's owner resource.

Helpful ? Like and Accept Solution
Buy me a Coffee
Need a Shopify Developer?
Send Email or Chat on WhatsApp
lendormi
Shopify Partner
3 0 0

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"
    }
}

 

MastersHub
Shopify Partner
216 21 38

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

Helpful ? Like and Accept Solution
Buy me a Coffee
Need a Shopify Developer?
Send Email or Chat on WhatsApp