Skip to content

Commit 7ca1394

Browse files
committed
Skip total count if select result is empty
1 parent 4e3cfca commit 7ca1394

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import graphql.schema.DataFetcher;
3535
import graphql.schema.DataFetchingEnvironment;
3636
import graphql.schema.GraphQLScalarType;
37+
import java.util.Collection;
3738
import java.util.LinkedHashMap;
3839
import java.util.List;
3940
import java.util.Map;
@@ -117,7 +118,12 @@ public PagedResult<Object> get(DataFetchingEnvironment environment) {
117118
}
118119

119120
if (totalSelection.isPresent() || pagesSelection.isPresent()) {
120-
final Long total = queryFactory.queryTotalCount(environment, restrictedKeys);
121+
final var selectResult = pagedResult.getSelect();
122+
123+
final long total = recordsSelection.isEmpty() ||
124+
selectResult.filter(Predicate.not(Collection::isEmpty)).isPresent()
125+
? queryFactory.queryTotalCount(environment, restrictedKeys)
126+
: 0L;
121127

122128
pagedResult.withTotal(total);
123129
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.LinkedHashMap;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.Optional;
2324

2425
public class PagedResult<T> {
2526

@@ -135,6 +136,10 @@ public Builder<T> withSelect(List<T> select) {
135136
return this;
136137
}
137138

139+
public Optional<List<T>> getSelect() {
140+
return Optional.ofNullable(select);
141+
}
142+
138143
/**
139144
* Builder method for select parameter.
140145
* @param select field to set

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
6363
return System.identityHashCode(proxy);
6464
} else if ("spliterator".equals(method.getName())) {
6565
return stream.spliterator();
66+
} else if ("isEmpty".equals(method.getName())) {
67+
return size == 0;
6668
}
6769
throw new UnsupportedOperationException(method + " is not supported");
6870
}

0 commit comments

Comments
 (0)