use of net.nemerosa.ontrack.graphql.schema.GQLTypeCache in project ontrack by nemerosa.
the class GraphQLBeanConverter method asObjectType.
public static GraphQLObjectType asObjectType(Class<?> type, GQLTypeCache cache, Set<String> exclusions) {
GraphQLObjectType.Builder builder = GraphQLObjectType.newObject().name(type.getSimpleName());
// Actual exclusions
Set<String> actualExclusions = new HashSet<>(DEFAULT_EXCLUSIONS);
if (exclusions != null) {
actualExclusions.addAll(exclusions);
}
// Gets the properties for the type
for (PropertyDescriptor descriptor : BeanUtils.getPropertyDescriptors(type)) {
if (descriptor.getReadMethod() != null) {
String name = descriptor.getName();
// Excludes some names by defaults
if (!actualExclusions.contains(name)) {
String description = descriptor.getShortDescription();
Class<?> propertyType = descriptor.getPropertyType();
GraphQLScalarType scalarType = getScalarType(propertyType);
if (scalarType != null) {
builder = builder.field(field -> field.name(name).description(description).type(scalarType));
} else // Maps & collections not supported yet
if (Map.class.isAssignableFrom(propertyType) || Collection.class.isAssignableFrom(propertyType)) {
throw new IllegalArgumentException(String.format("Maps and collections are not supported yet: %s in %s", name, type.getName()));
} else {
// Tries to convert to an object type
// Note: caching might be needed here...
GraphQLObjectType propertyObjectType = cache.getOrCreate(propertyType.getSimpleName(), () -> asObjectType(propertyType, cache));
builder = builder.field(field -> field.name(name).description(description).type(propertyObjectType));
}
}
}
}
// OK
return builder.build();
}
Aggregations