Updating meta fields with GraphQL - conditionals possible?

Topic summary

Main issue: updating Shopify product metafields (custom product fields) across an inventory where some products have existing metafields and others do not.

Key question: can a single GraphQL mutation (an operation that modifies data) conditionally update existing metafields and create new ones, or must this be handled externally with separate steps (finding existing metafields and using their IDs vs. creating new ones)?

Example goal: keep Product A’s metafield value, change Product B’s value, and add a new metafield to Product C.

Current approach: using productUpdate with a metafields array; includes the metafield id for updates and omits the id for new creations. Code snippets provided are central to understanding the setup.

Latest development: a follow-up asks whether a solution was found.

Status: no definitive answer or resolution is shared; the thread remains open with the core question unanswered.

Summarized with AI on February 1. AI used: gpt-5.

Hi,

I’d like to regularly update product metafields. Each product will have a fixed number of metafields. Between each update, new products will be created, so the inventory will include products which have and don’t have these metafields.

Is there a possibility to create new metafields and update existing ones with a single GraphQL query? Or do I need first to find existing ones, then do one mutation for them incl. the metafield id, and another one for the new products?

Example for what I want to accomplish:

Before run

Product A, metafield-a: red

Product B, metafield-a: blue

Product C, // no metafields

After run

Product A, metafield-a: red

Product B, metafield-a: green // new value

Product C, metafield-a: yellow // new metafield

Right now I’m using following mutation to update fields:

mutation($input: ProductInput!) {
  productUpdate(input: $input) {
    product {
      metafields(first: 100) {
        edges {
          node {
            id
            namespace
            key
            value
          }
        }
      }
    }
    userErrors {
        field,
        message
    }
  }
}
{
	"input": {
		"id": "gid://shopify/Product/123",
		"metafields": [
			{
				"id": "gid://shopify/Metafield/456",
				"namespace": "product-description",
				"key": "wash",
				"value": "handwarm wash",
				"valueType": "STRING"
			}
		]
	}
}

and another one without the line

"id": "gid://shopify/Metafield/456",

for the new products.

So can I have a kind of conditional logic within a GraphQL mutation or do need to handle this outside of the mutation?

Thanks

Deniz

3 Likes

Hello

Have you managed to find a solution to this problem by the way ?

Thank you !

1 Like