Skip to content

Commit 4a75979

Browse files
committed
fix: Let's not pass DISTINCT in JPQL for better performance
1 parent 0e742a2 commit 4a75979

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package com.introproventures.graphql.jpa.query.schema.impl;
1717

18+
import java.util.Collection;
1819
import java.util.LinkedHashMap;
20+
import java.util.LinkedHashSet;
1921
import java.util.List;
2022
import java.util.Map;
2123
import java.util.Optional;
@@ -132,12 +134,17 @@ public Object get(DataFetchingEnvironment environment) {
132134
query.setHint(ORG_HIBERNATE_FETCH_SIZE, 1000);
133135
query.setHint(ORG_HIBERNATE_CACHEABLE, false);
134136

135-
// Let's pass distinct if enabled
137+
// Let's not pass distinct if enabled to have better performance
136138
if(isDistinct) {
137-
query.setHint(HIBERNATE_QUERY_PASS_DISTINCT_THROUGH, true);
139+
query.setHint(HIBERNATE_QUERY_PASS_DISTINCT_THROUGH, false);
138140
}
139141

140-
result.put(GraphQLJpaSchemaBuilder.QUERY_SELECT_PARAM_NAME, query.getResultList());
142+
// Let's remove any duplicate references for root entities
143+
Collection<?> resultList = isDistinct ?
144+
new LinkedHashSet<Object>(query.getResultList())
145+
: query.getResultList();
146+
147+
result.put(GraphQLJpaSchemaBuilder.QUERY_SELECT_PARAM_NAME, resultList);
141148
}
142149

143150
if (totalSelection.isPresent() || pagesSelection.isPresent()) {

0 commit comments

Comments
 (0)