Obtaining an inventory total for variant using GraphQL

Highlighted
New Member
9 0 0

Hi - I'm new to Shopify: love what I see, but am struggling with what I hope will be one small problem:

I'm trying to get the inventory count for a variant - I've scoured the Shopify dev site and docs, but haven't found anything that works for me. So far, I have managed to get back a JSON object with details of a test variant, using the steps at this link. This gives me results when I use https://havecakeeatcake.myshopify.com/admin/api/2020-07/products/5557596127382/variants/356239605433....

I'd really like to get the same data using GraphQL if at all possible - I can see there is an option to do this on that page, but if I use the GraphQL example given:

query getVariantByID($id: ID!) {
  productVariant(id: $id) {
    id
    title
    inventoryItem {
      id
    }
  }
}

I've updated the productVariant entry to shopifyProductVariant (I hope that's right!), but the inventoryItem entry does not feature at all in my GraphiQL explorer. How would I turn the REST query into something I can use in GraphQL? I'm using Gatsby / GraphQL as the basis for my site, so would like to return something that I can then consume in my site.

I suspect there is something not set correctly somewhere, but am not sure where - I've checked my Admin API permissions, and have Inventory and Products both set to read-only; is this sufficient? At this stage I'm not looking to update this value; it's purely a POC to confirm I can return details from the Shopify Admin API into my site when needed. The site is also a single location.

Any ideas? I'd love to see this working on my site - please help!

0 Likes
Highlighted
Shopify Staff
Shopify Staff
181 22 29

Hey @alibby 

You are looking for the inventoryLevels connection: 

{
  productVariant(id:"gid://shopify/ProductVariant/<id>"){
    inventoryItem{
      id
      inventoryLevels(first:5){
        edges{
          node{
            available
          }
        }
      }
    }
  }
}

 

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

0 Likes
Highlighted
New Member
9 0 0

Hey Kevin_A,

Thanks for the info - unfortunately though it's not worked for me!

I dropped it in to GraphiQL, and adjusted the id value being passed with a known ID from one of my variants - it wasn't recognising the value I had set as a query variable initially, so thought hardcoding it would at least confirm if it worked:

{
  productVariant(id:"gid://shopify/ProductVariant/35623960543382"){
    inventoryItem{
      id
      inventoryLevels(first:5){
        edges{
          node{
            available
          }
        }
      }
    }
  }
}

When I ran the query, it initially came back with "Cannot query field productVariant on type Query. Did you mean shopifyProductVariant, or allShopifyProductVariant?"

I changed it to the former, at which point it threw an error on the id value being passed in as the ID - it came back with an "Expected type StringQueryOperatorInput, found..." error, and it also complained with a second error, which was "Cannot query field inventoryItem on type Query." 

As a quick check, I also tried allShopifyProductVariant too - same issue with inventoryItem appeared, but this time it complained of not recognising the id being passed to the query.

I'm a bit stuck as to where to go next - is it possible that I've missed something in my config somewhere, which means not all of the relevant fields are displayed in GraphiQL?

0 Likes
Highlighted
Shopify Partner
661 46 132

Strange, as I just ran the query below in the GraphiQL app installed on my test store. It came back with results just fine?

{
  productVariant(id: "gid://shopify/ProductVariant/31517938876468") {
    inventoryItem {
      id
      inventoryLevels(first: 5) {
        edges {
          node {
            location {
              id
              name
            }
            available
          }
        }
      }
    }
  }
}

 

{
  "data": {
    "productVariant": {
      "inventoryItem": {
        "id": "gid://shopify/InventoryItem/33060349739060",
        "inventoryLevels": {
          "edges": [
            {
              "node": {
                "location": {
                  "id": "gid://shopify/Location/203128",
                  "name": "Sawmill"
                },
                "available": 2
              }
            },
            {
              "node": {
                "location": {
                  "id": "gid://shopify/Location/17431658558",
                  "name": "Easton"
                },
                "available": 0
              }
            },
            {
              "node": {
                "location": {
                  "id": "gid://shopify/Location/17431691326",
                  "name": "Weber"
                },
                "available": 0
              }
            }
          ]
        }
      }
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 14,
      "actualQueryCost": 10,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 990,
        "restoreRate": 50
      }
    }
  }
}

 

0 Likes
Highlighted
New Member
9 0 0

