Graphql API - Get all ACTIVE and PUBLISHED Products by Collection Id

pablores
Tourist
4 1 1

Hi, I need to get all active and published products by collection id.

 

I know I can get all Active and published products by doing this:

{
  products (first:50, query:"status:active AND published_status:published") {
    nodes {
      id,
      title,
    }
  }
}

And I know I can get all products by collection id by doing this:

query ProductsByCollection ($id: ID!) { 
  collection(id: $id) {
    handle
    products(first: 50) {
      nodes {
        title,
        id,
        status
      }
    }
  }
}

 

How can I get all active and published products for a specific collection with just one query ?

 

Thanks

Replies 6 (6)

ShopifyDevSup
Shopify Staff
1312 215 454

Hi @pablores,

 

Thanks for your post. Currently there's not a built in way to achieve that in a single query so we've submitted some feedback about it internally, particularly around having some more filters in the Collection.products connection.

 

Hope you have a great day

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

BohdanNikitchuk
Shopify Partner
5 0 1

Is there another way to do this with multiple queries?

RfcSilva
Shopify Partner
17 1 2

Hi Pablores!

Put this in Query:

query Collections ($first: Int!, $reverse: Boolean!, $query: String!){
    collections(first: $first, reverse: $reverse, query: $query) {
        edges {
            node {

                id
                products(first: 250) {
                    edges {
                        node {
                            id
                            title
                            status
                            description
                        }
                    }
                }
            }
        }
    }
}

 

and put this in Variables:

{
    "first": 1,
    "reverse": true,
    "query": "id:440808079677"
}

 

You will receive 250 products from that collection identified on the ID variable:

{
    "data": {
        "collections": {
            "edges": [
                {
                    "node": {
                        "id": "gid://shopify/Collection/440808079677",
                        "products": {
                            "edges": [
                                {
                                    "node": {
                                        "id": "gid://shopify/Product/8177835835709",
                                        "title": "DARSHAN & GURUPURNIMA 2023",
                                        "status": "ACTIVE",
                                        "description": "Darshan starts on the 01.07.2023 at 14:00 in the Tent. Gurupurnima celebration starts on the 03.07.2023 at 18:00 at the stage outside."
                                    }
                                },
                                {
                                    "node": {
                                        "id": "gid://shopify/Product/8189817225533",
                                        "title": "DARSHAN 01 JULY 2023 14:00 @ SPN",
                                        "status": "ACTIVE",
                                        "description": "You can attend 2 In-Person Darshan in July. One of them has to be the 24th of July.Newcomers can attend 3 In-Person Darshans in July."
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 7,
            "actualQueryCost": 7,
            "throttleStatus": {
                "maximumAvailable": 10000.0,
                "currentlyAvailable": 9993,
                "restoreRate": 500.0
            }
        }
    }
}
LongPC
Shopify Partner
7 0 1

You forgot the condition "products is ACTIVE" 😞 

RfcSilva
Shopify Partner
17 1 2

It's true, but I am using C#, and because of that I can just use LINQ to filter the returned data super quick.
Don't know what technology you are using, but maybe it has something similar

LongPC
Shopify Partner
7 0 1

But in case this collection has the first 250 products as DRAFT, that means at this time no products found in one graphQL query