diff --git a/data-index/data-index-common/src/main/java/org/kie/kogito/index/CommonUtils.java b/data-index/data-index-common/src/main/java/org/kie/kogito/index/CommonUtils.java index 047080e35a..47e70d8f7b 100644 --- a/data-index/data-index-common/src/main/java/org/kie/kogito/index/CommonUtils.java +++ b/data-index/data-index-common/src/main/java/org/kie/kogito/index/CommonUtils.java @@ -18,11 +18,17 @@ */ package org.kie.kogito.index; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import graphql.schema.idl.SchemaParser; +import graphql.schema.idl.TypeDefinitionRegistry; + public class CommonUtils { public static final int ERROR_STATE = 5; @@ -51,6 +57,16 @@ public static String getServiceUrl(String endpoint, String processId) { } } + public static TypeDefinitionRegistry loadSchemaDefinitionFile(String fileName) { + SchemaParser schemaParser = new SchemaParser(); + try (InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); + InputStreamReader reader = new InputStreamReader(stream)) { + return schemaParser.parse(reader); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + private static String getContext(String processId) { return processId != null && processId.contains(".") ? processId.substring(processId.lastIndexOf('.') + 1) : processId; } diff --git a/data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/AbstractGraphQLSchemaManager.java b/data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/AbstractGraphQLSchemaManager.java index f1cb5604d4..2b86012b77 100644 --- a/data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/AbstractGraphQLSchemaManager.java +++ b/data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/AbstractGraphQLSchemaManager.java @@ -18,9 +18,6 @@ */ package org.kie.kogito.index.graphql; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.util.Collection; import java.util.List; import java.util.Map; @@ -51,7 +48,6 @@ import graphql.schema.GraphQLNamedType; import graphql.schema.GraphQLScalarType; import graphql.schema.GraphQLSchema; -import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; import graphql.schema.idl.TypeRuntimeWiring.Builder; import jakarta.annotation.PostConstruct; @@ -83,8 +79,11 @@ public abstract class AbstractGraphQLSchemaManager implements GraphQLSchemaManag private GraphQLSchema schema; + private Collection mutations; + @PostConstruct public void setup() { + mutations = ServiceLoader.load(GraphQLMutationsProvider.class).stream().map(Provider::get).collect(Collectors.toList()); schema = createSchema(); GraphQLQueryParserRegistry.get().registerParsers( (GraphQLInputObjectType) schema.getType("ProcessDefinitionArgument"), @@ -94,18 +93,16 @@ public void setup() { } protected final void loadAdditionalMutations(Builder builder) { - ServiceLoader.load(GraphQLMutationsProvider.class).stream().map(Provider::get).map(m -> m.mutations(this)).flatMap(map -> map.entrySet().stream()) + mutations.stream().map(m -> m.mutations(this)).flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2)).forEach(builder::dataFetcher); } + protected final void loadAdditionalMutations(TypeDefinitionRegistry typeRegistry) { + mutations.stream().map(GraphQLMutationsProvider::registry).forEach(typeRegistry::merge); + } + protected TypeDefinitionRegistry loadSchemaDefinitionFile(String fileName) { - SchemaParser schemaParser = new SchemaParser(); - try (InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); - InputStreamReader reader = new InputStreamReader(stream)) { - return schemaParser.parse(reader); - } catch (IOException e) { - throw new RuntimeException(e); - } + return CommonUtils.loadSchemaDefinitionFile(fileName); } public abstract GraphQLSchema createSchema(); diff --git a/data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/GraphQLMutationsProvider.java b/data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/GraphQLMutationsProvider.java index c4b30c186d..6457ee47f4 100644 --- a/data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/GraphQLMutationsProvider.java +++ b/data-index/data-index-graphql/src/main/java/org/kie/kogito/index/graphql/GraphQLMutationsProvider.java @@ -22,7 +22,10 @@ import java.util.concurrent.CompletableFuture; import graphql.schema.DataFetcher; +import graphql.schema.idl.TypeDefinitionRegistry; public interface GraphQLMutationsProvider { Map>> mutations(AbstractGraphQLSchemaManager schemaManager); + + TypeDefinitionRegistry registry(); } diff --git a/data-index/data-index-mutations/data-index-shared-output-mutation/src/main/java/org/kie/kogito/index/mutations/OutputGraphQLMutationProvider.java b/data-index/data-index-mutations/data-index-shared-output-mutation/src/main/java/org/kie/kogito/index/mutations/OutputGraphQLMutationProvider.java index 43289c96d5..65a2e8155a 100644 --- a/data-index/data-index-mutations/data-index-shared-output-mutation/src/main/java/org/kie/kogito/index/mutations/OutputGraphQLMutationProvider.java +++ b/data-index/data-index-mutations/data-index-shared-output-mutation/src/main/java/org/kie/kogito/index/mutations/OutputGraphQLMutationProvider.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; +import org.kie.kogito.index.CommonUtils; import org.kie.kogito.index.api.ExecuteArgs; import org.kie.kogito.index.graphql.AbstractGraphQLSchemaManager; import org.kie.kogito.index.graphql.GraphQLMutationsProvider; @@ -36,6 +37,7 @@ import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; +import graphql.schema.idl.TypeDefinitionRegistry; public class OutputGraphQLMutationProvider implements GraphQLMutationsProvider { @@ -76,4 +78,9 @@ private static T mandatoryArgument(DataFetchingEnvironment env, String name) } return result; } + + @Override + public TypeDefinitionRegistry registry() { + return CommonUtils.loadSchemaDefinitionFile("mutation.schema.graphqls"); + } } diff --git a/data-index/data-index-mutations/data-index-shared-output-mutation/src/main/resources/mutation.schema.graphqls b/data-index/data-index-mutations/data-index-shared-output-mutation/src/main/resources/mutation.schema.graphqls new file mode 100644 index 0000000000..550b182a7e --- /dev/null +++ b/data-index/data-index-mutations/data-index-shared-output-mutation/src/main/resources/mutation.schema.graphqls @@ -0,0 +1,3 @@ +extend type Mutation { + ExecuteAfter(completeProcessId: String, processId: String, processVersion: String, input: JSON): String +} diff --git a/data-index/data-index-service/data-index-service-common/src/main/java/org/kie/kogito/index/service/graphql/GraphQLSchemaManagerImpl.java b/data-index/data-index-service/data-index-service-common/src/main/java/org/kie/kogito/index/service/graphql/GraphQLSchemaManagerImpl.java index 3320ede2e6..cadff87c60 100644 --- a/data-index/data-index-service/data-index-service-common/src/main/java/org/kie/kogito/index/service/graphql/GraphQLSchemaManagerImpl.java +++ b/data-index/data-index-service/data-index-service-common/src/main/java/org/kie/kogito/index/service/graphql/GraphQLSchemaManagerImpl.java @@ -74,6 +74,7 @@ public GraphQLSchema createSchema() { TypeDefinitionRegistry typeDefinitionRegistry = new TypeDefinitionRegistry(); typeDefinitionRegistry.merge(loadSchemaDefinitionFile("basic.schema.graphqls")); typeDefinitionRegistry.merge(loadSchemaDefinitionFile("domain.schema.graphqls")); + loadAdditionalMutations(typeDefinitionRegistry); RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring() .scalar(ExtendedScalars.Json) diff --git a/data-index/data-index-service/data-index-service-common/src/test/java/org/kie/kogito/index/service/graphql/query/AbstractGraphQLRuntimesQueriesIT.java b/data-index/data-index-service/data-index-service-common/src/test/java/org/kie/kogito/index/service/graphql/query/AbstractGraphQLRuntimesQueriesIT.java index eed2b8cd8a..530d48aba5 100644 --- a/data-index/data-index-service/data-index-service-common/src/test/java/org/kie/kogito/index/service/graphql/query/AbstractGraphQLRuntimesQueriesIT.java +++ b/data-index/data-index-service/data-index-service-common/src/test/java/org/kie/kogito/index/service/graphql/query/AbstractGraphQLRuntimesQueriesIT.java @@ -122,9 +122,9 @@ void testProcessExecuteInstance() { ProcessDefinitionDataEvent definitionEvent = TestUtils.getProcessDefinitionDataEvent(infraProcessId); indexProcessCloudEvent(definitionEvent); checkOkResponse("{ \"query\" : \"mutation{ ExecuteAfter ( " + fragment("completedProcessId", assesmentInstanceId) + "," + fragment("processId", infraProcessId) + - "," + fragment("processVersion", TestUtils.PROCESS_VERSION) + "," + "input: {" + fragment(assesmentVarName, assesmentVarValue) + "})}\"}"); + "," + fragment("processVersion", TestUtils.PROCESS_VERSION) + "," + "input: {" + fragment(infraVarName, infraVarValue) + "})}\"}"); verify(dataIndexApiClient).executeProcessIntance(getProcessDefinition(infraProcessId), ExecuteArgs.of(ObjectMapperFactory.get().createObjectNode().put(assesmentVarName, assesmentVarValue) - .put(infraVarName,infraVarValue))); + .put(infraVarName, infraVarValue))); } private String fragment(String name, String value) { diff --git a/data-index/kogito-addons-quarkus-data-index/kogito-addons-quarkus-data-index-common/runtime/src/main/java/org/kie/kogito/index/addon/graphql/GraphQLAddonSchemaManagerImpl.java b/data-index/kogito-addons-quarkus-data-index/kogito-addons-quarkus-data-index-common/runtime/src/main/java/org/kie/kogito/index/addon/graphql/GraphQLAddonSchemaManagerImpl.java index 4f44a1b5ec..ca8a07eb32 100644 --- a/data-index/kogito-addons-quarkus-data-index/kogito-addons-quarkus-data-index-common/runtime/src/main/java/org/kie/kogito/index/addon/graphql/GraphQLAddonSchemaManagerImpl.java +++ b/data-index/kogito-addons-quarkus-data-index/kogito-addons-quarkus-data-index-common/runtime/src/main/java/org/kie/kogito/index/addon/graphql/GraphQLAddonSchemaManagerImpl.java @@ -34,6 +34,7 @@ public class GraphQLAddonSchemaManagerImpl extends AbstractGraphQLSchemaManager public GraphQLSchema createSchema() { TypeDefinitionRegistry typeDefinitionRegistry = new TypeDefinitionRegistry(); typeDefinitionRegistry.merge(loadSchemaDefinitionFile("basic.schema.graphqls")); + loadAdditionalMutations(typeDefinitionRegistry); RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring() .type("Query", builder -> {