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.

Get list of all products and variants only in stock

Get list of all products and variants only in stock

dannycodes
Shopify Partner
5 0 3

Hi,

 

We are currently querying the graphql admin api and running a bulk operation query to get the list of all products, it's images, and it's variants.

 

Currently looks like this:

 

mutation {
      bulkOperationRunQuery(
      query: """
        {
  products(query: "published_status:published AND status:active") {
    edges {
      node {
        id
        images {
          edges {
            node {
              id
              originalSrc
              altText
            }
          }
        }
        variants {
          edges {
            node {
              id
              sku
              position
              selectedOptions {
                name
                value
              }
              image {
                id
                originalSrc
                altText
              }
              weight
              weightUnit
              taxCode
              inventoryQuantity
              inventoryPolicy
              price
              compareAtPrice
              requiresShipping
              taxable
              barcode
              product {
                id
                handle
                onlineStorePreviewUrl
                onlineStoreUrl
                title
                descriptionHtml
                vendor
                productType
                tags
                publishedAt
                options {
                  id
                  name
                  position
                  values
                }
                featuredImage {
                  id
                  originalSrc
                }
                images {
                  edges {
                    node {
                      id
                      originalSrc
                      altText
                    }
                  }
                }
                isGiftCard
                seo {
                  title
                  description
                }
                status
              }
            }
          }
        }
      }
    }
  }
}
        """
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }

 

 

Now we have a new requirement where we want only in stock items. As I understand it, inventory belongs to the variant and therefore I would need to ask for variants with `inventory_quantity` greater than 0.

I thought that the best way to do this would be to edit the query for so that variants adds some query to like so:

 

mutation {
      bulkOperationRunQuery(
      query: """
        {
  products(query: "published_status:published AND status:active") {
    edges {
      node {
        id
        images {
          edges {
            node {
              id
              originalSrc
              altText
            }
          }
        }
       # This is edited line
        variants(query: "inventory_quantity:>0") {
          edges {
            node {
              id
              sku
              position
              selectedOptions {
                name
                value
              }
              image {
                id
                originalSrc
                altText
              }
              weight
              weightUnit
              taxCode
              inventoryQuantity
              inventoryPolicy
              price
              compareAtPrice
              requiresShipping
              taxable
              barcode
              product {
                id
                handle
                onlineStorePreviewUrl
                onlineStoreUrl
                title
                descriptionHtml
                vendor
                productType
                tags
                publishedAt
                options {
                  id
                  name
                  position
                  values
                }
                featuredImage {
                  id
                  originalSrc
                }
                images {
                  edges {
                    node {
                      id
                      originalSrc
                      altText
                    }
                  }
                }
                isGiftCard
                seo {
                  title
                  description
                }
                status
              }
            }
          }
        }
      }
    }
  }
}
        """
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }

 


But it seems that variants doesn't support the query argument unless it's the query root. 

 

I would like to keep my query as close to the current structure as it is right now and I will need products and images in the response because I have multiple parts of my app that rely on the response structure. I would also like to avoid making multiple calls as I want only one jsonl file.

 

Any advice on how to do this would be greatly appreciated.

 

Thanks

Replies 2 (2)

chaitanya_Yanam
Visitor
1 0 0

@dannycodes wrote:

Hi,

 

We are currently querying the graphql admin api and running a bulk operation query to get the list of all products, it's images, and it's variants.

 

Currently looks like this:

 

 

mutation {
      bulkOperationRunQuery(
      query: """
        {
  products(query: "published_status:published AND status:active") {
    edges {
      node {
        id
        images {
          edges {
            node {
              id
              originalSrc
              altText
            }
          }
        }
        variants {
          edges {
            node {
              id
              sku
              position
              selectedOptions {
                name
                value
              }
              image {
                id
                originalSrc
                altText
              }
              weight
              weightUnit
              taxCode
              inventoryQuantity
              inventoryPolicy
              price
              compareAtPrice
              requiresShipping
              taxable
              barcode
              product {
                id
                handle
                onlineStorePreviewUrl
                onlineStoreUrl
                title
                descriptionHtml
                vendor
                productType
                tags
                publishedAt
                options {
                  id
                  name
                  position
                  values
                }
                featuredImage {
                  id
                  originalSrc
                }
                images {
                  edges {
                    node {
                      id
                      originalSrc
                      altText
                    }
                  }
                }
                isGiftCard
                seo {
                  title
                  description
                }
                status
              }
            }
          }
        }
      }
    }
  }
}
        """
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }

 

 

 

Now we have a new requirement where we want only in stock items. As I understand it, inventory belongs to the variant and therefore I would need to ask for variants with `inventory_quantity` greater than 0.

I thought that the best way to do this would be to edit the query for so that variants adds some query to like so:

 

 

mutation {
      bulkOperationRunQuery(
      query: """
        {
  products(query: "published_status:published AND status:active") {
    edges {
      node {
        id
        images {
          edges {
            node {
              id
              originalSrc
              altText
            }
          }
        }
       # This is edited line
        variants(query: "inventory_quantity:>0") {
          edges {
            node {
              id
              sku
              position
              selectedOptions {
                name
                value
              }
              image {
                id
                originalSrc
                altText
              }
              weight
              weightUnit
              taxCode
              inventoryQuantity
              inventoryPolicy
              price
              compareAtPrice
              requiresShipping
              taxable
              barcode
              product {
                id
                handle
                onlineStorePreviewUrl
                onlineStoreUrl
                title
                descriptionHtml
                vendor
                productType
                tags
                publishedAt
                options {
                  id
                  name
                  position
                  values
                }
                featuredImage {
                  id
                  originalSrc
                }
                images {
                  edges {
                    node {
                      id
                      originalSrc
                      altText
                    }
                  }
                }
                isGiftCard
                seo {
                  title
                  description
                }
                status
              }
            }
          }
        }
      }
    }
  }
}
        """
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }

 

 


But it seems that variants doesn't support the query argument unless it's the query root. 

 

I would like to keep my query as close to the current structure as it is right now and I will need products and images in the response because I have multiple parts of my app that rely on the response structure. I would also like to avoid making multiple calls as I want only one jsonl file.

 

Any advice on how to do this would be greatly appreciated.

 

Thanks


query:"status:ACTIVE product_type:Fabric out_of_stock_somewhere:false")

SapirGev
Shopify Partner
1 0 0

not sure how this helps. 

Im trying to get all products with variants that are in stock using the following query but I'm getting the following error:

"message": "Invalid bulk query: Field 'variants' doesn't accept argument 'query'"

 

mutation {
bulkOperationRunQuery(
query: """
{
    products (query: "published_status:published AND status:active AND inventory_quantity:>0"){
        edges {
            node {
                id
                options {
                    id
                    name
                    position
                    values
                }
                featuredImage {
                    id
                    originalSrc
                }
                status
                images {
                    edges {
                        node {
                            id
                            originalSrc
                            altText
                        }
                    }
                }
                variants(query: "inventory_quantity:>0") {
                    edges {
                        node {
                            id
                            sku
                            position
                            selectedOptions {
                                name
                                value
                            }
                            image {
                                id
                                originalSrc
                                altText
                            }
                            weight
                        }
                    }
                }
            }
        }
    }
}
"""
) {
    bulkOperation {
        id
        status
    }
    userErrors {
        field
        message
    }
}
}

 

Is there any option to do this with different query?