Pulling all item data with nested pagination via GraphQL?

ShaneOH
Tourist
12 0 4

Hi all, I'm trying to write a function to import a user's entire product catalog into our system. This of course includes all product fields, variation fields, images, and inventory items. 

I have an initial query that looks like this:

        query ($cursor: String) {
          products(first: 10, after: $cursor) {
            edges {
              cursor
              node {
                id
                descriptionHtml
                images(first: 5) {
                  edges {
                    node {
                      id
                      transformedSrc
                    }
                  }
                }
                options {
                  name
                  position
                  values
                }
                productType
                publishedAt
                tags
                templateSuffix
                title
                metafields(first: 5) {
                  edges {
                    node {
                      id
                    }
                  }
                }
                variants(first: 5) {
                  edges {
                    node {
                      id
                      barcode
                      compareAtPrice
                      fulfillmentService {
                        id
                        inventoryManagement
                      }
                      weight
                      weightUnit
                      inventoryItem {
                        id
                        unitCost {
                          amount
                          currencyCode
                        }
                        countryCodeOfOrigin
                        harmonizedSystemCode
                        provinceCodeOfOrigin
                        tracked
                      }
                      inventoryPolicy
                      inventoryQuantity
                      metafields {
                        edges {
                          node {
                            id
                          }
                        }
                      }
                      price
                      sku
                      taxable
                      taxCode
                    }
                  }
                }
                vendor
                totalInventory
              }
            }
            pageInfo {
              hasNextPage
            }
          }
        }


As you can see we loop through products 10 at a time. However, inside products, we have images and variants, which also must be looped through (we choose 5 at a time). 

Cursor-based pagination on the parent object (products) is pretty straightforward from reading through the docs, but how do we then recursively paginate through the inner objects (variations and images) on a per-product basis if there are more than 5 in any given product?

Also, I know that bulk operations exist -- is there any way to just convert this entire query into a bulk operation, removing the need for pagination and pulling all the needed data out at once? For an import this would be fine since we don't need it on a synchronous basis. If that's not possible though I still need to figure out this pagination issue.

Any help would be much appreciated!

0 Likes
hassain
Shopify Staff (Retired)
Shopify Staff (Retired)
624 103 135

Hey @ShaneOH ,

 

To answer your question "is there any way to just convert this entire query into a bulk operation, removing the need for pagination and pulling all the needed data out at once?" --> Yes. Here is how the Bulk Operation query for your products query would look like:

 

mutation {
  bulkOperationRunQuery(
   query: """
   {
  	products {
            edges {
              node {
                id
                descriptionHtml
                images {
                  edges {
                    node {
                      id
                      transformedSrc
                    }
                  }
                }
                options {
                  name
                  position
                  values
                }
                productType
                publishedAt
                tags
                templateSuffix
                title
                metafields {
                  edges {
                    node {
                      id
                    }
                  }
                }
                variants {
                  edges {
                    node {
                      id
                      barcode
                      compareAtPrice
                      fulfillmentService {
                        id
                        inventoryManagement
                      }
                      weight
                      weightUnit
                      inventoryItem {
                        id
                        unitCost {
                          amount
                          currencyCode
                        }
                        countryCodeOfOrigin
                        harmonizedSystemCode
                        provinceCodeOfOrigin
                        tracked
                      }
                      inventoryPolicy
                      inventoryQuantity
                      metafields {
                        edges {
                          node {
                            id
                          }
                        }
                      }
                      price
                      sku
                      taxable
                      taxCode
                    }
                  }
                }
                vendor
                totalInventory
              }
            }
            
          }
  }
    """
  ) {
    bulkOperation {
      id
      status
    }
    userErrors {
      field
      message
    }
  }
}

Hassain | Developer Support Specialist @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

tahafarooqui
Tourist
8 0 2

@hassain  Is there any way to rejoin nested fields because in response, it breaks into separate objects. 

 

{"id":"gid:\/\/shopify\/Product\/1755556806746","title":"A Book bind","description":"Energize is a pure Sativa formulation designed to stimulate, excite, and keep you productive","productType":"CARTRIDGE","vendor":"ACES","totalInventory":0,"featuredImage":null,"handle":"1g-energizedurban-cart-82-89"}
{"id":"gid:\/\/shopify\/Metafield\/4680431861850","namespace":"custom_fields","key":"thc_percentage","value":"aa","__parentId":"gid:\/\/shopify\/Product\/1755556806746"}
{"id":"gid:\/\/shopify\/Metafield\/4680431894618","namespace":"custom_fields","key":"cbd_percentage","value":"bb","__parentId":"gid:\/\/shopify\/Product\/1755556806746"}
{"id":"gid:\/\/shopify\/Metafield\/4680431960154","namespace":"custom_fields","key":"strain_name","value":"Strain","__parentId":"gid:\/\/shopify\/Product\/1755556806746"}

I want response like that:

{"id":"gid:\/\/shopify\/Product\/1755556806746","title":"A Book bind","description":"Energize is a pure Sativa formulation designed to stimulate, excite, and keep you productive","productType":"CARTRIDGE","vendor":"ACES","totalInventory":0,"featuredImage":null,"handle":"1g-energizedurban-cart-82-89",
metafields:[{"id":"gid:\/\/shopify\/Metafield\/4680431861850","namespace":"custom_fields","key":"thc_percentage","value":"aa","__parentId":"gid:\/\/shopify\/Product\/1755556806746"},
{"id":"gid:\/\/shopify\/Metafield\/4680431894618","namespace":"custom_fields","key":"cbd_percentage","value":"bb","__parentId":"gid:\/\/shopify\/Product\/1755556806746"},
{"id":"gid:\/\/shopify\/Metafield\/4680431960154","namespace":"custom_fields","key":"strain_name","value":"Strain","__parentId":"gid:\/\/shopify\/Product\/1755556806746"}
]
}
0 Likes