forked from apache/incubator-kie-kogito-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix apache#2165] Adding custom mutations
- Loading branch information
Showing
12 changed files
with
273 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
data-index/data-index-common/src/main/java/org/kie/kogito/index/api/ExecuteArgs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.kie.kogito.index.api; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
public record ExecuteArgs(JsonNode input, String businessKey, String referenceId) { | ||
|
||
public static ExecuteArgs of(JsonNode input) { | ||
return new Builder().withInput(input).build(); | ||
} | ||
|
||
public static Builder builder(JsonNode modelInput) { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private JsonNode input; | ||
private String businessKey; | ||
private String referenceId; | ||
|
||
private Builder() { | ||
} | ||
|
||
public Builder withInput(JsonNode input) { | ||
this.input = input; | ||
return this; | ||
} | ||
|
||
public Builder withBusinessKey(String businessKey) { | ||
this.businessKey = businessKey; | ||
return this; | ||
} | ||
|
||
public Builder withReferenceId(String referenceId) { | ||
this.referenceId = referenceId; | ||
return this; | ||
} | ||
|
||
public ExecuteArgs build() { | ||
return new ExecuteArgs(input, businessKey, referenceId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...e-common/src/main/java/org/kie/kogito/index/service/graphql/GraphQLMutationsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.kie.kogito.index.service.graphql; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.kie.kogito.index.graphql.AbstractGraphQLSchemaManager; | ||
|
||
import graphql.schema.DataFetcher; | ||
|
||
public interface GraphQLMutationsProvider { | ||
Map<String, DataFetcher<CompletableFuture<?>>> mutations(AbstractGraphQLSchemaManager schemaManager); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
data-index/data-index-service/data-index-service-shared-output/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.kie.kogito</groupId> | ||
<artifactId>data-index-service</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>data-index-service-shared-output</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.kie.kogito</groupId> | ||
<artifactId>data-index-service-common</artifactId> | ||
</dependency> | ||
</dependencies> | ||
<properties> | ||
<java.module.name>org.kie.kogito.index.service.mutations</java.module.name> | ||
</properties> | ||
</project> |
79 changes: 79 additions & 0 deletions
79
...t/src/main/java/org/kie/kogito/index/service/mutations/OutputGraphQLMutationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.kie.kogito.index.service.mutations; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.kie.kogito.index.api.ExecuteArgs; | ||
import org.kie.kogito.index.graphql.AbstractGraphQLSchemaManager; | ||
import org.kie.kogito.index.model.ProcessDefinition; | ||
import org.kie.kogito.index.model.ProcessDefinitionKey; | ||
import org.kie.kogito.index.model.ProcessInstance; | ||
import org.kie.kogito.index.service.graphql.GraphQLMutationsProvider; | ||
import org.kie.kogito.index.storage.DataIndexStorageService; | ||
import org.kie.kogito.jackson.utils.MergeUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
|
||
public class OutputGraphQLMutationProvider implements GraphQLMutationsProvider { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(OutputGraphQLMutationProvider.class); | ||
private static final String COMPLETED_INSTANCE_ID = "completedInstanceId"; | ||
|
||
@Override | ||
public Map<String, DataFetcher<CompletableFuture<?>>> mutations(AbstractGraphQLSchemaManager schemaManager) { | ||
return Map.of("sharedOutput", env -> sharedOutput(schemaManager, env)); | ||
} | ||
|
||
private CompletableFuture<String> sharedOutput(AbstractGraphQLSchemaManager schemaManager, DataFetchingEnvironment env) { | ||
DataIndexStorageService cacheService = schemaManager.getCacheService(); | ||
ProcessDefinitionKey key = new ProcessDefinitionKey(mandatoryArgument(env, "processId"), mandatoryArgument(env, "processVersion")); | ||
ProcessDefinition processDefinition = cacheService.getProcessDefinitionStorage().get(key); | ||
if (processDefinition == null) { | ||
throw new IllegalArgumentException(key + "does not correspond to any existing process definition"); | ||
} | ||
JsonNode input = env.getArgument("input"); | ||
String completedInstanceId = env.getArgument(COMPLETED_INSTANCE_ID); | ||
if (completedInstanceId != null) { | ||
ProcessInstance processInstance = cacheService.getProcessInstanceStorage().get(completedInstanceId); | ||
if (processInstance != null) { | ||
input = MergeUtils.merge(input, processInstance.getVariables()); | ||
} else { | ||
logger.warn("Completed Instance Id {} cannot be found, using user input as it is", completedInstanceId); | ||
} | ||
} else { | ||
logger.warn("Missing " + COMPLETED_INSTANCE_ID + " parameter, using user input as it is"); | ||
} | ||
return schemaManager.getDataIndexApiExecutor().executeProcessIntance(processDefinition, ExecuteArgs.of(input)); | ||
} | ||
|
||
private static <T> T mandatoryArgument(DataFetchingEnvironment env, String name) { | ||
T result = env.getArgument(name); | ||
if (result == null) { | ||
throw new IllegalArgumentException("Missing " + name + " mandatory parameter"); | ||
} | ||
return result; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...resources/META-INF/services/org.kie.kogito.index.service.graphql.GraphQLMutationsProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
org.kie.kogito.index.service.mutations.OutputGraphQLMutationProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.