metafields_global_title_tag and graphql

GrantDB
Explorer
50 2 8

Are the meta data field metafields_global_title_tag and metafields_global_description_tag still supported with graphql.  I had a look but couldn't see anything, apart from more general metadata fields

 

Thanks

 

Grant

0 Likes
Zameer
Shopify Staff
Shopify Staff
295 32 75

Hey Grant,

 

Yes, both are definitely still supported in GraphQL, they are just accessed through the general metafield properties which you mentioned.

 

You can either retrieve all metafields, and find them amongst those returned using something like:

{
  product(id: "gid://shopify/Product/{product_id}") {
    metafields (first:100){
      edges {
        node {
          description
          id
          key
          legacyResourceId
          namespace
          value
          valueType
        }
      }
    }
  }
}

Or you can access them directly using their namespace and key with the individual metafield property:

{
  product(id: "gid://shopify/Product/2867945144420") {
    id
    title: metafield(namespace: "global", key: "title_tag") {
      description
      id
      key
      legacyResourceId
      namespace
      value
      valueType
    }
    description: metafield(namespace: "global", key: "description_tag") {
      description
      id
      key
      legacyResourceId
      namespace
      value
      valueType
    }
  }
}

N.B. In the above example, I've used aliases to prevent naming conflicts between fields.

 

If you are looking to create a new product with the global metafields, not just access them, you can do so with the productCreate mutation. It accepts a ProductInput, which in turn accepts a MetafieldInput where you specify the metafield values. Here's an example input which can be used in the mutation:

 

{
	"input": {
		"title": "Product Title",
		"metafields": [
			{
				"namespace": "global",
				"key": "description_tag",
				"value": "Description value",
				"valueType": "STRING"
			}
		]
	}
}
GrantDB
Explorer
50 2 8

Thanks for this.

 

When I try this when key already exists I get the following error "Key must be unique within this namespace on this resource".  I would have thought that Graphql would know that this is an update.  Is there a way in the same payload to update (or create) the product details

 

I am also wondering if there is a list somewhere of the metadata tags that are already within Shopify, like the title and description tags

 

Thanks

 

Grant

0 Likes
Zameer
Shopify Staff
Shopify Staff
295 32 75

Hey Grant,

 

Although Shopify doesn't automatically detect whether or not you're providing an existing key that you're looking to update, you're still able to accomplish this using the update mutation.

 

With productUpdate you can pass in the id of the given product as well as the id of the given metafield that you are looking to update, along with the new values for the required namespace, key, value and valueType parameters.

 

When the id parameter is present for the MetafieldInput, Shopify will update the existing product metafield instead of trying to create a new one.

 

In terms of pre-defined metafields, there's nothing currently outside of the title and description tags. That being said, there may be new ones added in the future. Subscribing to the API changelog will ensure you don't miss those updates once they're announced.

0 Likes
GrantDB
Explorer
50 2 8

Thanks for clarifying.  My main issue with that is that I need to do a separate call to get the ID (we don't store IDs anywhere) and then use that in the product update, which looks to me to be an extra step

 

Thanks

 

Grant

tomasdelaveau
New Member
8 0 0

I'm sending the following query from Insomnia and it works:

mutation {
  productCreate(input: {
    title: "sync test",
    options:["Option 1"],
    variants: [
      {
        sku: "default",
        price: "0",
        inventoryItem: {
          cost: 0.0,
          tracked: true
        }
      }
    ],
    metafields: [
      {
        namespace: "global",
        key: "title_tag",
        value: "sync_test",
        valueType: STRING
      }
    ]
  }) {
    product {
      id
      metafields(first: 10, after: null) {
        edges {
          node {
            key
            namespace
            value
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

But when I send it from Rails using HTTParty, it creates the product but not the metafield. I've tried changing the metafield key to "some_key" and that works fine. You know why?

0 Likes
tomasdelaveau
New Member
8 0 0

UPDATE: I've realised the Content-Type header differed between the Insomnia and the Rails query (json and graphql respectively). Still, can't see why changing the key of the metafield allows for its creation.

0 Likes