A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
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.
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!
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?
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!
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
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
Also interested in hearing that 🙂