Skip to content

Commit

Permalink
Merge pull request #351 from robertn702/string-regex-filters
Browse files Browse the repository at this point in the history
String regex filters
  • Loading branch information
olirice authored Apr 25, 2023
2 parents f214f94 + 759360a commit 82c28fd
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 14 deletions.
30 changes: 17 additions & 13 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ Where the `<Table>Filter` type enumerates filterable fields and their associated
startsWith: String
like: String
ilike: String
regex: String
iregex: String
}
```

Expand All @@ -501,19 +503,21 @@ Where the `<Table>Filter` type enumerates filterable fields and their associated
The following list shows the operators that may be available on `<Type>Filter` types.


| Operator | Description |
| ----------- | ------------------------- |
| eq | Equal To |
| neq | Not Equal To |
| gt | Greater Than |
| gte | Greater Than Or Equal To |
| in | Contained by Value List |
| lt | Less Than |
| lte | Less Than Or Equal To |
| is | Null or Not Null |
| startsWith | `String` starts with prefix |
| like | Case Sensitive `String` Pattern Match. '%' as wildcard |
| ilike | Case Snsensitive `String` Pattern Match. '%' as wildcard |
| Operator | Description |
| ----------- | -------------------------------------------------|
| eq | Equal To |
| neq | Not Equal To |
| gt | Greater Than |
| gte | Greater Than Or Equal To |
| in | Contained by Value List |
| lt | Less Than |
| lte | Less Than Or Equal To |
| is | Null or Not Null |
| startsWith | Starts with prefix |
| like | Pattern Match. '%' as wildcard |
| ilike | Pattern Match. '%' as wildcard. Case Insensitive |
| regex | POSIX Regular Expression Match |
| iregex | POSIX Regular Expression Match. Case Insensitive |

Not all operators are available on every `<Type>Filter` type. For example, `UUIDFilter` only supports `eq` and `neq` because `UUID`s are not ordered.

Expand Down
2 changes: 2 additions & 0 deletions docs/assets/demo_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,8 @@ input StringFilter {
startsWith: String
like: String
ilike: String
regex: String
iregex: String
}

scalar Time
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
- bugfix: Unknown types are represented in GraphQL schema as `Opaque` rather than `String`
- bugfix: PostgreSQL type modifiers, e.g. char(n), no longer truncate excess text
- bugfix: Creating a new enum variant between existing variants no longer errors

## master
- feature: `String` type filters support `regex`, `iregex`
12 changes: 11 additions & 1 deletion src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3011,6 +3011,8 @@ pub enum FilterOp {
StartsWith,
Like,
ILike,
RegEx,
IRegEx,
}

impl ToString for FilterOp {
Expand All @@ -3028,6 +3030,8 @@ impl ToString for FilterOp {
Self::StartsWith => "startsWith",
Self::Like => "like",
Self::ILike => "ilike",
Self::RegEx => "regex",
Self::IRegEx => "iregex",
}
.to_string()
}
Expand All @@ -3050,6 +3054,8 @@ impl FromStr for FilterOp {
"startsWith" => Ok(Self::StartsWith),
"like" => Ok(Self::Like),
"ilike" => Ok(Self::ILike),
"regex" => Ok(Self::RegEx),
"iregex" => Ok(Self::IRegEx),
_ => Err("Invalid filter operation".to_string()),
}
}
Expand Down Expand Up @@ -3133,6 +3139,8 @@ impl ___Type for FilterTypeType {
FilterOp::StartsWith,
FilterOp::Like,
FilterOp::ILike,
FilterOp::RegEx,
FilterOp::IRegEx,
],
Scalar::BigInt => vec![
FilterOp::Equal,
Expand Down Expand Up @@ -3205,7 +3213,9 @@ impl ___Type for FilterTypeType {
| FilterOp::LessThanEqualTo
| FilterOp::StartsWith
| FilterOp::Like
| FilterOp::ILike => __InputValue {
| FilterOp::ILike
| FilterOp::RegEx
| FilterOp::IRegEx => __InputValue {
name_: op.to_string(),
type_: __Type::Scalar(scalar.clone()),
description: None,
Expand Down
2 changes: 2 additions & 0 deletions src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@ impl FilterBuilderElem {
FilterOp::StartsWith => "^@",
FilterOp::Like => "like",
FilterOp::ILike => "ilike",
FilterOp::RegEx => "~",
FilterOp::IRegEx => "~*",
FilterOp::Is => {
return Err("Error transpiling Is filter".to_string());
}
Expand Down
20 changes: 20 additions & 0 deletions test/expected/resolve_graphiql_schema.out

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions test/expected/string_filters.out
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,69 @@ begin;
}
(1 row)

rollback to savepoint a;
-- Filter by regex
select jsonb_pretty(
graphql.resolve($$
{
memoCollection(filter: {contents: {regex: "^F\\w+$"}}) {
edges {
node {
contents
}
}
}
}
$$)
);
jsonb_pretty
-------------------------------------------
{ +
"data": { +
"memoCollection": { +
"edges": [ +
{ +
"node": { +
"contents": "Foo"+
} +
} +
] +
} +
} +
}
(1 row)

rollback to savepoint a;
-- iregex is not case sensitive
select jsonb_pretty(
graphql.resolve($$
{
memoCollection(filter: {contents: {iregex: "^f\\w+$"}}) {
edges {
node {
contents
}
}
}
}
$$)
);
jsonb_pretty
-------------------------------------------
{ +
"data": { +
"memoCollection": { +
"edges": [ +
{ +
"node": { +
"contents": "Foo"+
} +
} +
] +
} +
} +
}
(1 row)

rollback to savepoint a;
rollback;
32 changes: 32 additions & 0 deletions test/sql/string_filters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,36 @@ begin;
);
rollback to savepoint a;

-- Filter by regex
select jsonb_pretty(
graphql.resolve($$
{
memoCollection(filter: {contents: {regex: "^F\\w+$"}}) {
edges {
node {
contents
}
}
}
}
$$)
);
rollback to savepoint a;

-- iregex is not case sensitive
select jsonb_pretty(
graphql.resolve($$
{
memoCollection(filter: {contents: {iregex: "^f\\w+$"}}) {
edges {
node {
contents
}
}
}
}
$$)
);
rollback to savepoint a;

rollback;

0 comments on commit 82c28fd

Please sign in to comment.