Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Get products by tag1 OR tag2 using graphql bulk operation

Solved

Get products by tag1 OR tag2 using graphql bulk operation

redochkaya
Shopify Partner
14 0 1

I am using the following graphql bulk operation request to get all products with tag: "TAG1" from a shopify store:

mutation {
  bulkOperationRunQuery(
   query: """
    {
      products (first: 250, query: "tag:tag1 OR tag:tag2") {
        edges {
          cursor
          node {
            id,
            tags
          }
        }
        pageInfo {
          hasNextPage
        }
      }
    }
    """
  ) {
    bulkOperation {
      id
      status
    }
    userErrors {
      field
      message
    }
  }
}

This request returns the operation id, when it is completed I download the result. However, the result contains one product only whereas there are many products with this "tag1" or "tag2".

 

Why one product only is returned?

 

The AND operator works fine however. So it seems that Shopify's GraphQL API does not directly support filtering products using the OR operator for tags in a single query. 

Accepted Solution (1)

Liam
Community Manager
3108 344 889

This is an accepted solution.

Hi Redochkaya,

 

Shopify's GraphQL API does not support the OR operator within the product filter query. The query is performed in a way that it pulls the products that contain either "tag1" or "tag2", not both.

 

However, you can use the AND operator to get all products that have both "tag1" and "tag2". The query will return only those products that have both tags.

 

If you want to get all products that have either "tag1" or "tag2", you will need to perform two separate queries and then merge the results.

 

Remember to also set the 'first' parameter to a high number if you expect to have more than the default number of products returned by the query. For example, set it to 100 if you want to retrieve up to 100 products. If you have more than 100 products, you will need to implement pagination to retrieve all products.

 

Hope this helps,

Liam | Developer Advocate @ 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

View solution in original post

Reply 1 (1)

Liam
Community Manager
3108 344 889

This is an accepted solution.

Hi Redochkaya,

 

Shopify's GraphQL API does not support the OR operator within the product filter query. The query is performed in a way that it pulls the products that contain either "tag1" or "tag2", not both.

 

However, you can use the AND operator to get all products that have both "tag1" and "tag2". The query will return only those products that have both tags.

 

If you want to get all products that have either "tag1" or "tag2", you will need to perform two separate queries and then merge the results.

 

Remember to also set the 'first' parameter to a high number if you expect to have more than the default number of products returned by the query. For example, set it to 100 if you want to retrieve up to 100 products. If you have more than 100 products, you will need to implement pagination to retrieve all products.

 

Hope this helps,

Liam | Developer Advocate @ 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