2
votes

I am brand new to graphQL and I have to retrieve all of the products. I have been looking around, watching tutorials and reading and people seem to return all of a certain type of data they want but I cannot. For example it will throw an error saying you must provide first or last but I want them all or it will say totalCount or count does not exist.

 {
  products {
    id
    title
    price
    
  }
}

// or get total count of products

 {
  products {
   totalCount
  }
}

I was basically trying to do something like that. I understand I may not receive any help because no one has access to my shopify admin api but maybe even an example or anything that I could see. This is from my graphQL query root of options I have for products. List of products.

ProductConnection!
first: Int
Returns up to the first n elements from the list.

after: String
Returns the elements that come after the specified cursor.

last: Int
Returns up to the last n elements from the list.

before: String
Returns the elements that come before the specified cursor.

reverse: Boolean = false
Reverse the order of the underlying list.

sortKey: ProductSortKeys = ID
Sort the underlying list by the given key.

query: String
Supported filter parameters:

barcode
created_at
delivery_profile_id
error_feedback
gift_card
inventory_total
is_price_reduced
out_of_stock_somewhere
price
product_type
publishable_status
published_status
sku
status
tag
title
updated_at
vendor
See the detailed search syntax for more information about using filters.

savedSearchId: ID
ID of an existing saved search. The search’s query string is used as the query argument.
1

1 Answers

0
votes

You can't get all products with a single GraphQL request either with the StoreFront GraphQL or the Admin GraphQL.

If your products are below 250 you can make a request with first: 250 but if you have more than 250 products than you need to make recursive request with the cursor pagination for GraphQL. So if you have 1000 products you will need to make 4 request (depending on which API you are using, the storefront or admin graphql api, since they are different)

At the moment there is no way to get all products using a single request via any of the provided APIs from Shopify.

The only way to achieve this is to make a custom template with the following code:

[
{% paginate collection.products by 9999 %}
  {% for product in collection.products %}
    {{product | json}}{% unless forloop.last %},{% endunless %}
  {% endfor %}
{% endpagination %}
]

Call it something like collection.ajax.liquid.

And make a fetch request to it using the view param:

fetch('/collections/all?view=ajax').then((response) => handle the response)

Have in mind that the more products you have the longer the request to that page will be. If you have 1000 products the request can take up to 10 seconds. So this is not a great solution as well for massive pool of products.


As for the total count there is a liquid object for that {{ collection.all_products_count }} or if you are doing admin stuff, use the rest api, since there is a method to get the products count, but there is none in the graphql.