StoreFront API Graphql - Query all products using collectionIds

RobertSWE
New Member
9 0 0

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) {

 

0 Likes
michaeltheodore
Explorer
59 6 8

What framework are you using?

0 Likes
RobertSWE
New Member
9 0 0

Hi!

 

React, Next.Js and Apollo.

0 Likes
michaeltheodore
Explorer
59 6 8

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
                                      }
                                  }
                              }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          `
0 Likes
RobertSWE
New Member
9 0 0

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

 

 

0 Likes