Skip to content

Commit

Permalink
feat: allow scalar shorthand for in filter (#3890)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Apr 18, 2023
1 parent 0de09f5 commit c965da6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ mod filter_spec {
@r###"{"data":{"findManyUser":[{"unique":1},{"unique":2}]}}"###
);

insta::assert_snapshot!(
&user_uniques(&runner, r#"(where: { name: { in: "Bernd" }})"#).await?,
@r###"{"data":{"findManyUser":[{"unique":2}]}}"###
);

insta::assert_snapshot!(
&user_uniques(&runner, r#"(where: { NOT: { name: { in: "Bernd" } }})"#).await?,
@r###"{"data":{"findManyUser":[{"unique":1},{"unique":3},{"unique":4}]}}"###
);

Ok(())
}

Expand All @@ -158,6 +168,17 @@ mod filter_spec {
@r###"{"data":{"findManyUser":[{"unique":3},{"unique":4}]}}"###
);

insta::assert_snapshot!(
&user_uniques(&runner, r#"(where: { name: { notIn: "Bernd" }})"#).await?,
@r###"{"data":{"findManyUser":[{"unique":1},{"unique":3},{"unique":4}]}}"###
);

// NOT notIn == in
insta::assert_snapshot!(
&user_uniques(&runner, r#"(where: { NOT: { name: { notIn: "Bernd" }}})"#).await?,
@r###"{"data":{"findManyUser":[{"unique":2}]}}"###
);

Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ impl<'a> ScalarFilterParser<'a> {
PrismaValue::Null => field.equals(value),
PrismaValue::List(values) => field.is_in(values),

_ => unreachable!(), // Validation guarantees this.
val if self.reverse() => field.not_in(vec![val]),
val => field.is_in(vec![val]),
},
ConditionValue::FieldRef(field_ref) if self.reverse() => field.not_in(field_ref),
ConditionValue::FieldRef(field_ref) => field.is_in(field_ref),
Expand All @@ -119,7 +120,8 @@ impl<'a> ScalarFilterParser<'a> {
PrismaValue::Null => field.not_equals(value),
PrismaValue::List(values) => field.not_in(values),

_ => unreachable!(), // Validation guarantees this.
val if self.reverse() => field.is_in(vec![val]),
val => field.not_in(vec![val]),
},
ConditionValue::FieldRef(field_ref) if self.reverse() => field.is_in(field_ref),
ConditionValue::FieldRef(field_ref) => field.not_in(field_ref),
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,17 @@ fn inclusion_filters(
mapped_type: InputType,
nullable: bool,
) -> impl Iterator<Item = InputField> {
let input_type = InputType::list(mapped_type);
let input_type = InputType::list(mapped_type.clone());

let field_types: Vec<InputType> = if ctx.has_capability(ConnectorCapability::ScalarLists) {
let mut field_types: Vec<InputType> = if ctx.has_capability(ConnectorCapability::ScalarLists) {
input_type.with_field_ref_input(ctx)
} else {
vec![input_type]
};

// Allow for scalar shorthand too: { in: 2 } <=> { in: [2] }
field_types.push(mapped_type);

vec![
input_field(ctx, filters::IN, field_types.clone(), None)
.optional()
Expand Down

0 comments on commit c965da6

Please sign in to comment.