From a14d4f3ef16029768c2b3df5f56933f174ee1a93 Mon Sep 17 00:00:00 2001 From: Yash Jagtap Date: Sat, 28 Nov 2026 13:07:07 +0530 Subject: [PATCH] graphql: Build `and` and `or` filters from their own list The `And` and `Or` arms of `build_filter_from_object` built their filter list from the whole parent object, which flattened the lists of every key into one. A `where` with both `and` and `or` put all branches into both filters, and `_change_block` next to `or` failed with `InvalidFilterError`. Build the list from the key's own value and drop the now unused `build_list_filter_from_object`. Fixes #6809 --- graphql/src/store/query.rs | 87 +++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/graphql/src/store/query.rs b/graphql/src/store/query.rs index f893663b258..b066d9fda03 100644 --- a/graphql/src/store/query.rs +++ b/graphql/src/store/query.rs @@ -219,22 +219,6 @@ fn build_list_filter_from_value( } } -/// build a filter which has list of nested filters -fn build_list_filter_from_object( - entity: &ObjectOrInterface, - object: &Object, - schema: &InputSchema, -) -> Result, QueryExecutionError> { - Ok(object - .iter() - .map(|(_, value)| build_list_filter_from_value(entity, schema, value)) - .collect::>, QueryExecutionError>>()? - .into_iter() - // We iterate an object so all entity filters are flattened into one list - .flatten() - .collect::>()) -} - /// Parses a GraphQL input object into an EntityFilter, if present. fn build_filter_from_object( entity: &ObjectOrInterface, @@ -289,8 +273,8 @@ fn build_filter_from_object( )); } - return Ok(EntityFilter::And(build_list_filter_from_object( - entity, object, schema, + return Ok(EntityFilter::And(build_list_filter_from_value( + entity, schema, value, )?)); } Or => { @@ -300,8 +284,8 @@ fn build_filter_from_object( )); } - return Ok(EntityFilter::Or(build_list_filter_from_object( - entity, object, schema, + return Ok(EntityFilter::Or(build_list_filter_from_value( + entity, schema, value, )?)); } Child => match value { @@ -1167,6 +1151,69 @@ mod tests { } } + fn eq_list(pairs: &[(&str, &str)]) -> r::Value { + r::Value::List( + pairs + .iter() + .map(|(k, v)| { + r::Value::Object(Object::from_iter(vec![( + (*k).into(), + r::Value::String(v.to_string()), + )])) + }) + .collect(), + ) + } + + fn eq_and(k: &str, v: &str) -> EntityFilter { + EntityFilter::And(vec![EntityFilter::Equal( + k.to_string(), + Value::String(v.to_string()), + )]) + } + + #[test] + fn build_query_builds_and_and_or_from_their_own_lists() { + let query_field = default_field_with( + "where", + r::Value::Object(Object::from_iter(vec![ + ("and".into(), eq_list(&[("name", "A1"), ("email", "A2")])), + ("or".into(), eq_list(&[("name", "B1"), ("email", "B2")])), + ])), + ); + assert_eq!( + query(&query_field).filter, + Some(EntityFilter::And(vec![ + EntityFilter::And(vec![eq_and("name", "A1"), eq_and("email", "A2")]), + EntityFilter::Or(vec![eq_and("name", "B1"), eq_and("email", "B2")]), + ])) + ); + } + + #[test] + fn build_query_allows_change_block_next_to_or() { + let query_field = default_field_with( + "where", + r::Value::Object(Object::from_iter(vec![ + ( + "_change_block".into(), + r::Value::Object(Object::from_iter(vec![( + "number_gte".into(), + r::Value::Int(10), + )])), + ), + ("or".into(), eq_list(&[("name", "B1"), ("email", "B2")])), + ])), + ); + assert_eq!( + query(&query_field).filter, + Some(EntityFilter::And(vec![ + EntityFilter::ChangeBlockGte(10), + EntityFilter::Or(vec![eq_and("name", "B1"), eq_and("email", "B2")]), + ])) + ); + } + #[test] fn build_query_detects_invalid_or_filter_structure_with_operators() { // Test that column filters with operators (like name_gt) are also detected