Bulk Update Products through Admin GraphQL

Solved
majidzulfiqar
Shopify Partner
8 0 1

Hi,

I am looking a way by which i can update bulk update products using your Admin GraphQL.

I have explored Admin GraphQL, but at the moment i only found productUpdate mutation which will update only a single product or I am missing something here. Is there a way to bulk update products in one call.

Looking forward to your response.

Thanks in Advance!

Accepted Solution (1)

Accepted Solutions
KarlOffenberger
Shopify Partner
1873 184 863

This is an accepted solution.

There is no mutation for bulk creating or updating product resources or any other resource. Same goes for the REST API. The only exception is the ability to bulk update inventory levels at a location via inventoryBulkAdjustQuantityAtLocation.

 

That said, you can have multiple mutations in one request which will be executed in series - so essentially it is a question of testing and confirming whether this approach yields better performance or not for your use case).

 

mutation {
  Product1: productCreate(input: {
    title: "Test Item 1", 
    publishOn: "2019-01-31T00:00:00-12:00"
  }) {
    product {
      id
    }
  }
  
  Product2: productCreate(input: {
    title: "Test Item 2", 
    publishOn: "2019-01-31T00:00:00-12:00"
  }) {
    product {
      id
    }
  }
}

Best wishes!

Liked this post? You might also like our fantastic upsell apps Candy Rack, Last Upsell post-purchase and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie

View solution in original post

Replies 10 (10)
KarlOffenberger
Shopify Partner
1873 184 863

This is an accepted solution.

There is no mutation for bulk creating or updating product resources or any other resource. Same goes for the REST API. The only exception is the ability to bulk update inventory levels at a location via inventoryBulkAdjustQuantityAtLocation.

 

That said, you can have multiple mutations in one request which will be executed in series - so essentially it is a question of testing and confirming whether this approach yields better performance or not for your use case).

 

mutation {
  Product1: productCreate(input: {
    title: "Test Item 1", 
    publishOn: "2019-01-31T00:00:00-12:00"
  }) {
    product {
      id
    }
  }
  
  Product2: productCreate(input: {
    title: "Test Item 2", 
    publishOn: "2019-01-31T00:00:00-12:00"
  }) {
    product {
      id
    }
  }
}

Best wishes!

Liked this post? You might also like our fantastic upsell apps Candy Rack, Last Upsell post-purchase and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
majidzulfiqar
Shopify Partner
8 0 1

I got your point on updating products by having multiple mutations in one request, So what will be the cost of this request. I have tested this multiple mutations query and it is showing me query cost: 20 and when i add three mutations it show me 30. Does it mean that for one update mutation request it cost me 10 and it multiplies by number of mutations?

 

Also you have mentioned about bulk update inventory levels at a location via inventoryBulkAdjustQuantityAtLocation, this will only update/adjust inventory availableDelta for given inventoryItemId's at one location, or it can update multiple locations as well in same mutation ? 

KarlOffenberger
Shopify Partner
1873 184 863

I don't know the exact query costs and how it grows per additional mutation - that's up to you to add up and decide on the benefits / disadvantages.

 

No, the inventoryBulkAdjustQuantityAtLocation is per location. You would also need to be careful that it expects deltas, not absolute inventory levels.

Liked this post? You might also like our fantastic upsell apps Candy Rack, Last Upsell post-purchase and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
majidzulfiqar
Shopify Partner
8 0 1

Thanks for information 🙂

gnurpreet
New Member
1 0 0

Another example where prices of multiple variants:

 

mutation{
  ProductVariant1: productVariantUpdate(input: {
    id: "gid://shopify/ProductVariant/27868413755001",
    price: 34.97
  }){
    productVariant{ id, price }
  }
  
  ProductVariant2: productVariantUpdate(input: {
    id: "gid://shopify/ProductVariant/27868413788000",
    price: 34.97
  }){
    productVariant{ id, price }
  }
}

The cost of the query is 20 since 2 are being updated

sandip001
Shopify Partner
5 0 6

1. why there is no mutation to set inventory levels directly using the GraphQL Admin API ?

2. Any idea about in future shopify allow to set inventory levels directly using the GraphQL Admin API ?

aveshopstech
Excursionist
24 0 15

Its 2021 here! Same need still exists. @Shopify, any chance we can get an inventoryBulkSetQuantityAtLocation mutation? Bulk adjust is good in some scenarios, but very frequently forcing the inventory levels to a specific quantity is the only solution. Right now, and in the absence of this mutation, I'm calling a bunch of REST endpoints in a huge loop. That's far from efficient for either of us. Thanks for considering.

shubhamsoni2617
New Member
8 0 0

@aveshopstech  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()}>Bulk Update</button>
    </Page>
  );
};

export default Index;

 

 

lauri_jalonen
New Member
2 0 0

I'm using inventoryBulkAdjustQuantityAtLocation successfully at the moment. However, one of our platforms has 8,500 inventory entries which change frequently. That bulk call only allows for 100 at a time, so, I'm having to chunk my inventory update deltas into batches of 100.
Based on your example, I'd be able to chain multiple calls though... would this mean I could chain inventoryBulkAdjustQuantityAtLocation calls in a similar manner?

Just in time for your next adventure
lauri_jalonen
New Member
2 0 0

You can calculate the delta easily: 

  1. use GraphQL to get an export of all products from Shopify. Include variants and their inventory levels in the query fields.
  2. parse out variants from that query result, and grab their inventory id's
  3. compare to your variant stock levels, by substracting the shopify inventory level from your inventory level. You get a delta.
  4. use inventoryBulkAdjustQuantityAtLocation call to send inventory deltas.
Just in time for your next adventure