Trying to get all products with their collections

Solved
jbpapp
New Member
4 0 0

Hi All,

I'm working on an app and trying to get all the products (their ID and tags) of the store with bulk operation. This is working fine. But now, I'd like to also include their collections.
Is it something I can do?

This is my original query:

 

mutation {
      bulkOperationRunQuery(
       query: """
            { products(query: "published_status:published") {
        edges {
          node {
            id
            tags
          }
        }
    }}
        """
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }

 

And this is what I've tried, but no luck (most probably I do something wrong).

mutation {
      bulkOperationRunQuery(
       query: """
            { products(query: "published_status:published") {
        edges {
          node {
            id
            tags
            collections(first: 10) {
                edges {
                    node {
                        id
                        title
                    }
                }
            }
          }
        }
    }}
        """
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
0 Likes
_JB
Shopify Staff
Shopify Staff
750 86 163

This is an accepted solution.

Hey @jbpapp,

Your query should work, you just need to remove the first: 10 from the collections connection. This works for me:

 {
  products (query: "published_status:published") {
    edges {
      node {
        id
        title
        collections {
          edges {
            node {
              id
              title
            }
          }
        }
      }
    }
  }
}

JB | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes
jbpapp
New Member
4 0 0

Hey @_JB ,

Thank you for your reply.

Unfortunately it is not working (or I'm missing something).
In the response the first element is a product, the second element it's collection, but the next elements are products again.

Eg:

{"id":"gid:\/\/shopify\/Product\/4827863187492","title":"Michael Kors SOFIE MK6560 női karóra W4"}
{"id":"gid:\/\/shopify\/Collection\/161535688740","title":"Home page","__parentId":"gid:\/\/shopify\/Product\/4827863187492"}
{"id":"gid:\/\/shopify\/Product\/4827863318564","title":"Michael Kors SOFIE MK3882 női karóra W4"}
{"id":"gid:\/\/shopify\/Product\/4827863416868","title":"Michael Kors SOFIE MK4335 női karóra W4"}

 
Do you have any idea what's wrong?

0 Likes
jbpapp
New Member
4 0 0

Ah, ok, they didn't have collections, that's why just the second element was a collection.


0 Likes
_JB
Shopify Staff
Shopify Staff
750 86 163

Hey @jbpapp,

Glad you figured it out. For yourself and anyone else reading along, I also want to point out this consideration from our bulk operations tutorial doc:

In the JSONL results, each [parent] object is followed by each of its [child] objects on a new line. The order of each connection type is preserved and all nested connections appear after their parents in the file; however, child nodes might not appear directly after their parent.

So keep in mind that even if your query shows 2 collection objects directly after one another, it's possible that the child nodes will be located further down in the file. As mentioned in the linked doc, one way to handle this is by reading the file in reverse. This way your app can collect the child nodes, and once you reach the parent node you can be sure you already have all connections.

JB | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes