A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Hi All,
So I was asked to update 300 products in our store and I wanted to give GQL a try so I went to postman and wrote a test mutation on a product. I wrote a mutation to mutate price, compareAtPrice and tags, but then when I executed the mutation, somehow the SKU and the barcode data on the product got deleted. I am convinced that I am a silly goose, because I'm still learning GQL, but I would love to hear what I am doing wrong or get some directions on what knowledge I miss.
So here is the test mutation
mutation productUpdate($input: ProductInput!) {
productUpdate(input: $input) {
product {
# Product fields
tags: tags,
variants ( first: 1){
edges {
node {
compareAtPrice,
price
}
}
}
}
userErrors {
field
message
}
}
}
and here is the input
{
"input": {
"id": "gid://shopify/Product/7149760184515",
"tags": [
"CAT_BE_MM_1214",
"logistics_small_devices",
"tax_key_1",
"rrp"
],
"variants": [
{
"compareAtPrice": "54.99",
"price": "47.00"
}
]
}
}
Hi @SudoBas 👋
Not silly at all! The `Product.variants` connection is a little nuanced since it behaves like a pointer to the mutable array of variants on the product object. The reason why your first variant is being losing the SKU after the mutation is because there isn't an `productVariant.id` field specified in the list of variants. Without an explicit id, your single variant array input is creating a new variant, then overriding the existing array of variants. In fact, if your original product had 2 variants, and the input has a variant array with a single variant with the ID specified, you'll notice that the resulting product will have only that single variant left.
The inputs needs to include the entire array of variants with their respective ids, then specify the fields to be updated. Alternatively, you can leave out the variants from the input when using `productUpdate` then use `productVariantsBulkUpdate` mutation to update the variant level fields.
To demonstrate the `productUpdate` mutation, say we query the below product:
{
product(id: "gid://shopify/Product/0") {
tags
variants (first:10) {
nodes {
id
sku
price
}
}
}
}
Returning:
{
"data": {
"product": {
"tags": [],
"variants": {
"nodes": [
{
"id": "gid://shopify/ProductVariant/1",
"sku": "sudobas-one",
"price": "1.00"
},
{
"id": "gid://shopify/ProductVariant/2",
"sku": "sudobas-two",
"price": "2.00"
},
{
"id": "gid://shopify/ProductVariant/3",
"sku": "sudobas-three",
"price": "3.00"
}
]
}
}
},
Then say we use the `updateProduct` mutation as follows:
mutation ($input: ProductInput!) {
productUpdate(input: $input) {
product {
tags
variants (first: 10){
nodes {
id
sku
price
}
}
}
}
}
With inputs:
{
"input": {
"id": "gid://shopify/Product/0",
"tags": [
"sudobas-tag"
],
"variants": [
{
"id": "gid://shopify/ProductVariant/1"
},
{
"id": "gid://shopify/ProductVariant/2",
"price": 99
}
]
}
}
We would expect no change to `/ProductVariant/1`, then only the price changed for `/ProductVariant/2`, and finally `ProductVariant/3` would be removed from the product:
{
"data": {
"productUpdate": {
"product": {
"tags": [
"sudobas-tag"
],
"variants": {
"nodes": [
{
"id": "gid://shopify/ProductVariant/1",
"sku": "sudobas-one",
"price": "1.00"
},
{
"id": "gid://shopify/ProductVariant/2",
"sku": "sudobas-two",
"price": "99.00"
}
]
}
}
}
},
Hope that helps!
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog