skip to Main Content

I am using the shopify buy sdk: https://www.npmjs.com/package/shopify-buy

I would like to get the current number of items in a stock for a specific product. I can fetch product info like this:

var Client = require('shopify-buy');
const shopify = Client.buildClient({
    domain: REDACTED_SHOPIFY_DOMAIN,
    storefrontAccessToken: REDACTED_SHOPIFY_TOKEN
});
let product = await shopify.product.fetch(productId);

the product returns the following values:

  'attrs',           '__typename',
  'id',              'availableForSale',
  'createdAt',       'updatedAt',
  'descriptionHtml', 'description',
  'handle',          'productType',
  'title',           'vendor',
  'publishedAt',     'onlineStoreUrl',
  'options',         'images',
  'variants',        'type'

This does not include information about the stock.

On this page it shows there’s a value called "totalInventory" which is what i need, but this i think is a different SDK and mine doesn’t return that value: https://shopify.dev/docs/api/storefront/2023-04/objects/Product

I also went into my api app settings, and looked at the "Storefront API access scopes". I added one called unauthenticated_read_product_inventory which I thought would do it, but it still returns the same info.

I also tried to use the graphql client, which seems like another way to access the API.

async function getProductGraphql(productId) {
    const productsQuery = shopify.graphQLClient.query((root) => {
        root.addConnection('products', { args: { first: 10 } }, (product) => {
            product.add('title');
            //product.add('totalInventory');
        });
    });

    shopify.graphQLClient.send(productsQuery).then(({ model, data }) => {
        let productData = data.products.edges[0].node;
        console.log('got product', productData);
    });
}

It works if I just ask for the title, but if I try to get totalInventory it says "Error: No field of name "totalInventory" found on type "Product" in schema"

Any idea how I can get this info using this sdk?

EDIT: I am closer. I switched require to this, which gives you access to properties that arent included normally. It now succecssfully can tell me the totalInventory of products.

var Client = require('shopify-buy/index.unoptimized.umd');
    const productsQuery = shopify.graphQLClient.query((root) => {
        root.addConnection('products', { args: { first: 10 } }, (product) => {
            product.add('title');
            product.add('totalInventory');
        });
    });

    shopify.graphQLClient.send(productsQuery).then(({ model, data }) => {
        let productData = data?.products?.edges[0]?.node;
        console.log('got product', productData);
    });

The problem with this example is that it’s looking for a list of products, not a specific one. So I tried to modify the query to access product instead of products, which should let me search by ID:

const productsQuery = shopify.graphQLClient.query((root) => {
    root.addConnection('product', { args: { id: productId } }, (product) => {
        product.add('title');
        product.add('totalInventory');
    });
});

shopify.graphQLClient.send(productsQuery).then(({ model, data }) => {
    let productData = data?.products?.edges[0]?.node;
    console.log('got product', productData);
});

But this throws an error on "root.addConnection" that says Error: No field of name "pageInfo" found on type "Product" in schema, which I dont understand because I don’t say "pageInfo" anywhere…

2

Answers


  1. Chosen as BEST ANSWER

    Found there's a bug report open for this exact issue (since 2021).

    It seems this wont work to get a single product, and I instead have to grab ALL products, then search through them for the right one.

    async function getProductsGraphql4(productId) {
        const productsQuery = shopify.graphQLClient.query((root) => {
            root.addConnection('products', { args: { first: 250 } }, (product) => {
                product.add('title');
                product.add('totalInventory');
            });
        });
        let result = await shopify.graphQLClient.send(productsQuery)
        return result.data.products.edges.find(p => p.node.id === productId).node;
    }
    

  2. Any update on this? I am dealing with the same issue. I built a static site, and on the product details page, need to do an inventory lookup.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search