Out now! Check out the Poll results: Do you have a Shopify store?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

GraphQL get product variant quantity

GraphQL get product variant quantity

ggsandro
Shopify Partner
3 0 0

Hi guys,

 

if anyone knows how to get a quantity of product variant using GraphQL? 

 

As the documentation says "inventoryQuantity" field should exist in ProductVariant object.  

https://help.shopify.com/en/api/graphql-admin-api/reference/object/productvariant

 

However, when I try to get it I see this error

"No field of name "inventoryQuantity" found on type "ProductVariant" in schema"

 

My product type is "event" and I do see an "available" field in my ProductVariant which is true. Could it be something wrong in how I set up my Shopify product?

 

Thx for your help.

Replies 6 (6)

KarlOffenberger
Shopify Partner
1873 184 903

A little more context would be required to understand your error. Are you making direct calls to the API or via a wrapper library / SDK? Are you using the correct endpoint? Storefront API and GraphQL Admin API are considerably different even though many resource object names are the same. It's easy to tell: Storefront is a URL like https://storename.myshopify.com/api/graphql while GraphQL Admin API is https://storename.myshopify.com/admin/api/graphql.json.

 

For instance, the Storefront API only has availableForSale of type boolean. Admin API does in fact have the inventoryQuantity so a admin query such as below would work perfectly fine.

 

{
  product(id: "gid:\/\/shopify\/Product\/<PRODUCT_ID>") {
    id
    variants(first: 100) {
      edges {
        node {
          id
          inventoryQuantity
        }
      }
    }
  }
}

Hope this helps!

ggsandro
Shopify Partner
3 0 0

Thx for your reply!

 

I'm using https://www.npmjs.com/package/graphql-js-client and requests to Storefront.

 

This is my current request to get product information.

 

 let query = client.query((root) => {
      root.add('node', { args: { id: _id }, alias: 'product' }, (node) => {
        node.add('id');
        node.addInlineFragmentOn('Product', (Product) => {
          Product.add('title');
          Product.add('createdAt');
          Product.add('description');
          Product.add('descriptionHtml');
          Product.add('productType');
          Product.add('publishedAt');
          Product.add('tags');
          Product.add('updatedAt');
          Product.add('vendor');
          Product.addConnection('images', { args: { first: 250 } }, (images) => {
            images.add('src');
            images.add('id');
            images.add('altText');
          })
          Product.addConnection('variants', { args: { first: 250 } }, (variants) => {
            variants.add('id');
            variants.add('product');
            variants.add('title');
            variants.add('price');
            variants.add('available');
            variants.add('image', (image) => {
              image.add('src');
              image.add('id');
              image.add('altText');
            })
          })
        })
      })
    });

So, you are saying that it's not possible to get variants quantity using Storefront, right? And I should use  GraphQL Admin API, right?

KarlOffenberger
Shopify Partner
1873 184 903

Yes and no 😉

 

Yes, I am saying you cannot get inventory levels via Storefront API.

 

No, not necessarily saying you should be using the admin API. That depends on what you are developing. If it is a store, I'd be very careful how you use the admin API. Unlike the Storefront APIs public access token, the admin API requires a key and password which you never want to expose publicly - that would be like giving away your admin UI access credentials.

 

Second, even if you used the admin API on a server, say a node.js express app or similar, the admin API is throttled and not designed for the kind of load demanded by a store - you'd need to account for this in your design.

 

Third, if you really need inventory levels you can also work with Liquid - the variant object in Liquid does have an inventory_quantity and you could use a nolayout template to output a JSON object thereby creating your own "kinda API endpoints". However, even the Liquid inventory_quantity is an aggregate of all inventory locations so you wouldn't be able to query how much at location 1, how much at location 2 etc.

 

If you need inventory level per location then there is no way around admin API, but it will be very difficult to get consistent levels because you will most likely not be able to query the admin API for every storefront request for inventory levels. You'd hit throttling soon. So expect to have cached inventory levels that may be outdated.

 

Best of luck!

Thomas_Lang1
Shopify Partner
237 6 56

Hi There,

 

My app let's you display inventory information per location on the product detail page in the frontend, is that what you are looking for?

 

You can check it out here: https://apps.shopify.com/product-inventory-information

 

 

Software Developer | Owner of Tom IT - We build shopify apps
Marketplace Order Connector | Amazon bol.com & Walmart DSV
Order Related Documents | Print documents, Email document &Autoprint
Blog Product Spotlight | Add products to your blog articles!

Ziv_Emmet
Visitor
1 0 1

Hello Karl, On a similar issue,

I use the Storefront API [end point api/2019-07/graphql.json]

I'm able to query my products in different ways, also using productByHandle, but for some reason i'm not able to use productVariants query like for example:

productVariants(first:1, query: "sku:BMY722"). I get an error that its not exists under QueryRoot.

 

Any idea what could be the probable reason for this ?

Thank you

Ziv

dev88
Tourist
11 0 6

Also interested in hearing that 🙂