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.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

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

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

pablores
Tourist
4 1 3

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 8 (8)

ShopifyDevSup
Shopify Staff
1453 239 535

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 2

Is there another way to do this with multiple queries?

RfcSilva
Shopify Partner
26 1 6

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 3

You forgot the condition "products is ACTIVE" 😞 

RfcSilva
Shopify Partner
26 1 6

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 3

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

ozzyonfire
Shopify Partner
52 2 19

This should be possible now by using the productVariants graphql query. 

 

For example:

query getProductVariantsByCollection {
  productVariants(query:"collection:{your-collection-id} status:ACTIVE" first:10) {
    nodes {
      id
      sku
      product {
        title
      }
    }
  }
}

 

This will give you all of the active products for a specific collection id. The collection id you send in is just the number and not the gid. Hope this helps.

ozzyonfire
Shopify Partner
52 2 19

I made a typo in my query... it should actually be

 

... product_status:active