I’m currently facing an issue: I want to perform a PUT request to update some metafields, but when a metafield is empty (i.e., hasn’t been created yet), I can’t retrieve its id.
As a result, the response only includes the id of the title_tag metafield. The description_tag metafield is missing because it doesn’t exist yet.
What’s the best way to handle this ?
Should I check first if the metafield exists and then POST it if missing, or is there a better approach to ensure all metafields are always present?
Thanks for the answer but this is not working for me cause i can’t POST a title metafield for a product I tried everything but it still doesn’t work can you tell me how to post a new metafields title in n8n ?
Withe graphQL ? HTTP Request ? and what do you put in your query ?
And then,
GraphQL Query to Create a Metafield
{
“query”: “mutation MetafieldCreate($input: MetafieldInput!) { metafieldCreate(input: $input) { metafield { id namespace key value type } userErrors { field message } } }”,
“variables”: {
“input”: {
“namespace”: “global”,
“key”: “title_tag”,
“value”: “Your Title Here”,
“type”: “single_line_text_field”,
“ownerId”: “gid://shopify/Collection/723425165694”
}
}
}
^^^^This one’s for adding a title_tag metafield to a collection
If the metafield already exists with that namespace/key pair for the owner, this will throw an error. So if you want to make it safer, you should first query to check existence, then:
Use metafieldCreate if it doesn’t exist
Use metafieldUpdate if it does
Use this to check the existence of metafield:
query {
collection(id: “gid://shopify/Collection/723425165694”) {
metafield(namespace: “global”, key: “title_tag”) {
id
value
}
}
}