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

add $in and $nin operators to connector schema #122

Merged
Show file tree
Hide file tree
Changes from 6 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
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This changelog documents the changes between release versions.

### Added

- Adds `_in` and `_nin` operators ([#122](https://github.com/hasura/ndc-mongodb/pull/122))

### Changed

- **BREAKING:** If `configuration.json` cannot be parsed the connector will fail to start. This change also prohibits unknown keys in that file. These changes will help to prevent typos configuration being silently ignored. ([#115](https://github.com/hasura/ndc-mongodb/pull/115))
Expand All @@ -15,6 +17,26 @@ This changelog documents the changes between release versions.
- Fixes for filtering by complex predicate that references variables, or field names that require escaping ([#111](https://github.com/hasura/ndc-mongodb/pull/111))
- Escape names if necessary instead of failing when joining relationship on field names with special characters ([#113](https://github.com/hasura/ndc-mongodb/pull/113))

#### `_in` and `_nin`

These operators compare document values for equality against a given set of
options. `_in` matches documents where one of the given values matches, `_nin` matches
documents where none of the given values matches. For example this query selects
movies that are rated either "G" or "TV-G":

```graphql
query {
movies(
where: { rated: { _in: ["G", "TV-G"] } }
order_by: { id: Asc }
limit: 5
) {
title
rated
}
}
```

## [1.3.0] - 2024-10-01

### Fixed
Expand Down
23 changes: 23 additions & 0 deletions crates/integration-tests/src/tests/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@ use ndc_test_helpers::{binop, field, query, query_request, target, variable};

use crate::{connector::Connector, graphql_query, run_connector_query};

#[tokio::test]
async fn filters_using_in_operator() -> anyhow::Result<()> {
assert_yaml_snapshot!(
graphql_query(
r#"
query {
movies(
where: { rated: { _in: ["G", "TV-G"] } }
order_by: { id: Asc }
limit: 5
) {
title
rated
}
}
"#
)
.run()
.await?
);
Ok(())
}

#[tokio::test]
async fn filters_on_extended_json_using_string_comparison() -> anyhow::Result<()> {
assert_yaml_snapshot!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/integration-tests/src/tests/filtering.rs
expression: "graphql_query(r#\"\n query {\n movies(\n where: { rated: { _in: [\"G\", \"TV-G\"] } }\n order_by: { id: Asc }\n limit: 5\n ) {\n title\n rated\n }\n }\n \"#).run().await?"
---
data:
movies:
- title: The Great Train Robbery
rated: TV-G
- title: A Corner in Wheat
rated: G
- title: From Hand to Mouth
rated: TV-G
- title: One Week
rated: TV-G
- title: The Devil to Pay!
rated: TV-G
errors: ~
3 changes: 3 additions & 0 deletions crates/mongodb-agent-common/src/comparison_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum ComparisonFunction {
NotEqual,

In,
NotIn,

Regex,
/// case-insensitive regex
Expand All @@ -36,6 +37,7 @@ impl ComparisonFunction {
C::Equal => "_eq",
C::NotEqual => "_neq",
C::In => "_in",
C::NotIn => "_nin",
C::Regex => "_regex",
C::IRegex => "_iregex",
}
Expand All @@ -49,6 +51,7 @@ impl ComparisonFunction {
C::GreaterThanOrEqual => "$gte",
C::Equal => "$eq",
C::In => "$in",
C::NotIn => "$nin",
C::NotEqual => "$ne",
C::Regex => "$regex",
C::IRegex => "$regex",
Expand Down
39 changes: 30 additions & 9 deletions crates/mongodb-agent-common/src/scalar_types_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,13 @@ fn bson_comparison_operators(
bson_scalar_type: BsonScalarType,
) -> BTreeMap<ComparisonOperatorName, ComparisonOperatorDefinition> {
comparison_operators(bson_scalar_type)
.map(|(comparison_fn, arg_type)| {
.map(|(comparison_fn, argument_type)| {
let fn_name = comparison_fn.graphql_name().into();
match comparison_fn {
ComparisonFunction::Equal => (fn_name, ComparisonOperatorDefinition::Equal),
_ => (
fn_name,
ComparisonOperatorDefinition::Custom {
argument_type: bson_to_named_type(arg_type),
},
ComparisonOperatorDefinition::Custom { argument_type },
),
}
})
Expand Down Expand Up @@ -167,10 +165,27 @@ pub fn aggregate_functions(

pub fn comparison_operators(
scalar_type: BsonScalarType,
) -> impl Iterator<Item = (ComparisonFunction, BsonScalarType)> {
) -> impl Iterator<Item = (ComparisonFunction, Type)> {
iter_if(
scalar_type.is_comparable(),
[(C::Equal, scalar_type), (C::NotEqual, scalar_type)].into_iter(),
[
(C::Equal, bson_to_named_type(scalar_type)),
(C::NotEqual, bson_to_named_type(scalar_type)),
(
C::In,
Type::Array {
element_type: Box::new(bson_to_named_type(scalar_type)),
},
),
(
C::NotIn,
Type::Array {
element_type: Box::new(bson_to_named_type(scalar_type)),
},
),
(C::NotEqual, bson_to_named_type(scalar_type)),
]
.into_iter(),
)
.chain(iter_if(
scalar_type.is_orderable(),
Expand All @@ -181,11 +196,17 @@ pub fn comparison_operators(
C::GreaterThanOrEqual,
]
.into_iter()
.map(move |op| (op, scalar_type)),
.map(move |op| (op, bson_to_named_type(scalar_type))),
))
.chain(match scalar_type {
S::String => Box::new([(C::Regex, S::String), (C::IRegex, S::String)].into_iter()),
_ => Box::new(std::iter::empty()) as Box<dyn Iterator<Item = (C, S)>>,
S::String => Box::new(
[
(C::Regex, bson_to_named_type(S::String)),
(C::IRegex, bson_to_named_type(S::String)),
]
.into_iter(),
),
_ => Box::new(std::iter::empty()) as Box<dyn Iterator<Item = (C, Type)>>,
})
}

Expand Down
Loading
Loading