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

Rick_Murt
Shopify Partner
5 0 4

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!

Replies 9 (9)
Rick_Murt
Shopify Partner
5 0 4

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!

Nick_Neufeld
Shopify Staff (Retired)
Shopify Staff (Retired)
1 0 14

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
    }
  }
}

 

Thomas_Lang1
Shopify Partner
213 6 43

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

Software Developer | Owner of Tom IT - We build shopify apps
Marketplace Order Connector | Amazon bol.com & Walmart DSV
Product Inventory Information | Geolocation, Store Pickup & More

Order Related Documents | Link Documents to Products Or Countries
jpraninPF
Tourist
12 1 1

I've noticed that this "guid" string has to be base64 encoded for it to work with the Storefront API

"gid://shopify/Product/456" --> Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzQ1Ng==

This can be accomplished with the Javascript btoa() function - https://www.w3schools.com/jsref/met_win_btoa.asp 

ymq
Shopify Partner
15 0 12

 

微信截图_20200921105024.png

 

query test($ids: [ID!]!) {
  nodes(ids: $ids) {
    ... on Product {
      id
      variants(first: 100) {
        edges {
          node {
            title
          }
        }
      }
    }
  }
}

{
  "ids": ["gid://shopify/Product/4406002679856","gid://shopify/Product/4567473815600"]
}

 

Leo_Huynh
Tourist
7 1 1

Works for me. Thanks to @ymq 

Leo Huynh | Shopify Software Engineer @ Insights Studio
Email: leo@insights.is
Personal site: https://leohuynh.dev
darakhsa_farhan
New Member
11 0 0

Hello Sir,

From your query, i will be able to obtain specific products by product ID(s) but how to obtain available products only with queryroot Nodes?

Like this: query:"published_status:published"

Can you please guide me?

darakhsa_farhan
New Member
11 0 0

How to obtain available products only with this query like this >> query:"published_status:published" ?

dev_it21
New Member
1 0 0

Do you have an idea how will we paginate this with cursor based pagination?