graphql-ce icon indicating copy to clipboard operation
graphql-ce copied to clipboard

Graphql OR condition not working

Open ksz2013 opened this issue 7 years ago • 11 comments

Preconditions (*)

Clean Magento 2.3

  1. Simple core graphql request , product filtering
  2. Json used for request { products( filter: { or: { price: {gt:"1"} name: {like: "%Bag"} } } pageSize: 25 sort: { price: DESC } ) { total_count items { name sku price { regularPrice { amount { value currency } } } } page_info { page_size current_page } } }

Expected result (*)

1.This should return all products where price is greater then 1, or name ends with Bag (using OR condition)

Actual result (*)

  1. This does not make an OR condition, it is actually an AND contdition

The filtering works properly if instead of the price we use a different attribute, but with price AND logic is taken in consideration. Is there a limitation with price filtering?

ksz2013 avatar Jan 23 '19 11:01 ksz2013

I'm also interested in this topic

rusdragos avatar Jan 23 '19 12:01 rusdragos

have you tried setting the or condition in an array, not object ?

cylosh avatar Jan 23 '19 13:01 cylosh

Reason - Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor::addFilterGroupToCollection() function check getCustomFilterForField which list inject from app/code/Magento/CatalogGraphQl/etc/graphql/di.xml -> customFilters(line: 67) virtual type.

when use price clause in the filter then compare with custom filter list('price') and skip to add to or clause. instead of that its add to the where clause in function Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductPriceFilter::apply(). selection_005 selection_006 selection_004

dominicfernando avatar Jan 25 '19 08:01 dominicfernando

Hi @dominicfernando ,

Is this a bug, that will be fixed, or price cannot be used with OR condition ?

Thanks,

ksz2013 avatar Jan 30 '19 11:01 ksz2013

Hi @ksz2013,

In my point of view, this is a bug. and also we can't use price, min_price, max_price, and category_id with OR condition.

This functionality needs to refactor.

Eg. When the filter has the price, min_price, max_price, and category_id then

#1 - Do the needful like sql joins in Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductPriceFilter::apply()) but not add to sql to where clause

#2: Instead of that add to the Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor::addFilterGroupToCollection() => addfieldToFilter()

dominicfernando avatar Jan 30 '19 13:01 dominicfernando

Hi, @ksz2013 .

It is a bug indeed, but I think there are no possible stable solutions for that without architectural changes.

Any fields which are not attributes (price, min_price and max_price are stored in price index table) cannot be applied using OR condition.

Partially this problem is related to https://github.com/magento/magento2/issues/4060

novikor avatar Jan 31 '19 13:01 novikor

This issue needs someone who has enough time and skills to suggest new architecture.

novikor avatar Jan 31 '19 13:01 novikor

@mslabko please provide more information about plans related to OR condition Thanks

naydav avatar Jun 21 '19 18:06 naydav

Hi @ksz2013 ,

We plan to eliminate support of "OR" condition starting from 2.4 for GraphQL (https://github.com/magento-performance/architecture/blob/graphql/design-documents/graph-ql/storefront-api.md#filtration)

Moreover, currently, Magento does not have scenarios on Store Front where "OR" condition is used. Note, we still will support "OR" condition for the same attribute, e.g. color IN ("red", "blue")

Could you please describe a business case where you need support for "OR" condition?

mslabko avatar Jun 21 '19 20:06 mslabko

Hi @mslabko, I can do it instead of @ksz2013 Sometimes I need to have OR condition in cases like baby carriage

color IN ('blue', 'grey') # it is a boy so both colors a are fit BUT also I need raincoat and mosquito net (both of equipment)

Thanks

naydav avatar Jun 25 '19 14:06 naydav

@naydav , the first case is covered. For the second one, you still can do 2 requests. @maghamed what do you think regarding this case?

mslabko avatar Jun 25 '19 15:06 mslabko