Join us for an upcoming Shopify Partner webinar on February 27, 2024. Discover the latest Checkout Extensibility features, and deep dive on improvements to Shopify Functions and Web Pixels. Register now for either the 10am EST or 2pm EST sessions.

StoreFront API Graphql - Query all products using collectionIds

RobertSWE
Tourist
9 0 6

Hi!

I'm looking for the best way to get all products that are within several collections, using Storefront API (Graphql).

I'm currently using the following query, but then I need to filter out all products from their collection in a separate array (Which is fine I guess). It has to be possible in some other way right?

query ($ids:[ID!]!) {
  nodes(ids: $ids) {
    ... on Collection {
      id
      products(first: 250) {

 

Replies 4 (4)

michaeltheodore
Explorer
59 6 9

What framework are you using?

RobertSWE
Tourist
9 0 6

Hi!

 

React, Next.Js and Apollo.

michaeltheodore
Explorer
59 6 9

I recommend using Apollo Boost so that you can connect to your store like so:

import fetch from 'node-fetch'
import ApolloClient, { gql } from 'apollo-boost'

const client = new ApolloClient({
    uri: 'https://your-store-name.myshopify.com/api/2020-10/graphql.json',
    headers: {
      'X-Shopify-Storefront-Access-Token': 'abc123'
    },
    fetch: fetch,
  });

client.query({
          query: gql`
            {
              collections(first: 10) {
                edges {
                  node {
                    id
                    title
                    products(first: 100) {
                      edges {
                        node {
                          id
                          title
                          description
                          variants(first: 10) {
                              edges {
                                  node {
                                      id
                                      title
                                      price
                                      image {
                                          src
                                      }
                                  }
                              }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          `
RobertSWE
Tourist
9 0 6

Hi, 

Thank you for your answer, but that didn't really answer my question.
I want to get all products from several collections using collection ids in one query.

Regards

Robert