Get all Products and variants GraphQL

Hi there,

I’m migrating our app from the rest API and wondering what is the best way to fetch all products and their variants for a given shop.

With the rest API you can go page by page, and each product includes all its variants, simple.

Currently I’m working off of something like this:

{
    products(first: 250) {
      edges {
        node {
          id
          title
          handle
          description
          variants(first: 250) {
            edges {
              node {
                id
                title
                inventory_item_id
                price
              }
            }
          }
        }
      }
    }
  }

What I’m wondering here

  1. what is the best way to handle going over the cost? Can we guarantee we can fetch all 250 variants similar to the rest API?

I haven’t run over the cost yet (which I used to when graphql first came out). But what I do is on the variants portion of the products query, add in the pageInfo with the endCursor. Then I follow up with a call to the product passing in that query with the after on the variants.

            pageInfo {
              hasNextPage
              endCursor
            }

Now in my case I’m not getting all products, I’m getting one product, and it’s variants (and more info). So it’s possible you may hit costs I don’t. Not 100% what you asked but all I have (since I haven’t hit the cost limit but aren’t requesting all products).

(realized after writing this the emailed post was different than what you have now oh well).

Thanks for the info, yeah I ended up figuring out the nested pagination. I guess the only way to really find out about the cost limits is going to be to play around with various queries and product layouts and find some heuristic values that work…

If anyone has tuned this type of query for themselves would love to know what you came up with!

Hi @Andrej

The Bulk Operations API is perfect for retrieving all data from a store of a specific type without hitting limits or implementing pagination.

You essentially create a meta-mutation that contains your query, then Shopify will give you a link to poll, or you can specify a webhook that should receive this data in a JSONL format for easy parsing.

I highly recommend this approach if you need all data of a certain type from a given Shopify store.

Note that you can only run one of these queries of each type (query or mutation) at a given time. So if you have parallel batch scripts attempting this they can sometimes run into each other and you have to sync them. Just something to consider before you plan to use it.

I use it to try to sync a set of products stored in another system as well as inventory so they sometimes have to wait for the other to finish.

You can run only one bulk operation of each type (bulkOperationRunMutation or bulkOperationRunQuery) at a time per shop. This limit is in place because operations are asynchronous and long-running. To run a subsequent bulk operation for a shop, you need to either cancel the running operation or wait for it to finish.

Note that you can only run one of these queries of each type (query or mutation) at a given time.

Small detail, you can run a single bulk operation per merchant.

But Shopify’s API will give you an error message if you happen to run into this.