Adding Metafield on a productVariant using GraphQL

Solved
Tourist
3 0 0

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
                            }
                          }
                       }
              }
            }
          }'
        ];
0 Likes
Shopify Staff
Shopify Staff
918 55 124

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! 

 

1 Like
Tourist
3 0 0

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.

2019-08-13_2116.png

Thanks in advance..

0 Likes

Success.

Shopify Staff
Shopify Staff
918 55 124

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? 

1 Like
Tourist
3 0 0

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.

0 Likes
Shopify Staff
Shopify Staff
918 55 124

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. 

0 Likes