Efficient way to update prices by using graph ql

Highlighted
New Member
1 0 0

We are using Shopify api to upate single's product's price. However, as we have thousands of them it takes long time to do it.
We would like to use Graphql instead.
We were trying to understand Graphql api located here:

https://shopify.dev/docs/admin-api/graphql/reference/mutation/productvariantupdate?api[version]=2020...

However it still seems to be complicated.

We were trying to find some useful examples however we haven't succeed (for example: this resource:
https://community.shopify.com/c/Technical-Q-A/GraphQL-API-update-product-and-variants/td-p/525813)
Do you have any working example how to do it in an efficient way?

Best regards,
Konrad

0 Likes
Highlighted
Shopify Staff
Shopify Staff
587 72 129

Hey @Konrado82,

 

GraphQL can definitely take some time to wrap one's head around, since it operates so differently from REST. I'll outline some general advice for using GraphQL, and I'll address your specific question about updating product prices at the bottom.

 

I recommend having a look at our GraphQL Explorer, which gives a kind of GUI interface to explore the API and see the schema while you're constructing a query. Here's some tips I wish I had when I first started using GraphQL:

 

- When you're learning, use a tool that allows you to see the schema. This makes grasping the concept of GraphQL much easier as you don't need to flip back and forth between docs and the query you're writing. Some examples:

     -Insomnia is my go to, it's an HTTP client like Postman but it has full support for GraphQL. Once you enter your API credentials and endpoint URL in Insomnia, it will automatically fetch the schema and autofill as you write a query

     -Shopify's GraphiQL app is another great option, this is an embedded app you can install on any store, and gives you a similar GUI to the GraphQL explorer I linked at the top.

 

- All queries must start on the QueryRoot. Unlike REST, GraphQL uses a schema, which means you need to know the path to your data. If you're writing a query to retrieve data from certain fields, know where those fields are located in the schema first. 

 

- Concentrate first on learning how to construct a query, worry about mutations after. Understanding how to write a query means you've grasped how to navigate the schema to get the data you need. Mutations require you to pass variables to your query, and I think it's easier to learn that once you've mastered the schema.

 

 

Updating product variant prices

 

Although one of the big advantages of GraphQL is that it allows you to move data more efficiently, this isn't the case for all actions GraphQL supports. With regards to updating product prices specifically, our GraphQL implementation currently requires 1 API call per update, so in this case GraphQL is on par with REST in terms of the number of calls required. However GraphQL are generally less expensive, so using it to update prices should allow you to make more calls per second than if you were using REST. 

 

Here's an example of a simple mutation to update the price for a variant. This mutation includes the variant_id, variant_price, and any errors in the response:

 

mutation productVariantUpdate($input: ProductVariantInput!) {
  productVariantUpdate(input: $input) {
    product {
      id
    }
    productVariant {
      id
      price
    }
    userErrors {
      field
      message
    }}}
Variables:

{
"input": { "id": "gid://shopify/ProductVariant/31365788074006", "price": 10 } }

JB | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

1 Like