Skip to content

Commit dd7fe62

Browse files
committedOct 23, 2018
fix: improve page argument handling
1 parent 8950021 commit dd7fe62

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed
 

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

+30-9
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
*
4949
*/
5050
class GraphQLJpaQueryDataFetcher extends QraphQLJpaBaseDataFetcher {
51-
51+
5252
public GraphQLJpaQueryDataFetcher(EntityManager entityManager, EntityType<?> entityType) {
5353
super(entityManager, entityType);
5454
}
@@ -63,6 +63,7 @@ public Object get(DataFetchingEnvironment environment) {
6363
Optional<Field> totalSelection = getSelectionField(field, GraphQLJpaSchemaBuilder.PAGE_TOTAL_PARAM_NAME);
6464
Optional<Field> recordsSelection = getSelectionField(field, GraphQLJpaSchemaBuilder.QUERY_SELECT_PARAM_NAME);
6565

66+
Optional<Argument> pageArgument = getPageArgument(field);
6667
Page page = extractPageArgument(environment, field);
6768
Argument distinctArg = extractArgument(environment, field, GraphQLJpaSchemaBuilder.SELECT_DISTINCT_PARAM_NAME, new BooleanValue(true));
6869

@@ -98,14 +99,28 @@ public Object get(DataFetchingEnvironment environment) {
9899

99100
queryField = new Field(fieldName, field.getArguments(), recordsSelection.get().getSelectionSet());
100101

101-
TypedQuery<?> query = getQuery(queryEnvironment, queryField, isDistinct)
102-
.setMaxResults(page.size)
103-
.setFirstResult((page.page - 1) * page.size);
102+
TypedQuery<?> query = getQuery(queryEnvironment, queryField, isDistinct);
103+
104+
// Let's apply page only if present
105+
if(pageArgument.isPresent()) {
106+
query
107+
.setMaxResults(page.size)
108+
.setFirstResult((page.page - 1) * page.size);
109+
}
104110

105-
// Create entity graph from selection
111+
// Let's create entity graph from selection
112+
// When using fetchgraph all relationships are considered to be lazy regardless of annotation,
113+
// and only the elements of the provided graph are loaded. This particularly useful when running
114+
// reports on certain objects and you don't want a lot of the stuff that's normally flagged to
115+
// load via eager annotations.
106116
EntityGraph<?> graph = buildEntityGraph(queryField);
107117
query.setHint("javax.persistence.fetchgraph", graph);
108118

119+
// Let' try reduce overhead
120+
query.setHint("org.hibernate.readOnly", true);
121+
query.setHint("org.hibernate.fetchSize", 1000);
122+
query.setHint("org.hibernate.cacheable", true);
123+
109124
result.put(GraphQLJpaSchemaBuilder.QUERY_SELECT_PARAM_NAME, query.getResultList());
110125
}
111126

@@ -159,11 +174,17 @@ private TypedQuery<Long> getCountQuery(DataFetchingEnvironment environment, Fiel
159174
return entityManager.createQuery(query);
160175
}
161176

177+
178+
private Optional<Argument> getPageArgument(Field field) {
179+
return field.getArguments()
180+
.stream()
181+
.filter(it -> GraphQLJpaSchemaBuilder.PAGE_PARAM_NAME.equals(it.getName()))
182+
.findFirst();
183+
}
184+
185+
162186
private Page extractPageArgument(DataFetchingEnvironment environment, Field field) {
163-
Optional<Argument> paginationRequest = field.getArguments()
164-
.stream()
165-
.filter(it -> GraphQLJpaSchemaBuilder.PAGE_PARAM_NAME.equals(it.getName()))
166-
.findFirst();
187+
Optional<Argument> paginationRequest = getPageArgument(field);
167188

168189
if (paginationRequest.isPresent()) {
169190
field.getArguments()

0 commit comments

Comments
 (0)
Please sign in to comment.