What's your biggest current challenge? Have your say in Community Polls along the right column.
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.

Creating multiple products metafields with GraphQL

Creating multiple products metafields with GraphQL

DannyAtELS
Visitor
2 0 0

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:

Screenshot 2019-12-23 at 12.29.59.png

 

Anyone able to share any insight as to what's actually wrong with it? Thanks!

Replies 3 (3)

GrantDB
Shopify Partner
80 3 14

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: 

"message""Key must be unique within this namespace on this resource"
 
To update the metafield, if it exists, you need to use the id, so add something like this "id": "gid://shopify/Metafield/11498439114845", for the metafield item
 
For my code, I do something like this
 
  1. Get the id of a product using the handle
  2. AS part of this return the metadata items
  3. Get what I want to update
  4. Compare the two so I know what to delete (separate call), update (get the id) and create.  Update and create can be in the productupdate call, just make sure you have an ID
 
I hope this helps\makes sense
 
Thanks
 
Grant
DannyAtELS
Visitor
2 0 0

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

GrantDB
Shopify Partner
80 3 14

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