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.

Updating meta fields with GraphQL - conditionals possible?

Updating meta fields with GraphQL - conditionals possible?

Denizthemenace
Shopify Partner
29 2 10

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

Reply 1 (1)

antorsnt
Tourist
7 0 2

Hello

 

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

 

Thank you !