Skip to content

Commit 40a0e2d

Browse files
authored
fix(GH-193): Fixed error in GQL Queries when total or pages is included with condition where: NOT_EXISTS or EXISTS (#201)
* fix(GH-193): fixed NPE in count query with EXISTS/NOT_EXISTS criteria
1 parent 5d99c3b commit 40a0e2d

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,14 @@ private TypedQuery<Long> getCountQuery(DataFetchingEnvironment environment, Fiel
182182
CriteriaQuery<Long> query = cb.createQuery(Long.class);
183183
Root<?> root = query.from(entityType);
184184

185+
DataFetchingEnvironment queryEnvironment = DataFetchingEnvironmentBuilder.newDataFetchingEnvironment(environment)
186+
.root(query)
187+
.build();
188+
185189
query.select(cb.count(root));
186190

187191
List<Predicate> predicates = field.getArguments().stream()
188-
.map(it -> getPredicate(cb, root, null, environment, it))
192+
.map(it -> getPredicate(cb, root, null, queryEnvironment, it))
189193
.filter(it -> it != null)
190194
.collect(Collectors.toList());
191195

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

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ public void queryForAuthorsWithWhereEXISTSBooksLIKETitle() {
627627
" title: {LIKE: \"War\"}" +
628628
" }" +
629629
" }" +
630-
" }) {" +
630+
" }) {" +
631631
" select {" +
632632
" id" +
633633
" name" +
@@ -835,8 +835,82 @@ public void queryForAuthorsWithWhereBooksNOTEXISTSAuthorLIKENameLeo() {
835835

836836
// then
837837
assertThat(result.toString()).isEqualTo(expected);
838-
}
839-
838+
}
839+
840+
@Test
841+
public void queryTotalForAuthorsWithWhereEXISTSBooksLIKETitleEmpty() {
842+
//given
843+
String query = "query { "
844+
+ "Authors(where: {" +
845+
" EXISTS: {" +
846+
" books: {" +
847+
" author: {name: {LIKE: \"Anton\"}}" +
848+
" title: {LIKE: \"War\"}" +
849+
" }" +
850+
" }" +
851+
" }) {" +
852+
" total" +
853+
" pages" +
854+
" select {" +
855+
" id" +
856+
" name" +
857+
" books {" +
858+
" id" +
859+
" title" +
860+
" }" +
861+
" }" +
862+
" }"+
863+
"}";
864+
865+
String expected = "{Authors={total=0, pages=0, select=[]}}";
866+
867+
//when
868+
Object result = executor.execute(query).getData();
869+
870+
// then
871+
assertThat(result.toString()).isEqualTo(expected);
872+
}
873+
874+
@Test
875+
public void queryTotalForAuthorsWithWhereBooksNOTEXISTSAuthorLIKENameLeo() {
876+
//given
877+
String query = "query { "
878+
+ " Authors(where: {" +
879+
" books: {" +
880+
" NOT_EXISTS: {" +
881+
" author: {" +
882+
" name: {LIKE: \"Leo\"}" +
883+
" }" +
884+
" }" +
885+
" }" +
886+
" }) {" +
887+
" total" +
888+
" pages" +
889+
" select {" +
890+
" id" +
891+
" name" +
892+
" books {" +
893+
" id" +
894+
" title" +
895+
" }" +
896+
" }" +
897+
" }"+
898+
"}";
899+
900+
String expected = "{Authors={total=3, pages=1, select=["
901+
+ "{id=4, name=Anton Chekhov, books=["
902+
+ "{id=5, title=The Cherry Orchard}, "
903+
+ "{id=6, title=The Seagull}, "
904+
+ "{id=7, title=Three Sisters}]}"
905+
+ "]}}";
906+
907+
//when
908+
Object result = executor.execute(query).getData();
909+
910+
// then
911+
assertThat(result.toString()).isEqualTo(expected);
912+
}
913+
840914
@Test
841915
public void queryForAuthorssWithWhereBooksGenreEquals() {
842916
//given

0 commit comments

Comments
 (0)