GraphQL filter by list of ids

garyrgilbert
Shopify Partner
397 40 163

 

Hi All,

 

Is there any way to use the results of a part of a query to filter the result of a second part of a query?

 

The use-case: I need to know the customs information for the inventory_items of a specific fulfillment_order of an order.  

 

How I would go about it now, would be first to get the InventoryItemids from the fulfillment order line_items, then do a query for each inventoryitem to get the customs information.  The more products in the order the more network overhead.

 

I would be ok with two queries, one to get the inventoryItemIds for the lineItems and then another to get all the customs info for the line Items, but since there is no support for an "in" operator in the shopify graphql implementation I would have to build a query string that would be very very ugly....

 

query:"id:{id} or id:{id}..." 

wouldn't it be "better" to be able to do query:"id:[{array of ids}]

 

But if wishes were horses...

 

Any help/alternatives would be appreciated.

 

Cheers,

 

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
Replies 2 (2)

ShopifyDevSup
Shopify Staff
1357 224 473

Hi @garyrgilbert,

I do agree that an In operator would be very useful for the Shopify API Search Syntax, and I will definitely be sending up feedback on your behalf to our developers that we should add this feature in the future.

That said there are a few possible workarounds you can use to get the behaviour you're looking for.

If you wish to continue using two queries to retrieve this information, you can always use an array to collect the Inventory Item Id's from the first query results, then in your app's logic you can parse the array and concatenate each element into a single string. For example if you are writing your app in Javascript, you can use the Array Prototype's join method to create a string out of an array, with a specified connector character, in this case you would want to use ' OR ' as the join's connector.
 

 

If you instead wished to use a single API call to retrieve the line item's custom's information from the Fulfillment Order, you can use the lineItems connector on the FulfillmentOrder to retrieve a list of Inventory Id's in the specific Fulfillment Order. In the same query you would also use the order field on the FulfillmentOrder object to retrieve the custom's information from each line item in the order itself.

 

Do note however this will return ALL of the line items on the order itself, so if the order has multiple FulfillmentOrders you will need to implement some additional logic in your app that will filter out the results from fulfillmentOrder.order.lineItems.variant.inventoryItem (all inventory items on the order) using the results from fulfillmentOrder.lineItems (all inventory items on a single fulfillment order).  

Here's some relevant Shopify.dev documentation that should help, and an example query you can use to start with:
 

 

{
   fulfillmentOrder(id: "gid://shopify/FulfillmentOrder/7295469912226") {

       # these are the line items on the FulfillmentOrder itself, and can be used to filter the results from the order's line items below
       lineItems(first: 50) {
           edges {
               node {
                   inventoryItemId
                   productTitle
                   variantTitle
                   sku
               }
           }
       }
       order {
           lineItems(first: 50) {
               edges {
                   node {
                       variant {
                           inventoryItem {
                               id
                               countryCodeOfOrigin
                               harmonizedSystemCode
                               provinceCodeOfOrigin
                               measurement {
                                   id
                                   weight {
                                       unit
                                       value
                                   }
                               }
                               unitCost {
                                   amount
                                   currencyCode
                               }
                           }
                       }
                   }
               }
           }
       }
   }
}

I hope this helps, and I hope you have a great day 🙂

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

garyrgilbert
Shopify Partner
397 40 163

Hi there,

 

Thanks for replying. 

 

I ended up concatenating the ids with OR.  The other method that you suggested would probably end up being more expensive (query cost) and require more post processing if the orders contained a lot of line_items and I ended up having to limit the number of returned line_items which would result in pagination of the results on top. 

 

Would of course be cool to be able to provide an array instead of concatinating, I hope the suggestion is well received by the devs.

 

cheers,

 

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution