48
48
*
49
49
*/
50
50
class GraphQLJpaQueryDataFetcher extends QraphQLJpaBaseDataFetcher {
51
-
51
+
52
52
public GraphQLJpaQueryDataFetcher (EntityManager entityManager , EntityType <?> entityType ) {
53
53
super (entityManager , entityType );
54
54
}
@@ -63,6 +63,7 @@ public Object get(DataFetchingEnvironment environment) {
63
63
Optional <Field > totalSelection = getSelectionField (field , GraphQLJpaSchemaBuilder .PAGE_TOTAL_PARAM_NAME );
64
64
Optional <Field > recordsSelection = getSelectionField (field , GraphQLJpaSchemaBuilder .QUERY_SELECT_PARAM_NAME );
65
65
66
+ Optional <Argument > pageArgument = getPageArgument (field );
66
67
Page page = extractPageArgument (environment , field );
67
68
Argument distinctArg = extractArgument (environment , field , GraphQLJpaSchemaBuilder .SELECT_DISTINCT_PARAM_NAME , new BooleanValue (true ));
68
69
@@ -98,14 +99,28 @@ public Object get(DataFetchingEnvironment environment) {
98
99
99
100
queryField = new Field (fieldName , field .getArguments (), recordsSelection .get ().getSelectionSet ());
100
101
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
+ }
104
110
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.
106
116
EntityGraph <?> graph = buildEntityGraph (queryField );
107
117
query .setHint ("javax.persistence.fetchgraph" , graph );
108
118
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
+
109
124
result .put (GraphQLJpaSchemaBuilder .QUERY_SELECT_PARAM_NAME , query .getResultList ());
110
125
}
111
126
@@ -159,11 +174,17 @@ private TypedQuery<Long> getCountQuery(DataFetchingEnvironment environment, Fiel
159
174
return entityManager .createQuery (query );
160
175
}
161
176
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
+
162
186
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 );
167
188
168
189
if (paginationRequest .isPresent ()) {
169
190
field .getArguments ()
0 commit comments