21
21
import java .util .Collection ;
22
22
import java .util .List ;
23
23
import java .util .Map ;
24
- import java .util .Objects ;
25
24
import java .util .ServiceLoader ;
26
25
import java .util .ServiceLoader .Provider ;
27
26
import java .util .concurrent .CompletableFuture ;
31
30
import org .kie .kogito .index .CommonUtils ;
32
31
import org .kie .kogito .index .api .KogitoRuntimeClient ;
33
32
import org .kie .kogito .index .graphql .query .GraphQLQueryOrderByParser ;
33
+ import org .kie .kogito .index .graphql .query .GraphQLQueryParser ;
34
34
import org .kie .kogito .index .graphql .query .GraphQLQueryParserRegistry ;
35
35
import org .kie .kogito .index .model .Job ;
36
36
import org .kie .kogito .index .model .Node ;
41
41
import org .kie .kogito .index .service .DataIndexServiceException ;
42
42
import org .kie .kogito .index .storage .DataIndexStorageService ;
43
43
import org .kie .kogito .persistence .api .StorageFetcher ;
44
+ import org .kie .kogito .persistence .api .StorageServiceCapability ;
44
45
import org .kie .kogito .persistence .api .query .Query ;
45
46
import org .slf4j .Logger ;
46
47
import org .slf4j .LoggerFactory ;
47
48
48
49
import graphql .schema .DataFetcher ;
49
50
import graphql .schema .DataFetchingEnvironment ;
51
+ import graphql .schema .GraphQLArgument ;
50
52
import graphql .schema .GraphQLInputObjectType ;
53
+ import graphql .schema .GraphQLInputType ;
51
54
import graphql .schema .GraphQLNamedType ;
52
55
import graphql .schema .GraphQLScalarType ;
53
56
import graphql .schema .GraphQLSchema ;
@@ -109,6 +112,29 @@ protected final void loadAdditionalMutations(TypeDefinitionRegistry typeRegistry
109
112
mutations .stream ().map (GraphQLMutationsProvider ::registry ).forEach (typeRegistry ::merge );
110
113
}
111
114
115
+ protected final void addCountQueries (TypeDefinitionRegistry typeRegistry ) {
116
+ if (supportsCount ()) {
117
+ typeRegistry .merge (loadSchemaDefinitionFile ("count.schema.graphqls" ));
118
+ }
119
+ }
120
+
121
+ protected final void addJsonQueries (TypeDefinitionRegistry typeRegistry ) {
122
+ if (cacheService .capabilities ().contains (StorageServiceCapability .JSON_QUERY )) {
123
+ typeRegistry .merge (loadSchemaDefinitionFile ("json.schema.graphqls" ));
124
+ }
125
+ }
126
+
127
+ protected final void addCountQueries (Builder builder ) {
128
+ if (supportsCount ()) {
129
+ builder .dataFetcher ("CountProcessInstances" , this ::countProcessInstances );
130
+ builder .dataFetcher ("CountUserTaskInstances" , this ::countUserTaskInstances );
131
+ }
132
+ }
133
+
134
+ private boolean supportsCount () {
135
+ return cacheService .capabilities ().contains (StorageServiceCapability .COUNT );
136
+ }
137
+
112
138
protected TypeDefinitionRegistry loadSchemaDefinitionFile (String fileName ) {
113
139
return CommonUtils .loadSchemaDefinitionFile (fileName );
114
140
}
@@ -149,7 +175,7 @@ public String getProcessInstanceServiceUrl(DataFetchingEnvironment env) {
149
175
150
176
public ProcessDefinition getProcessDefinition (DataFetchingEnvironment env ) {
151
177
ProcessInstance source = env .getSource ();
152
- return source .getDefinition ( );
178
+ return cacheService . getProcessDefinitionStorage (). get ( new ProcessDefinitionKey ( source .getProcessId (), source . getVersion ()) );
153
179
}
154
180
155
181
protected String getServiceUrl (String endpoint , String processId ) {
@@ -182,18 +208,17 @@ protected Collection<ProcessInstance> getProcessInstancesValues(DataFetchingEnvi
182
208
return executeAdvancedQueryForCache (cacheService .getProcessInstanceStorage (), env );
183
209
}
184
210
185
- protected <K , T > List <T > executeAdvancedQueryForCache (StorageFetcher <K , T > cache , DataFetchingEnvironment env ) {
186
- Objects .requireNonNull (cache , "Cache not found" );
187
-
188
- String inputTypeName = ((GraphQLNamedType ) env .getFieldDefinition ().getArgument ("where" ).getType ()).getName ();
189
-
190
- Query <T > query = cache .query ();
211
+ protected long countProcessInstances (DataFetchingEnvironment env ) {
212
+ return executeCount (cacheService .getProcessInstanceStorage (), env );
213
+ }
191
214
192
- Map <String , Object > where = env .getArgument ("where" );
193
- query .filter (GraphQLQueryParserRegistry .get ().getParser (inputTypeName ).apply (where ));
215
+ protected long countUserTaskInstances (DataFetchingEnvironment env ) {
216
+ return executeCount (cacheService .getUserTaskInstanceStorage (), env );
217
+ }
194
218
219
+ protected <K , T > List <T > executeAdvancedQueryForCache (StorageFetcher <K , T > cache , DataFetchingEnvironment env ) {
220
+ Query <T > query = buildQuery (cache , env );
195
221
query .sort (new GraphQLQueryOrderByParser ().apply (env ));
196
-
197
222
Map <String , Integer > pagination = env .getArgument ("pagination" );
198
223
if (pagination != null ) {
199
224
Integer limit = pagination .get ("limit" );
@@ -205,10 +230,29 @@ protected <K, T> List<T> executeAdvancedQueryForCache(StorageFetcher<K, T> cache
205
230
query .offset (offset );
206
231
}
207
232
}
208
-
209
233
return query .execute ();
210
234
}
211
235
236
+ protected <K , T > long executeCount (StorageFetcher <K , T > cache , DataFetchingEnvironment env ) {
237
+ return buildQuery (cache , env ).count ();
238
+ }
239
+
240
+ private <K , T > Query <T > buildQuery (StorageFetcher <K , T > cache , DataFetchingEnvironment env ) {
241
+ assert cache != null ;
242
+ Query <T > query = cache .query ();
243
+ GraphQLArgument arg = env .getFieldDefinition ().getArgument ("where" );
244
+ if (arg != null ) {
245
+ GraphQLInputType inputType = arg .getType ();
246
+ if (inputType instanceof GraphQLNamedType ) {
247
+ GraphQLQueryParser parser = GraphQLQueryParserRegistry .get ().getParser (((GraphQLNamedType ) inputType ).getName ());
248
+ if (parser != null ) {
249
+ query .filter (parser .apply (env .getArgument ("where" )));
250
+ }
251
+ }
252
+ }
253
+ return query ;
254
+ }
255
+
212
256
protected Collection <UserTaskInstance > getUserTaskInstancesValues (DataFetchingEnvironment env ) {
213
257
return executeAdvancedQueryForCache (cacheService .getUserTaskInstanceStorage (), env );
214
258
}
0 commit comments