GraphQL API update product and variants

Solved
Peter_R_K
Tourist
3 1 3

G'day,

 

I need to update a product ( tags, productType, ... )  and all its variants with the same ( price, compareToPrice ).

Obviously, I can update the product and then loop through the (max 6) variants. This works ok, but ...

Is it possible to do this in one? If so, where/how do I specify the 'ProductVariantInput' and 'IDs' in the example below.

 

 mutation {
               productUpdate(input: {id: "gid://shopify/Product/193......83", tags: ["Jacket, warm"], productType: "on-sale"}) {
               product {
                              id
                              tags
                              productType
                              variants(first: 6) {
                                    edges {
                                          node {
                                                   id
                                                   price
                                                   compareAtPrice
                                                    }
                                              }
                                         }
                                 }
                           }
                       }    

0 Likes
Peter_R_K
Tourist
3 1 3

This is an accepted solution.

I have figured it out now. This is a solution, just in case someone else wants to know. 

The string for the mutation is generated within the Java program.

The number of blocks var0: ... var1: ... is determined by the number of variants of the product.

 

mutation {
      prod: productUpdate(input: {id: "gid://shopify/Product/193......99", tags: ["Knitwear, New Arrivals, Sale, Shorts"], productType: "Sale-Item"})

                {
                product {
                tags
                productType
               }
       }
       var0: productVariantUpdate(input: {id: "gid://shopify/ProductVariant/176.....219", price: 69.00, compareAtPrice: 89.95})

       {
       productVariant {
                price
                compareAtPrice
                }
     }
     var1: productVariantUpdate(input: {id: "gid://shopify/ProductVariant/1762....987", price: 69.00, compareAtPrice: 89.95})

    {
    productVariant {
               price
              compareAtPrice
            }
     }
}

FurnaceX
Shopify Partner
27 2 26

Hi Peter, this was very helpful.

 

Do you know if there is a way to dynamically do something similar...

Perhaps the number of variants you will have to update changes from mutation to mutation. 

 

I'm having two issues:

1. Not sure you can have a gql query that is created on demand.

2. Not sure how to pass in an array of inputs.

 

Thanks!

0 Likes
Peter_R_K
Tourist
3 1 3
Hi,

I am creating all the queries within a Java program like this: the object 'product' contains all sorts of things. I have written functions to read the
data from shopify into 'product', or an array of products, using GraphQL queries. I use the Java objects for all data manipulation and reporting.
Updates (mutations) are also generated in the program. For example, I am using some of the properties to update prices. Note the loop to add one "var" for each variation for that particular product. Product.variations is a list (could also be an array) of 'variation' objects, each with the appropriate properties for that product.

public String make_mutation_GraphQL(product p) {
String q = "mutation { prod: productUpdate( input: { id: \"" + p.id + "\", tags: [\"" + p.tags + "\"], productType:\"" + p.productType + "\" } ){ product { tags productType } } ";
int i = 0;
for (Object o : p.variations) {
String v = (String) o;
q = q + "var" + i++ + ": productVariantUpdate( input: { id: \"" + v + "\", price:" + p.price + " compareAtPrice: " + p.compareAtPrice + " } ) { productVariant { price compareAtPrice } } ";
}
q = q + "}";
return q;
}

The above function returns a string ( i.e. the query ), which is then fired off to the server. Here are the first lines of that function.

public String PostGraphQL(String GQLquery) {
String outp = "";
try {
ProcessBuilder pb = new ProcessBuilder("curl", "-v", "-X", "POST", "https://" + get_API() + "/admin/api/graphql.json", "-H", "Content-Type: application/graphql", "-d", GQLquery);
final Process p = pb.start();
...

I hope that helps ...

Cheers,

Peter
0 Likes
shubhamsoni2617
New Member
8 0 0

@Peter_R_K This feature is more to do with how to construct dynamic graphql query. Here is how I did it.

import { Heading, Page } from "@shopify/polaris";
import axios from "axios";
import gql from "graphql-tag";
import { useEffect } from "react";
import {  useMutation } from "react-apollo";

// construct the array in below format
const arr = [
  { id: "gid://shopify/Product/6788361027756", name: "Product1", title: "a" },
  { id: "gid://shopify/Product/6791099121836", name: "Product2", title: "b" },
  { id: "gid://shopify/Product/6791099383980", name: "Product4", title: "d" },
  { id: "gid://shopify/Product/6791098990764", name: "Product3", title: "c" },
];

let ipn = `
`;

for (let elem of arr) {
  ipn += `${[elem.name]} : productUpdate(
    input: { id: "${elem.id}", title: "${elem.title}" }
  ) {
    product {
      title
    }
  }
  `;
}

const UPDATE_PRODUCT = gql`
  mutation {
    ${ipn}
  }
`;

const Index = () => {
  const [updateProduct, { products }] = useMutation(UPDATE_PRODUCT);

  return (
    <Page>
      <Heading>Shopify app with Node and React</Heading>
      <button onClick={() => updateProduct()}>test</button>
    </Page>
  );
};

export default Index;
0 Likes