Search in sources :

Example 1 with DomainDescriptor

use of org.kie.kogito.persistence.api.proto.DomainDescriptor in project kogito-apps by kiegroup.

the class GraphQLObjectTypeMapper method build.

private Consumer<GraphQLObjectType.Builder> build(DomainDescriptor domain) {
    return builder -> domain.getAttributes().forEach(field -> {
        LOGGER.debug("GraphQL mapping field: {}", field.getName());
        if (KogitoMetadata.class.getName().equals(field.getTypeName())) {
            builder.field(newFieldDefinition().name(KOGITO_DOMAIN_ATTRIBUTE).type(schema.getObjectType("KogitoMetadata"))).build();
        } else {
            GraphQLOutputType type;
            switch(field.getTypeName()) {
                case "java.lang.Integer":
                    type = Scalars.GraphQLInt;
                    break;
                case "java.lang.Long":
                    type = ExtendedScalars.GraphQLLong;
                    break;
                case "java.lang.String":
                case "java.util.Date":
                    type = Scalars.GraphQLString;
                    break;
                case "java.lang.Boolean":
                    type = Scalars.GraphQLBoolean;
                    break;
                default:
                    type = getGraphQLType(field, schema, additionalTypes, allTypes);
            }
            builder.field(newFieldDefinition().name(field.getName()).type(type));
        }
    });
}
Also used : GraphQLObjectType(graphql.schema.GraphQLObjectType) Logger(org.slf4j.Logger) ExtendedScalars(graphql.scalars.ExtendedScalars) LoggerFactory(org.slf4j.LoggerFactory) GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLType(graphql.schema.GraphQLType) Function(java.util.function.Function) GraphQLFieldDefinition.newFieldDefinition(graphql.schema.GraphQLFieldDefinition.newFieldDefinition) Consumer(java.util.function.Consumer) AttributeDescriptor(org.kie.kogito.persistence.api.proto.AttributeDescriptor) Scalars(graphql.Scalars) KOGITO_DOMAIN_ATTRIBUTE(org.kie.kogito.index.Constants.KOGITO_DOMAIN_ATTRIBUTE) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor) Map(java.util.Map) GraphQLSchema(graphql.schema.GraphQLSchema) KogitoMetadata(org.kie.kogito.index.model.KogitoMetadata) GraphQLOutputType(graphql.schema.GraphQLOutputType) KogitoMetadata(org.kie.kogito.index.model.KogitoMetadata)

Example 2 with DomainDescriptor

use of org.kie.kogito.persistence.api.proto.DomainDescriptor in project kogito-apps by kiegroup.

the class GraphQLProtoSchemaMapper method onDomainModelRegisteredEvent.

