Updating meta fields in Shopify with GrapQL

Danielnz
Excursionist
34 0 17
 

I've never used GraphQL before so I am really lacking knowledge on how to go about this. I'm wanting to update product meta fields on Shopify and it appears this is the only way. What I've done so far is really fumbling...

My JSON is:

{
  "input": {
    "id": "gid://shopify/Product/749521178847",
    "metafields": [
      {
        "id": "gid://shopify/Metafield/2223333",
        "value": "Training Grounds"
      }
    ]
  }
}

I've minified this to:

{"input":{"id":"gid://shopify/Product/749521178847","metafields":[{"id":"gid://shopify/Metafield/2223333","value":"The Training Grounds"}]}}

And am then using an HTTP request to:

https://MYSTORE.myshopify.com/api/2021-10/graphql.json?query={"input":{"id":"gid://shopify/Product/749521178847","metafields":[{"id":"gid://shopify/Metafield/2223333","value":"The Training Grounds"}]}}

I get the error: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

I don't know if any of this is correct. If it is, I don't know if ?query= is the right variable to pass it through on.

Reply 1 (1)

Luke_K
Shopify Staff
402 66 98

Hey @Danielnz 

 

So with MetafieldSet in GraphQL's Admin API(docs), I'm able to get on with this when setting a Metafield to a product in version 2021-10 of the API - n.b, Metafield values will be set regardless if they were previously created or not. 

 

 

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

 

Input variables (note how I place my Product GID in ownerID)

 

{
	"metafields": {
	 "key":"test",
		"namespace":"test2",
		"ownerId":"gid://shopify/Product/123456789",
		"type":"single_line_text_field",
		"value":"test"
	}
}

 

 Here's my response my Product Metafield is created - 

 

 

{
	"data": {
		"metafieldsSet": {
			"metafields": [
				{
					"id": "gid:\/\/shopify\/Metafield\/987654321",
					"ownerType": "PRODUCT",
					"value": "test"
				}
			],
			"userErrors": []
		}
	},

 

Hope that helps! 

 

 

| Shopify |
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!