Solved

Trying to get all products with their collections

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
        }
      }
    }
Accepted Solution (1)

_JB
Shopify Staff
836 100 222

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 | Solutions Engineer @ Shopify 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 4 (4)

_JB
Shopify Staff
836 100 222

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 | Solutions Engineer @ Shopify 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

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?

jbpapp
New Member
4 0 0

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


_JB
Shopify Staff
836 100 222

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 | Solutions Engineer @ Shopify 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog