Skip to content

Commit 36ef81f

Browse files
syndesismattbretl
authored andcommitted
Add operators for SIMILAR TO and NOT SIMILAR TO (#24)
1 parent b213cfa commit 36ef81f

File tree

6 files changed

+106
-1
lines changed

6 files changed

+106
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
.idea

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ The following filter operators are exposed by default:
8383
| NOT LIKE '...' | notLike | Scalar |
8484
| ILIKE '...' | likeInsensitive | Scalar |
8585
| NOT ILIKE '...' | notLikeInsensitive | Scalar |
86+
| SIMILAR TO '...' | similarTo | Scalar |
87+
| NOT SIMILAR TO '...' | notSimilarTo | Scalar |
8688

8789
## Examples
8890

__tests__/fixtures/queries/connections-filter.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ query {
2727
string_likeInsensitive_es: allFilterables(filter: { string: { likeInsensitive: "%es%" } }) { ...filterableConnection }
2828
string_notLike_es: allFilterables(filter: { string: { notLike: "%es%" } }) { ...filterableConnection }
2929
string_notLikeInsensitive_es: allFilterables(filter: { string: { notLikeInsensitive: "%es%" } }) { ...filterableConnection }
30+
string_similarTo_te_or_st: allFilterables(filter:{ string: { similarTo: "%(te|st)%" } }) { ...filterableConnection }
31+
string_notSimilarTo_te_or_st: allFilterables(filter:{ string: { notSimilarTo: "%(te|st)%" } }) { ...filterableConnection }
3032
int_equalTo_2_or_string_endsWith_t: allFilterables(filter: { or: [{ int: { equalTo: 2 } }, { string: { endsWith: "t" } }] }) { ...filterableConnection }
3133
int_equalTo_2_and_string_endsWith_t: allFilterables(filter: { and: [{ int: { equalTo: 2 } }, { string: { endsWith: "t" } }] }) { ...filterableConnection }
3234
boolean_null_true: allFilterables(filter: { boolean: { null: true } }) { ...filterableConnection }

__tests__/integration/__snapshots__/queries.test.js.snap

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,39 @@ Object {
17681768
},
17691769
"totalCount": 2,
17701770
},
1771+
"string_notSimilarTo_te_or_st": Object {
1772+
"edges": Array [
1773+
Object {
1774+
"cursor": "WyJwcmltYXJ5X2tleV9hc2MiLFsxXV0=",
1775+
"node": Object {
1776+
"boolean": true,
1777+
"id": 1,
1778+
"int": 1,
1779+
"numeric": "0.1",
1780+
"real": 0.1,
1781+
"string": "TEST",
1782+
},
1783+
},
1784+
Object {
1785+
"cursor": "WyJwcmltYXJ5X2tleV9hc2MiLFszXV0=",
1786+
"node": Object {
1787+
"boolean": false,
1788+
"id": 3,
1789+
"int": 3,
1790+
"numeric": "0.3",
1791+
"real": 0.3,
1792+
"string": "tEST",
1793+
},
1794+
},
1795+
],
1796+
"pageInfo": Object {
1797+
"endCursor": "WyJwcmltYXJ5X2tleV9hc2MiLFszXV0=",
1798+
"hasNextPage": false,
1799+
"hasPreviousPage": false,
1800+
"startCursor": "WyJwcmltYXJ5X2tleV9hc2MiLFsxXV0=",
1801+
},
1802+
"totalCount": 2,
1803+
},
17711804
"string_notStartsWithInsensitive_t": Object {
17721805
"edges": Array [],
17731806
"pageInfo": Object {
@@ -1888,6 +1921,39 @@ Object {
18881921
},
18891922
"totalCount": 1,
18901923
},
1924+
"string_similarTo_te_or_st": Object {
1925+
"edges": Array [
1926+
Object {
1927+
"cursor": "WyJwcmltYXJ5X2tleV9hc2MiLFsyXV0=",
1928+
"node": Object {
1929+
"boolean": true,
1930+
"id": 2,
1931+
"int": 2,
1932+
"numeric": "0.2",
1933+
"real": 0.2,
1934+
"string": "Test",
1935+
},
1936+
},
1937+
Object {
1938+
"cursor": "WyJwcmltYXJ5X2tleV9hc2MiLFs0XV0=",
1939+
"node": Object {
1940+
"boolean": false,
1941+
"id": 4,
1942+
"int": 4,
1943+
"numeric": "0.4",
1944+
"real": 0.4,
1945+
"string": "test",
1946+
},
1947+
},
1948+
],
1949+
"pageInfo": Object {
1950+
"endCursor": "WyJwcmltYXJ5X2tleV9hc2MiLFs0XV0=",
1951+
"hasNextPage": false,
1952+
"hasPreviousPage": false,
1953+
"startCursor": "WyJwcmltYXJ5X2tleV9hc2MiLFsyXV0=",
1954+
},
1955+
"totalCount": 2,
1956+
},
18911957
"string_startsWithInsensitive_t": Object {
18921958
"edges": Array [
18931959
Object {

__tests__/integration/__snapshots__/schema.test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4261,6 +4261,9 @@ input StringFilter {
42614261
# Raw SQL 'not ilike', wildcards must be present and are not escaped
42624262
notLikeInsensitive: String
42634263

4264+
# Raw SQL 'not similar to', wildcards are not escaped
4265+
notSimilarTo: String
4266+
42644267
# Checks for strings that do not start with this value. Case sensitive.
42654268
notStartsWith: String
42664269

@@ -4270,6 +4273,9 @@ input StringFilter {
42704273
# If set to true, checks for null values. If set to false, checks for non-null values.
42714274
null: Boolean
42724275

4276+
# Raw SQL 'similar to', wildcards are not escaped
4277+
similarTo: String
4278+
42734279
# Checks for strings starting with this value. Case sensitive.
42744280
startsWith: String
42754281

@@ -7758,6 +7764,9 @@ input StringFilter {
77587764
# Raw SQL 'not ilike', wildcards must be present and are not escaped
77597765
notLikeInsensitive: String
77607766

7767+
# Raw SQL 'not similar to', wildcards are not escaped
7768+
notSimilarTo: String
7769+
77617770
# Checks for strings that do not start with this value. Case sensitive.
77627771
notStartsWith: String
77637772

@@ -7767,6 +7776,9 @@ input StringFilter {
77677776
# If set to true, checks for null values. If set to false, checks for non-null values.
77687777
null: Boolean
77697778

7779+
# Raw SQL 'similar to', wildcards are not escaped
7780+
similarTo: String
7781+
77707782
# Checks for strings starting with this value. Case sensitive.
77717783
startsWith: String
77727784

src/PgConnectionArgFilterOperatorsPlugin.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,28 @@ module.exports = function PgConnectionArgFilterOperatorsPlugin(
342342
allowedFieldTypes: ["String"],
343343
}
344344
);
345+
addConnectionFilterOperator(
346+
"similarTo",
347+
"Raw SQL 'similar to', wildcards are not escaped",
348+
typeName => getTypeByName(typeName),
349+
(identifier, val) => {
350+
return sql.query`${identifier} SIMILAR TO ${val}`;
351+
},
352+
{
353+
allowedFieldTypes: ["String"],
354+
}
355+
);
356+
addConnectionFilterOperator(
357+
"notSimilarTo",
358+
"Raw SQL 'not similar to', wildcards are not escaped",
359+
typeName => getTypeByName(typeName),
360+
(identifier, val) => {
361+
return sql.query`${identifier} NOT SIMILAR TO ${val}`;
362+
},
363+
{
364+
allowedFieldTypes: ["String"],
365+
}
366+
);
345367
return _;
346368
}
347369
);

0 commit comments

Comments
 (0)