Query array of product IDs with GraphQL API in one request?

Shopify Partner
5 0 0

Hi there,

Fairly new to GraphQL in general, very experienced with the admin REST API. If I'm developing an app in which I want to be able to select products from the default /admin/products and perform a bulk action (being passed an array of IDs from Shopify to my app) can I make one single request in GraphQL with an array of IDs? I searched the docs and forums up and down and don't see a way to do it. Do I need to make a new request for every single product? (If so, seems to defeat the purpose of making less requests!)

Thanks!

0 Likes
Shopify Partner
5 0 0

Okay, kind of answered my own question: aliases.

Example:

{
  product1: product(id: "gid://shopify/Product/1343489802342") {
    title
  }
  product2: product(id: "gid://shopify/Product/1343489835110") {
    title
  }
}

but let's say I have 30 products. Surely there is a way to dynamically create this query instead of specifying product1, product2, ...., product30

What would be the best method to do this? If I'm using JavaScript, should I just generate the query from JavaScript? Or is there a GraphQL way of doing this?

Thanks!

0 Likes
Shopify Staff (Retired)
Shopify Staff (Retired)
1 0 3

Hi Rick!

Another option is the nodes field on QueryRoot, which takes an array of ids.

{
  nodes(ids: ["gid://shopify/Product/123", "gid://shopify/Product/456"]) {
    ...on Product {
      title
    }
  }
}

 

3 Likes
Highlighted
Shopify Partner
133 4 12

Would this be possible to do with handle as well? I have 10 handles for products and 10 handles for collections, can I somehow group them into an efficient query? I need to get the collection titles / image and product titles / image from grahpql

0 Likes