A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Hi everyone, I'm having some issues when trying to create multiple metafields on a product via the GraphQL API. Obviously with it being relatively new-ish there's not a great deal of info out there at the mo, but from what I understand, the mutation I'm sending should be creating two metafields for a specific product, but it only ever creates one.
Here's a screenshot from GraphiQL:
Anyone able to share any insight as to what's actually wrong with it? Thanks!
One thing, make sure you have this in your GraphQL, you will then see errors if there are any
} userErrors { field, message } }
I think you aren't doing any update, rather just returning what you have created previously. From my experience when updating metadata fields you need to use the ID of the metadata item that was previously created, for example
mutation productUpdate($input: ProductInput!) { productUpdate(input: $input) { product { id handle title tags images(first:5) { edges { node { id } } }, metafields(first:5) { edges { node { id key
value } } } } userErrors { field, message } } }
Try this
{"input": { "id": "gid://shopify/Product/abc", "handle": "a handle", "title":"The title", "bodyHtml": "<div id='hc-product-description'><p>Continuing the story begun in The Hobbit, this is the first part of Tolkien\u2019s epic masterpiece, The Lord of the Rings, featuring an exclusive cover image from the film, the definitive text, and a detailed map of Middle-earth.</p><p>Sauron, the Dark Lord, has gathered to him all the Rings of Power \u2013 the means by which he intends to rule Middle-earth. All he lacks in his plans for dominion is the One Ring \u2013 the ring that rules them all \u2013 which has fallen into the hands of the hobbit, Bilbo Baggins.</p><p>In a sleepy village in the Shire, young Frodo Baggins finds himself faced with an immense task, as his elderly cousin Bilbo entrusts the Ring to his care. Frodo must leave his home and make a perilous journey across Middle-earth to the Cracks of Doom, there to destroy the Ring and foil the Dark Lord in his evil purpose.</p><p>To celebrate the release of the first of Peter Jackson\u2019s two-part film adaptation of The Hobbit, THE HOBBIT: AN UNEXPECTED JOURNEY, this first part of The Lord of the Rings is available for a limited time with an exclusive cover image from Peter Jackson\u2019s award-winning trilogy.</p></div>", "vendor": "Vendor", "productType": "Standard", "tags": ["version-0.1"], "metafields": [ {"namespace": "test", "key": "title_tag", "value":"a title","valueType": "STRING"} ], } }
Do it once and i will work (if the metadata field doesn't exist), do it again and you will see an error, in the error section:
Hey Grant,
Thanks for the help. Turns out the request in my OP is actually correct and does create multiple metafields, however your answer gave me some insight with errors. The issue was that the first metafield already existed. If I change that metafield to a new key, the request creates them all as planned.
Thanks for the help!
Danny
Great, glad it helped. Just remember that the next time you have date the product it will fail without the ID, that caught me previously.
Grant