public void onDomainModelRegisteredEvent(@Observes DomainModelRegisteredEvent event) {
    LOGGER.debug("Received new domain event: {}", event);
    GraphQLSchema schema = schemaManager.getGraphQLSchema();
    schemaManager.transform(builder -> {
        builder.clearAdditionalTypes();
        Map<String, DomainDescriptor> map = event.getAdditionalTypes().stream().collect(toMap(desc -> getTypeName(desc.getTypeName()), desc -> desc));
        Map<String, GraphQLType> additionalTypes = new ConcurrentHashMap<>();
        GraphQLObjectType rootType = new GraphQLObjectTypeMapper(schema, additionalTypes, map).apply(event.getDomainDescriptor());
        additionalTypes.put(rootType.getName(), rootType);
        GraphQLInputObjectType whereArgumentType = new GraphQLInputObjectTypeMapper(schema, additionalTypes).apply(rootType);
        additionalTypes.put(whereArgumentType.getName(), whereArgumentType);
        GraphQLInputObjectType orderByType = new GraphQLOrderByTypeMapper(schema, additionalTypes).apply(rootType);
        additionalTypes.put(orderByType.getName(), orderByType);
        LOGGER.debug("New GraphQL types: {}", additionalTypes.keySet());
        Set<GraphQLType> newTypes = additionalTypes.entrySet().stream().map(entry -> entry.getValue()).collect(toSet());
        newTypes.addAll(schema.getAdditionalTypes().stream().filter(type -> additionalTypes.containsKey(((GraphQLNamedType) type).getName()) == false).collect(toSet()));
        builder.additionalTypes(newTypes);
        GraphQLObjectType query = schema.getQueryType();
        // Should use extend instead?
        query = query.transform(qBuilder -> {
            if (qBuilder.hasField(rootType.getName())) {
                qBuilder.clearFields();
                qBuilder.fields(schema.getQueryType().getFieldDefinitions().stream().filter(field -> rootType.getName().equals(field.getName()) == false).collect(toList()));
            }
            GraphQLQueryParserRegistry.get().registerParser(whereArgumentType);
            GraphQLArgument where = newArgument().name("where").type(whereArgumentType).build();
            GraphQLArgument orderBy = newArgument().name("orderBy").type(orderByType).build();
            GraphQLArgument pagination = newArgument().name("pagination").type(new GraphQLTypeReference("Pagination")).build();
            qBuilder.field(newFieldDefinition().name(rootType.getName()).type(GraphQLList.list(rootType)).arguments(asList(where, orderBy, pagination)));
        });
        builder.query(query);
        GraphQLObjectType subscription = schema.getSubscriptionType();
        subscription = subscription.transform(sBuilder -> {
            sBuilder.field(newFieldDefinition().name(rootType.getName() + "Added").type(nonNull(rootType)).build());
            sBuilder.field(newFieldDefinition().name(rootType.getName() + "Updated").type(nonNull(rootType)).build());
        });
        builder.subscription(subscription);
        GraphQLCodeRegistry registry = schema.getCodeRegistry().transform(codeBuilder -> {
            codeBuilder.dataFetcher(coordinates("Query", rootType.getName()), schemaManager.getDomainModelDataFetcher(event.getProcessId()));
            codeBuilder.dataFetcher(coordinates("Subscription", rootType.getName() + "Added"), schemaManager.getDomainModelAddedDataFetcher(event.getProcessId()));
            codeBuilder.dataFetcher(coordinates("Subscription", rootType.getName() + "Updated"), schemaManager.getDomainModelUpdatedDataFetcher(event.getProcessId()));
        });
        builder.codeRegistry(registry);
    });
}
Also used : GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) GraphQLNamedType(graphql.schema.GraphQLNamedType) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) LoggerFactory(org.slf4j.LoggerFactory) GraphQLType(graphql.schema.GraphQLType) Inject(javax.inject.Inject) GraphQLInputObjectTypeMapper(org.kie.kogito.index.graphql.query.GraphQLInputObjectTypeMapper) Collectors.toMap(java.util.stream.Collectors.toMap) GraphQLNonNull.nonNull(graphql.schema.GraphQLNonNull.nonNull) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Observes(javax.enterprise.event.Observes) DomainModelRegisteredEvent(org.kie.kogito.persistence.api.proto.DomainModelRegisteredEvent) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLQueryParserRegistry(org.kie.kogito.index.graphql.query.GraphQLQueryParserRegistry) Collectors.toSet(java.util.stream.Collectors.toSet) GraphQLObjectTypeMapper.getTypeName(org.kie.kogito.index.graphql.GraphQLObjectTypeMapper.getTypeName) GraphQLObjectType(graphql.schema.GraphQLObjectType) Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) GraphQLArgument.newArgument(graphql.schema.GraphQLArgument.newArgument) GraphQLArgument(graphql.schema.GraphQLArgument) FieldCoordinates.coordinates(graphql.schema.FieldCoordinates.coordinates) GraphQLFieldDefinition.newFieldDefinition(graphql.schema.GraphQLFieldDefinition.newFieldDefinition) Collectors.toList(java.util.stream.Collectors.toList) GraphQLList(graphql.schema.GraphQLList) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor) ApplicationScoped(javax.enterprise.context.ApplicationScoped) GraphQLOrderByTypeMapper(org.kie.kogito.index.graphql.query.GraphQLOrderByTypeMapper) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) GraphQLType(graphql.schema.GraphQLType) GraphQLArgument(graphql.schema.GraphQLArgument) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLOrderByTypeMapper(org.kie.kogito.index.graphql.query.GraphQLOrderByTypeMapper) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLInputObjectTypeMapper(org.kie.kogito.index.graphql.query.GraphQLInputObjectTypeMapper) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor)

Example 3 with DomainDescriptor

use of org.kie.kogito.persistence.api.proto.DomainDescriptor in project kogito-apps by kiegroup.

the class GraphQLObjectTypeMapper method getGraphQLType.