Mmm - that is indeed strange! Thanks though for checking.

I'm running my query directly from http://localhost:8000/__graphql - when you say "GraphQL app", do you mean this route, or something else? I'm keen to see if I can display the value on my individual product pages, hence why I'm doing it via this URL.

I'm beginning to think there may be an issue with how my Shopify access is set up - in my Gatsby config file, I've got this:

    {
      resolve: `gatsby-source-shopify`,
      options: {
        shopName: process.env.SHOP_NAME,
        accessToken: process.env.SHOPIFY_ACCESS_TOKEN,
        verbose: true,
        includeCollections: ["shop", "product", "inventory"],
      },
    },

I've included the "inventory" collection in the above code, on the basis that I believe this is necessary to view Inventory items - is this correct? I've also checked the API permissions, and all are set to read-only.

EDIT: I've also tried in the Shopify GraphQL app at https://shopify.dev/graphiql/admin-graphiql, using your code, and a variety of different IDs (including both product and variants - just in case!). This is what I'm getting back:

{
  "data": {
    "productVariant": null
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 14,
      "actualQueryCost": 1,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 999,
         "restoreRate": 50
      }
    }
  }
}

Have I missed anything?

0 Likes
Highlighted
Shopify Partner
661 46 132

That's what I was using --- the GraphiQL app installed on my test shop --> https://shopify.dev/tools/graphiql-admin-api. If you are coming back with no results with that then I'd suggest verifying you are working with a legitimate variant ID. Issue a simple query in the GraphiQL app like this:

{
  productVariant(id: "gid://shopify/ProductVariant/31513252823092") {
    id
    displayName
  }
}

If that's coming back null as well, then that's an invalid variant ID... 

0 Likes
Highlighted
New Member
9 0 0

Hey Kevin_A,

I think we're making progress...kind of!

I've installed the GraphQL app; by trial and error, I worked out that part of the problem seems to be an invalid variant ID! If I run up your "ID check" code, I get this:

{
  "data": {
    "productVariant": {
      "id": "gid://shopify/ProductVariant/35623960543382",
      "displayName": "Chocolate & hazelnut cupcake - Individual cakes"
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 1,
      "actualQueryCost": 1,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 999,
        "restoreRate": 50
      }
    }
  }
}

However, if I retrofit that ID into your first query, I get this error returned:

{
  "data": {
    "productVariant": null
  },
  "errors": [
    {
      "message": "Access denied for inventoryItem field. Required access: `read_inventory` access scope.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "productVariant",
        "inventoryItem"
      ],
      "extensions": {
        "code": "ACCESS_DENIED",
        "documentation": "https://shopify.dev/docs/admin-api/access-scopes",
        "requiredAccessScope": "read_inventory"
      }
    }
  ],

I'm not sure where to set these permissions - I've checked the Storefront API permissions for my app and they all show as being enabled. Any ideas please? I'm assuming that if I can get this working, I can then plug this GraphQL code into my Gatsby/React code and it should produce the same result, right?

0 Likes
Highlighted
Shopify Partner
661 46 132

If you think your API permissions indeed cover all relevant scopes, then you can verify this with your access token. Trying hitting this API endpoint and it should return the scopes you have access to --> https://shopify.dev/docs/admin-api/rest/reference/access/accessscope. If read_inventory isn't listed in the response then that's definitely the issue. 

0 Likes
Highlighted
New Member
9 0 0

Hi - I've checked the Storefront API; this had Inventory and Products enabled, and that I believe the former covers the read_inventory access scope, right?

I tried the check URL you've suggested, which came back with this - confirming that it does indeed appear to be a permissions issue:

HTTP/1.1 200 OK
{
  "access_scopes": [
    {
      "handle": "read_products"
    },
    {
      "handle": "write_orders"
    },
    {
      "handle": "read_orders"
    }
  ]
}

However the read_inventory access scope was already set to read access, but it seems this permission hasn't filtered down to the GraphQL app, as this is still showing access denied errors. I've tried changing this to read and write to see if both levels are needed; that has had no effect. 

Is it possible that I need to enable other access scopes, or is just the Inventory one sufficient?

0 Likes
Highlighted
Shopify Partner
661 46 132

You might need to re-auth the app again maybe so the proper scopes are included in the URL parameters? 

 

https://shopify.dev/tutorials/authenticate-with-oauth

 

0 Likes