How to get product-related collections in Shopify using Storefront API?

Hey everyone! I’m stuck on a Shopify project and could use some help. I’m trying to figure out how to get all the collections that a specific product belongs to. Like if I have a product ID for a ‘Blue Hoodie’, I want to know which collections it’s part of.

I’ve been looking at the Shopify Storefront API, but I’m not sure if it can do this or how to use it for this purpose. Does anyone know if there’s a way to do this with the API? What kind of query would I need to use? Are there any tricks to make it work?

If someone could point me in the right direction or share some example code, that would be awesome. Thanks in advance for any help!

hey there HappyDancer99! yeah, you can totally do that with the Storefront API. u need to use the collections field on the Product type. try somethin like this:

query {
  product(id: "your-product-id") {
    collections {
      edges {
        node {
          title
        }
      }
    }
  }
}

that should give u the collections for ur Blue Hoodie. hope it helps!