private GraphQLOutputType getGraphQLType(AttributeDescriptor attribute, GraphQLSchema schema, Map<String, GraphQLType> additionalTypes, Map<String, DomainDescriptor> allTypes) {
    String typeName = getTypeName(attribute.getTypeName());
    GraphQLType type = schema.getType(typeName);
    if (type == null) {
        type = additionalTypes.computeIfAbsent(typeName, k -> new GraphQLObjectTypeMapper(schema, additionalTypes, allTypes).apply(allTypes.get(typeName)));
    }
    return (GraphQLOutputType) type;
}
Also used : GraphQLObjectType(graphql.schema.GraphQLObjectType) Logger(org.slf4j.Logger) ExtendedScalars(graphql.scalars.ExtendedScalars) LoggerFactory(org.slf4j.LoggerFactory) GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLType(graphql.schema.GraphQLType) Function(java.util.function.Function) GraphQLFieldDefinition.newFieldDefinition(graphql.schema.GraphQLFieldDefinition.newFieldDefinition) Consumer(java.util.function.Consumer) AttributeDescriptor(org.kie.kogito.persistence.api.proto.AttributeDescriptor) Scalars(graphql.Scalars) KOGITO_DOMAIN_ATTRIBUTE(org.kie.kogito.index.Constants.KOGITO_DOMAIN_ATTRIBUTE) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor) Map(java.util.Map) GraphQLSchema(graphql.schema.GraphQLSchema) KogitoMetadata(org.kie.kogito.index.model.KogitoMetadata) GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLType(graphql.schema.GraphQLType)

Example 4 with DomainDescriptor

use of org.kie.kogito.persistence.api.proto.DomainDescriptor in project kogito-apps by kiegroup.

the class ProtoDomainModelProducer method onFileDescriptorRegistered.

public void onFileDescriptorRegistered(@Observes FileDescriptorRegisteredEvent event) {
    FileDescriptor descriptor = event.getDescriptor();
    String rootMessage = (String) descriptor.getOption("kogito_model").getValue();
    String processId = (String) descriptor.getOption("kogito_id").getValue();
    Map<String, Descriptor> map = descriptor.getMessageTypes().stream().collect(toMap(AnnotatedDescriptorImpl::getName, desc -> desc));
    Descriptor rootDescriptor = map.remove(rootMessage);
    DomainDescriptor domain = new DomainDescriptorMapper().apply(rootDescriptor);
    List<DomainDescriptor> additionalTypes = map.values().stream().map(desc -> new DomainDescriptorMapper().apply(desc)).collect(toList());
    domainEvent.fire(new DomainModelRegisteredEvent(processId, domain, additionalTypes));
}
Also used : Event(javax.enterprise.event.Event) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Descriptor(org.infinispan.protostream.descriptors.Descriptor) Function(java.util.function.Function) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Inject(javax.inject.Inject) AnnotatedDescriptorImpl(org.infinispan.protostream.impl.AnnotatedDescriptorImpl) AttributeDescriptor(org.kie.kogito.persistence.api.proto.AttributeDescriptor) FileDescriptorRegisteredEvent(org.kie.kogito.persistence.protobuf.FileDescriptorRegisteredEvent) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Collectors.toMap(java.util.stream.Collectors.toMap) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor) Map(java.util.Map) Observes(javax.enterprise.event.Observes) DomainModelRegisteredEvent(org.kie.kogito.persistence.api.proto.DomainModelRegisteredEvent) ApplicationScoped(javax.enterprise.context.ApplicationScoped) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) AttributeDescriptor(org.kie.kogito.persistence.api.proto.AttributeDescriptor) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor) DomainModelRegisteredEvent(org.kie.kogito.persistence.api.proto.DomainModelRegisteredEvent) DomainDescriptor(org.kie.kogito.persistence.api.proto.DomainDescriptor) FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor)

Aggregations

Map (java.util.Map)4 DomainDescriptor (org.kie.kogito.persistence.api.proto.DomainDescriptor)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 GraphQLFieldDefinition.newFieldDefinition (graphql.schema.GraphQLFieldDefinition.newFieldDefinition)3 GraphQLObjectType (graphql.schema.GraphQLObjectType)3 GraphQLSchema (graphql.schema.GraphQLSchema)3 GraphQLType (graphql.schema.GraphQLType)3 Function (java.util.function.Function)3 AttributeDescriptor (org.kie.kogito.persistence.api.proto.AttributeDescriptor)3 Scalars (graphql.Scalars)2 ExtendedScalars (graphql.scalars.ExtendedScalars)2 GraphQLOutputType (graphql.schema.GraphQLOutputType)2 Consumer (java.util.function.Consumer)2 Collectors.toList (java.util.stream.Collectors.toList)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Observes (javax.enterprise.event.Observes)2 Inject (javax.inject.Inject)2 KOGITO_DOMAIN_ATTRIBUTE (org.kie.kogito.index.Constants.KOGITO_DOMAIN_ATTRIBUTE)2