skip to Main Content

I am using the following graphql to list all products in all collections, I am trying to modify this now to only list products in a single collection, I know the collection ID already but I cannot figure out where to put it in order to filter on the collection ID.

{
  shop {
    collections(first: 10) {
      edges {
        node {
          id
          description
          products(first: 250) {
            edges {
              node {
                id
                description
                variants(first: 10) {
                  edges {
                    node {
                      id
                      sku
                      price
                      selectedOptions {
                        name
                        value
                      }
                    }
                  }
                }
              }
            }
          }
        }
        cursor
      }
      pageInfo {
        hasNextPage
      }
    }
  }
}

2

Answers


  1. Instead of grabbing all collections and then filtering, you can just grab a collection by its handle.

    {
      shop {
        collectionByHandle(handle: "frontpage") {
          id
          description
          products(first: 250) {
            edges {
              node {
                id
                description
                variants(first: 10) {
                  edges {
                    node {
                      id
                      sku
                      price
                      selectedOptions {
                        name
                        value
                      }
                    }
                  }
                }
              }
              cursor
            }
            pageInfo {
              hasNextPage
            }
          }
        }
      }
    }
    

    Just replace frontpage with whatever your handle you like.

    Login or Signup to reply.
  2. Here is another answer with particular collection id wise
    
    
    {
    shop {
        .node(id: <YOUR COLLECTION ID>) {
            .onCollection {
                .products(first: 10) {
                    pageInfo {
                        hasNextPage
                    }
                    edges {
                        node {
                            id
                            description
                            variants(first: 10) {
                                edges {
                                    node {
                                        id
                                        sku
                                        price
                                        selectedOptions {
                                            name
                                            value
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search