Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql: fields of object should be ANDed always #4171

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions graphql/src/store/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,23 @@ fn build_list_filter_from_value(
schema: &ApiSchema,
value: &r::Value,
) -> Result<Vec<EntityFilter>, QueryExecutionError> {
// We have object like this
// { or: [{ name: \"John\", id: \"m1\" }, { mainBand: \"b2\" }] }
return match value {
r::Value::List(list) => Ok(list
.iter()
.map(|item| {
// It is each filter in the object
// { name: \"John\", id: \"m1\" }
// the fields within the object are ANDed together
return match item {
r::Value::Object(object) => {
Ok(build_filter_from_object(entity, object, schema)?)
}
r::Value::Object(object) => Ok(EntityFilter::And(build_filter_from_object(
entity, object, schema,
)?)),
_ => Err(QueryExecutionError::InvalidFilterError),
};
})
.collect::<Result<Vec<Vec<EntityFilter>>, QueryExecutionError>>()?
// Flatten all different EntityFilters into one list
.into_iter()
.flatten()
.collect::<Vec<EntityFilter>>()),
.collect::<Result<Vec<EntityFilter>, QueryExecutionError>>()?),
_ => Err(QueryExecutionError::InvalidFilterError),
};
}
Expand Down
22 changes: 21 additions & 1 deletion graphql/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,7 @@ fn deterministic_error() {
fn can_query_with_or_filter() {
const QUERY: &str = "
query {
musicians(where: { or: [{ name: \"John\", id: \"m2\" }] }) {
musicians(where: { or: [{ name: \"John\" }, { id: \"m2\" }] }) {
name
id
}
Expand All @@ -2147,6 +2147,26 @@ fn can_query_with_or_filter() {
})
}

#[test]
fn can_query_with_or_filter_fields_always_and() {
const QUERY: &str = "
query {
musicians(where: { or: [{ name: \"John\", id: \"m2\" }] }) {
name
id
}
}
";

run_query(QUERY, |result, _| {
let exp = object! {
musicians: r::Value::List(vec![]),
};
let data = extract_data!(result).unwrap();
assert_eq!(data, exp);
})
}

#[test]
fn can_query_with_and_filter() {
const QUERY: &str = "
Expand Down