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));
}
});
}
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);
});
}
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;
}
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));
}
Aggregations