Re: Getting a list of products in a collection using the Storefront API

Getting a list of products in a collection using the Storefront API

Sterling_Smith
Excursionist
19 0 6

I have a bit of a dilemma, our app utilizes both the Admin API and the Storefront API for different features.  We currently utilize the Admin API to retrieve a list of products within a given collection, however sometimes those products that the function retrieves have not been added to app's Sales Channel, which we need to have happen before we are able to retrieve info on the product via the Storefront API (it will return an error otherwise).  The problem is, I have not been able to find a way to determine via the Admin API if a product is included in the Sales Channel or not.  The goal to show a list of only products that are included in the Sales Channel, rather than play a game of Russian roulette and have some of the products throw errors when more details are requested.  It seems inefficient to manually call for full product details from the Storefront API for each product in a long before displaying the list, so my question is twofold: is there a shorter way to determine if a product has been included in the Sales Channel without needing fetch the full details from the Storefront API, and/or is there a way to leverage a mutation on the Storefront API to retrieve a list of products within a collection like you are able to with the Admin API (I would assume this wouldn't return products outside of our app's Sales Channel, if possible)?

 

Thanks for your time.

Reply 1 (1)

AndreBastos
Excursionist
15 0 23

I dont think i understand your question.

 

The Storefront API uses GraphQL, so you only retrieve what you want from the Products API.

 

For example, if you want to show a list of products, you only fetch the data you need to show on that specific list, and then when the user enters the product details page, you fetch the remaining data you need(if you need).

 

Since you will need to use a API Key to call this API, it will only give you the products available on the Private App with that specific API Key.

 

If you want to fetch products of a specific collection, you can use the following query: 

 

query Collection($handle: String!, $first: Int = 20) {
    collectionByHandle(handle: $handle) {
        products(first: $first, after: $cursor, sortKey: TITLE) {
        pageInfo {
            hasNextPage
        }
        edges {
            cursor
                node {
                    id
                    {{other fields}}
                }
            }
        }
    }
}