Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hello, I am working on a shopify app using graphQL.. how can we add a metafield on a productVariant using graphQL? I tried to replicate this from the docs https://help.shopify.com/en/api/guides/metafields/admin-api-metafields#creating-metafields and since i am trying to add metafield in the variant I used the productVariantUpdate instead of productUpdate.. I don't have any error.. but it seems that it was not added based on the response of the request.. Here's the query I used.. thanks in advance..
$input = '{ id: "gid://shopify/ProductVariant/1", metafields: [ { namespace: "test", key: "b_date", value: "testing" } ] }'; $query = [ 'mutation { productVariantUpdate(input: '.$input.') { productVariant { id title sku metafields(first: 100) { edges { node { id namespace key value } } } } } }' ];
Solved! Go to the solution
This is an accepted solution.
Hey again @reshielann ,
Did you happen to copy my mutation and input exactly? I'm not able to replicate any errors when I do using the same version as you (2019-10).
If you do copy it verbatim (and swap your own variant_id in) and still have errors, could you let me know the shop you're attempting the mutation on?
Josh | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Hello @reshielann ,
I think it may just be because there isn't a valueType field on your metafield input, the following mutation worked for me :
Input : { "input": { "id": "gid://shopify/ProductVariant/1", "metafields": [ { "namespace": "test", "key": "b_date", "value": "testing", "valueType": "STRING" } ] } } Mutation : mutation ($input: ProductVariantInput!) { productVariantUpdate(input: $input) { productVariant { id title sku metafields(first: 10) { edges { node { id namespace key value } } } } } }
Hope that helps!
Josh | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Hello @Josh,
Thanks for the reply.
I tried to add the valueType but it says
Argument 'valueType' on InputObject 'MetafieldInput' has an invalid value. Expected type 'MetafieldValueType'."
when I removed it, I don't have any error but the data are not updated. here's the screenshot.
Thanks in advance..
This is an accepted solution.
Hey again @reshielann ,
Did you happen to copy my mutation and input exactly? I'm not able to replicate any errors when I do using the same version as you (2019-10).
If you do copy it verbatim (and swap your own variant_id in) and still have errors, could you let me know the shop you're attempting the mutation on?
Josh | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
thanks @Josh , it's working for me now..
Sorry, just one more thing, are we allowed to update the metafields in the variant?
I don't see any update metafields in the docs.
Hey again @reshielann ,
You certainly can! You can update a variant metafield with the same mutation actually, you just also need to add the ID of the metafield you want to update into your input's metafield payload.
Josh | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog