Skip to content

Commit 88d571e

Browse files
authored
fix: Add support for EQ,NE predicates for nullable scalar type values (#301)
* fix: Add support for EQ,NE predicates with null values * fix: add more tests * fix: apply null value predicate only for scalar values
1 parent 12f606d commit 88d571e

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/JpaPredicateBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,15 @@ private Predicate getTypedPredicate(From<?,?> from, Path<?> field, PredicateFilt
729729

730730
if (type.isPrimitive())
731731
type = PRIMITIVES_TO_WRAPPERS.get(type);
732+
733+
if (NullValue.class.isInstance(value) && WRAPPERS_TO_PRIMITIVES.get(type) != null) {
734+
if (criterias.contains(Criteria.EQ)) {
735+
return cb.isNull(field);
736+
} else if (criterias.contains(Criteria.NE)) {
737+
return cb.isNotNull(field);
738+
}
739+
}
740+
732741
if (type.equals(String.class)) {
733742
return getStringPredicate((Path<String>)field, filter);
734743
}

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/GraphQLExecutorTests.java

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,37 @@ public void queryAuthorBooksByAlliasesWithInlineWhereSearch() {
559559
assertThat(result.toString()).isEqualTo(expected);
560560
}
561561

562+
@Test
563+
public void queryAuthorBooksWithNotNullFalseId() {
564+
//given
565+
String query = "query { "
566+
+ "Authors(" +
567+
" where: {" +
568+
" books: {" +
569+
" id: {NOT_NULL: false}" +
570+
" }" +
571+
" }" +
572+
" ) {" +
573+
" select {" +
574+
" id" +
575+
" name" +
576+
" books {" +
577+
" id" +
578+
" title" +
579+
" genre" +
580+
" }" +
581+
" }" +
582+
" }"
583+
+ "}";
584+
585+
String expected = "{Authors={select=[{id=8, name=Igor Dianov, books=[]}]}}";
586+
587+
//when
588+
Object result = executor.execute(query).getData();
589+
590+
// then
591+
assertThat(result.toString()).isEqualTo(expected);
592+
}
562593
@Test
563594
public void queryAuthorBooksWithIsNullId() {
564595
//given
@@ -591,6 +622,109 @@ public void queryAuthorBooksWithIsNullId() {
591622
assertThat(result.toString()).isEqualTo(expected);
592623
}
593624

625+
@Test
626+
public void queryAuthorBooksWithEQNullId() {
627+
//given
628+
String query = "query { "
629+
+ "Authors(" +
630+
" where: {" +
631+
" books: {" +
632+
" id: {EQ: null}" +
633+
" }" +
634+
" }" +
635+
" ) {" +
636+
" select {" +
637+
" id" +
638+
" name" +
639+
" books {" +
640+
" id" +
641+
" title" +
642+
" genre" +
643+
" }" +
644+
" }" +
645+
" }"
646+
+ "}";
647+
648+
String expected = "{Authors={select=[{id=8, name=Igor Dianov, books=[]}]}}";
649+
650+
//when
651+
Object result = executor.execute(query).getData();
652+
653+
// then
654+
assertThat(result.toString()).isEqualTo(expected);
655+
}
656+
657+
@Test
658+
public void queryAuthorBooksWithNENullId() {
659+
//given
660+
String query = "query { "
661+
+ "Authors(" +
662+
" where: {" +
663+
" books: {" +
664+
" id: {NE: null}" +
665+
" }" +
666+
" }" +
667+
" ) {" +
668+
" select {" +
669+
" id" +
670+
" name" +
671+
" books {" +
672+
" id" +
673+
" title" +
674+
" genre" +
675+
" }" +
676+
" }" +
677+
" }"
678+
+ "}";
679+
680+
String expected = "{Authors={select=[" +
681+
"{id=1, name=Leo Tolstoy, books=[{id=2, title=War and Peace, genre=NOVEL}, {id=3, title=Anna Karenina, genre=NOVEL}]}, " +
682+
"{id=4, name=Anton Chekhov, books=[{id=5, title=The Cherry Orchard, genre=PLAY}, {id=6, title=The Seagull, genre=PLAY}, {id=7, title=Three Sisters, genre=PLAY}]}" +
683+
"]}}";
684+
685+
//when
686+
Object result = executor.execute(query).getData();
687+
688+
// then
689+
assertThat(result.toString()).isEqualTo(expected);
690+
}
691+
692+
693+
@Test
694+
public void queryAuthorBooksWithNOT_NULLId() {
695+
//given
696+
String query = "query { "
697+
+ "Authors(" +
698+
" where: {" +
699+
" books: {" +
700+
" id: {NOT_NULL: true}" +
701+
" }" +
702+
" }" +
703+
" ) {" +
704+
" select {" +
705+
" id" +
706+
" name" +
707+
" books {" +
708+
" id" +
709+
" title" +
710+
" genre" +
711+
" }" +
712+
" }" +
713+
" }"
714+
+ "}";
715+
716+
String expected = "{Authors={select=[" +
717+
"{id=1, name=Leo Tolstoy, books=[{id=2, title=War and Peace, genre=NOVEL}, {id=3, title=Anna Karenina, genre=NOVEL}]}, " +
718+
"{id=4, name=Anton Chekhov, books=[{id=5, title=The Cherry Orchard, genre=PLAY}, {id=6, title=The Seagull, genre=PLAY}, {id=7, title=Three Sisters, genre=PLAY}]}" +
719+
"]}}";
720+
721+
//when
722+
Object result = executor.execute(query).getData();
723+
724+
// then
725+
assertThat(result.toString()).isEqualTo(expected);
726+
}
727+
594728
@Test
595729
public void queryBooksAuthorWithExplictOptionalTrue() {
596730
//given

0 commit comments

Comments
 (0)