Storefront API - Return Multiple Products Queried By Multiple IDs In One Request

BenNA
Shopify Partner
2 0 1

Hi,

 

I've been using the following to pull back individual product data from the Storefront API by ID:

 

 

query getProductById ($id: ID!) {
    product(id: $id) {
        id,
        title,
        requiresSellingPlan
    }
}

 

 

To avoid looping through multiple products and making individual requests for each ID, I'm looking for a way to query by multiple product IDs so that I can return multiple products via one request to the API. Does anyone have a solution for this please?

 

I should note that the specific requirements I have require this to be via product ID, so filtering a collection will not work in this instance. The page in question will show to a customer details of all their active subscriptions.

 

Thanks in advance

Replies 3 (3)

BenNA
Shopify Partner
2 0 1

Solved my own problem in the end:

 

query ExampleQuery {
    products(first: 50, query: "id:1234 OR id:5678") {
        edges {
            node {
                id,
                title,
                requiresSellingPlan
            }
        }
    }
}
c10s
Shopify Partner
67 12 26

You can also do this by querying for the nodes directly:

 

{
  nodes(ids: ["gid:\/\/shopify\/Product\/1234", "gid:\/\/shopify\/Product\/5678"]) {
    ... on Product {
      id
      title
      requiresSellingPlan
    }
  }
}
Chris29
Shopify Partner
2 0 1

Thanks so much, @c10s ! This is the first useful result on this issue, I could find so far!

For this problem, the provided tutorials by Shopify do not help at all!