Skip to content

Commit 1332ab2

Browse files
authored
fix: empty array handling (#69)
1 parent ad302f7 commit 1332ab2

File tree

5 files changed

+213
-35
lines changed

5 files changed

+213
-35
lines changed

__tests__/fixtures/queries/connections-filter.graphql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ query {
1010
int_greaterThan_2: allFilterables(filter: { int: { greaterThan: 2 } }) { ...intConnection }
1111
int_greaterThanOrEqualTo_2: allFilterables(filter: { int: { greaterThanOrEqualTo: 2 } }) { ...intConnection }
1212
int_in_2_3: allFilterables(filter: { int: { in: [2, 3] } }) { ...intConnection }
13+
int_in_empty: allFilterables(filter: { int: { in: [] } }) { ...intConnection }
1314
int_notIn_2_3: allFilterables(filter: { int: { notIn: [2, 3] } }) { ...intConnection }
15+
int_notIn_empty: allFilterables(filter: { int: { notIn: [] } }) { ...intConnection }
1416
string_contains_t: allFilterables(filter: { string: { includes: "t" } }) { ...stringConnection }
1517
string_containsInsensitive_t: allFilterables(filter: { string: { includesInsensitive: "t" } }) { ...stringConnection }
1618
string_notContains_t: allFilterables(filter: { string: { notIncludes: "t" } }) { ...stringConnection }
@@ -41,20 +43,29 @@ query {
4143
not_int_equalTo_2_or_string_endsWith_t: allFilterables(filter: { not: { or: [{ int: { equalTo: 2 } }, { string: { endsWith: "t" } }] } }) { ...stringAndIntConnection }
4244
computed_equalTo_test_computed: allFilterables(filter: { computed: { equalTo: "test computed" } }) { ...computedConnection }
4345
computedIntArray_equalTo_2_20: allFilterables(filter: { computedIntArray: { equalTo: [2, 20] } }) { ...computedConnection }
46+
computedIntArray_equalTo_empty: allFilterables(filter: { computedIntArray: { equalTo: [] } }) { ...computedConnection }
4447
jsonb_contains_string: allFilterables(filter: { jsonb: { contains: "{\"string\":\"test\"}" } }) { ...jsonbConnection }
4548
jsonb_contains_int: allFilterables(filter: { jsonb: { contains: "{\"int\":2}" } }) { ...jsonbConnection }
4649
jsonb_contains_boolean: allFilterables(filter: { jsonb: { contains: "{\"boolean\":true}" } }) { ...jsonbConnection }
4750
jsonb_containedBy_object: allFilterables(filter: { jsonb: { containedBy: "{\"string\":\"test\",\"int\":4,\"boolean\":false,\"foo\":\"bar\"}" } }) { ...jsonbConnection }
4851
intArray_isNull_true: allFilterables(filter: { intArray: { isNull: true } }) { ...intArrayConnection }
4952
intArray_isNull_false: allFilterables(filter: { intArray: { isNull: false } }) { ...intArrayConnection }
5053
intArray_distinctFrom_2_20: allFilterables(filter: { intArray: { distinctFrom: [2, 20] } }) { ...intArrayConnection }
54+
intArray_distinctFrom_empty: allFilterables(filter: { intArray: { distinctFrom: [] } }) { ...intArrayConnection }
5155
intArray_notDistinctFrom_2_20: allFilterables(filter: { intArray: { notDistinctFrom: [2, 20] } }) { ...intArrayConnection }
56+
intArray_notDistinctFrom_empty: allFilterables(filter: { intArray: { notDistinctFrom: [] } }) { ...intArrayConnection }
5257
intArray_equalTo_2_20: allFilterables(filter: { intArray: { equalTo: [2, 20] } }) { ...intArrayConnection }
58+
intArray_equalTo_empty: allFilterables(filter: { intArray: { equalTo: [] } }) { ...intArrayConnection }
5359
intArray_notEqualTo_2_20: allFilterables(filter: { intArray: { notEqualTo: [2, 20] } }) { ...intArrayConnection }
60+
intArray_notEqualTo_empty: allFilterables(filter: { intArray: { notEqualTo: [] } }) { ...intArrayConnection }
5461
intArray_lessThan_2_20: allFilterables(filter: { intArray: { lessThan: [2, 20] } }) { ...intArrayConnection }
62+
intArray_lessThan_empty: allFilterables(filter: { intArray: { lessThan: [] } }) { ...intArrayConnection }
5563
intArray_lessThanOrEqualTo_2_20: allFilterables(filter: { intArray: { lessThanOrEqualTo: [2, 20] } }) { ...intArrayConnection }
64+
intArray_lessThanOrEqualTo_empty: allFilterables(filter: { intArray: { lessThanOrEqualTo: [] } }) { ...intArrayConnection }
5665
intArray_greaterThan_2_20: allFilterables(filter: { intArray: { greaterThan: [2, 20] } }) { ...intArrayConnection }
66+
intArray_greaterThan_empty: allFilterables(filter: { intArray: { greaterThan: [] } }) { ...intArrayConnection }
5767
intArray_greaterThanOrEqualTo_2_20: allFilterables(filter: { intArray: { greaterThanOrEqualTo: [2, 20] } }) { ...intArrayConnection }
68+
intArray_greaterThanOrEqualTo_empty: allFilterables(filter: { intArray: { greaterThanOrEqualTo: [] } }) { ...intArrayConnection }
5869
intArray_anyEqualTo_2: allFilterables(filter: { intArray: { anyEqualTo: 2 } }) { ...intArrayConnection }
5970
intArray_anyNotEqualTo_2: allFilterables(filter: { intArray: { anyNotEqualTo: 2 } }) { ...intArrayConnection }
6071
intArray_anyLessThan_2: allFilterables(filter: { intArray: { anyLessThan: 2 } }) { ...intArrayConnection }

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

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ Object {
173173
},
174174
],
175175
},
176+
"computedIntArray_equalTo_empty": Object {
177+
"nodes": Array [],
178+
},
176179
"computed_equalTo_test_computed": Object {
177180
"nodes": Array [
178181
Object {
@@ -429,6 +432,42 @@ Object {
429432
},
430433
],
431434
},
435+
"intArray_distinctFrom_empty": Object {
436+
"nodes": Array [
437+
Object {
438+
"id": 1,
439+
"intArray": Array [
440+
1,
441+
10,
442+
],
443+
},
444+
Object {
445+
"id": 2,
446+
"intArray": Array [
447+
2,
448+
20,
449+
],
450+
},
451+
Object {
452+
"id": 3,
453+
"intArray": Array [
454+
3,
455+
30,
456+
],
457+
},
458+
Object {
459+
"id": 4,
460+
"intArray": Array [
461+
4,
462+
40,
463+
],
464+
},
465+
Object {
466+
"id": 5,
467+
"intArray": null,
468+
},
469+
],
470+
},
432471
"intArray_equalTo_2_20": Object {
433472
"nodes": Array [
434473
Object {
@@ -440,6 +479,9 @@ Object {
440479
},
441480
],
442481
},
482+
"intArray_equalTo_empty": Object {
483+
"nodes": Array [],
484+
},
443485
"intArray_greaterThanOrEqualTo_2_20": Object {
444486
"nodes": Array [
445487
Object {
@@ -465,6 +507,38 @@ Object {
465507
},
466508
],
467509
},
510+
"intArray_greaterThanOrEqualTo_empty": Object {
511+
"nodes": Array [
512+
Object {
513+
"id": 1,
514+
"intArray": Array [
515+
1,
516+
10,
517+
],
518+
},
519+
Object {
520+
"id": 2,
521+
"intArray": Array [
522+
2,
523+
20,
524+
],
525+
},
526+
Object {
527+
"id": 3,
528+
"intArray": Array [
529+
3,
530+
30,
531+
],
532+
},
533+
Object {
534+
"id": 4,
535+
"intArray": Array [
536+
4,
537+
40,
538+
],
539+
},
540+
],
541+
},
468542
"intArray_greaterThan_2_20": Object {
469543
"nodes": Array [
470544
Object {
@@ -483,6 +557,38 @@ Object {
483557
},
484558
],
485559
},
560+
"intArray_greaterThan_empty": Object {
561+
"nodes": Array [
562+
Object {
563+
"id": 1,
564+
"intArray": Array [
565+
1,
566+
10,
567+
],
568+
},
569+
Object {
570+
"id": 2,
571+
"intArray": Array [
572+
2,
573+
20,
574+
],
575+
},
576+
Object {
577+
"id": 3,
578+
"intArray": Array [
579+
3,
580+
30,
581+
],
582+
},
583+
Object {
584+
"id": 4,
585+
"intArray": Array [
586+
4,
587+
40,
588+
],
589+
},
590+
],
591+
},
486592
"intArray_isNull_false": Object {
487593
"nodes": Array [
488594
Object {
@@ -541,6 +647,9 @@ Object {
541647
},
542648
],
543649
},
650+
"intArray_lessThanOrEqualTo_empty": Object {
651+
"nodes": Array [],
652+
},
544653
"intArray_lessThan_2_20": Object {
545654
"nodes": Array [
546655
Object {
@@ -552,6 +661,9 @@ Object {
552661
},
553662
],
554663
},
664+
"intArray_lessThan_empty": Object {
665+
"nodes": Array [],
666+
},
555667
"intArray_notDistinctFrom_2_20": Object {
556668
"nodes": Array [
557669
Object {
@@ -563,6 +675,9 @@ Object {
563675
},
564676
],
565677
},
678+
"intArray_notDistinctFrom_empty": Object {
679+
"nodes": Array [],
680+
},
566681
"intArray_notEqualTo_2_20": Object {
567682
"nodes": Array [
568683
Object {
@@ -588,6 +703,38 @@ Object {
588703
},
589704
],
590705
},
706+
"intArray_notEqualTo_empty": Object {
707+
"nodes": Array [
708+
Object {
709+
"id": 1,
710+
"intArray": Array [
711+
1,
712+
10,
713+
],
714+
},
715+
Object {
716+
"id": 2,
717+
"intArray": Array [
718+
2,
719+
20,
720+
],
721+
},
722+
Object {
723+
"id": 3,
724+
"intArray": Array [
725+
3,
726+
30,
727+
],
728+
},
729+
Object {
730+
"id": 4,
731+
"intArray": Array [
732+
4,
733+
40,
734+
],
735+
},
736+
],
737+
},
591738
"int_equalTo_2_and_string_endsWith_t": Object {
592739
"nodes": Array [
593740
Object {
@@ -648,6 +795,9 @@ Object {
648795
},
649796
],
650797
},
798+
"int_in_empty": Object {
799+
"nodes": Array [],
800+
},
651801
"int_lessThanOrEqualTo_2": Object {
652802
"nodes": Array [
653803
Object {
@@ -680,6 +830,30 @@ Object {
680830
},
681831
],
682832
},
833+
"int_notIn_empty": Object {
834+
"nodes": Array [
835+
Object {
836+
"id": 1,
837+
"string": "TEST",
838+
},
839+
Object {
840+
"id": 2,
841+
"string": "Test",
842+
},
843+
Object {
844+
"id": 3,
845+
"string": "tEST",
846+
},
847+
Object {
848+
"id": 4,
849+
"string": "test",
850+
},
851+
Object {
852+
"id": 5,
853+
"string": null,
854+
},
855+
],
856+
},
683857
"jsonb_containedBy_object": Object {
684858
"nodes": Array [
685859
Object {

src/PgConnectionArgFilterColumnsPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ module.exports = function PgConnectionArgFilterColumnsPlugin(builder) {
8484
attr.type,
8585
attr.typeModifier,
8686
fieldName,
87-
queryBuilder
87+
queryBuilder,
88+
sourceAlias
8889
);
8990
})
9091
.filter(x => x != null);

src/PgConnectionArgFilterComputedColumnsPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ module.exports = function PgConnectionArgFilterComputedColumnsPlugin(builder) {
110110
procReturnType,
111111
null,
112112
fieldName,
113-
queryBuilder
113+
queryBuilder,
114+
sourceAlias
114115
);
115116
})
116117
.filter(x => x != null);

src/PgConnectionArgFilterPlugin.js

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -264,37 +264,6 @@ module.exports = function PgConnectionArgFilterPlugin(
264264
);
265265
};
266266

267-
const sqlValueFromInput = (input, inputResolver, pgType, pgTypeModifier) =>
268-
Array.isArray(input)
269-
? pgType.isPgArray
270-
? sql.query`${gql2pg(
271-
(inputResolver && inputResolver(input)) || input,
272-
pgType,
273-
pgTypeModifier
274-
)}`
275-
: sql.query`(${sql.join(
276-
input.map(
277-
i =>
278-
sql.query`${gql2pg(
279-
(inputResolver && inputResolver(i)) || i,
280-
pgType,
281-
pgTypeModifier
282-
)}`
283-
),
284-
","
285-
)})`
286-
: pgType.isPgArray
287-
? sql.query`${gql2pg(
288-
(inputResolver && inputResolver(input)) || input,
289-
pgType.arrayItemType,
290-
pgTypeModifier
291-
)}`
292-
: sql.query`${gql2pg(
293-
(inputResolver && inputResolver(input)) || input,
294-
pgType,
295-
pgTypeModifier
296-
)}`;
297-
298267
const resolveWhereComparison = (
299268
sqlIdentifier,
300269
operatorName,
@@ -321,10 +290,32 @@ module.exports = function PgConnectionArgFilterPlugin(
321290
if (input == null && !operator.options.processNull) {
322291
return null;
323292
}
324-
const inputResolver = operator.options.inputResolver;
293+
294+
const sqlValueFromInput = (input, pgType, pgTypeModifier) =>
295+
sql.query`${gql2pg(
296+
(operator.options.inputResolver &&
297+
operator.options.inputResolver(input)) ||
298+
input,
299+
pgType,
300+
pgTypeModifier
301+
)}`;
302+
303+
// TODO: Remove `resolveWithRawInput` option before v1.0.0
325304
const sqlValue = operator.options.resolveWithRawInput
326305
? input
327-
: sqlValueFromInput(input, inputResolver, pgType, pgTypeModifier);
306+
: Array.isArray(input)
307+
? pgType.isPgArray
308+
? sqlValueFromInput(input, pgType, pgTypeModifier)
309+
: input.length === 0
310+
? sql.query`(select ${sqlIdentifier} limit 0)`
311+
: sql.query`(${sql.join(
312+
input.map(i => sqlValueFromInput(i, pgType, pgTypeModifier)),
313+
","
314+
)})`
315+
: pgType.isPgArray
316+
? sqlValueFromInput(input, pgType.arrayItemType, pgTypeModifier)
317+
: sqlValueFromInput(input, pgType, pgTypeModifier);
318+
328319
return operator.resolveWhereClause(
329320
sqlIdentifier,
330321
sqlValue,

0 commit comments

Comments
 (0)