Search in sources :

Example 1 with GQLTypeCache

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();
}
Also used : GraphQLObjectType(graphql.schema.GraphQLObjectType) ImmutableSet(com.google.common.collect.ImmutableSet) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLScalarType(graphql.schema.GraphQLScalarType) GQLTypeCache(net.nemerosa.ontrack.graphql.schema.GQLTypeCache) Collection(java.util.Collection) LocalDateTime(java.time.LocalDateTime) GraphQLInputType(graphql.schema.GraphQLInputType) Set(java.util.Set) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet) Scalars(graphql.Scalars) PropertyDescriptor(java.beans.PropertyDescriptor) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Method(java.lang.reflect.Method) BeanUtils(org.springframework.beans.BeanUtils) GraphQLInputObjectType.newInputObject(graphql.schema.GraphQLInputObjectType.newInputObject) PropertyDescriptor(java.beans.PropertyDescriptor) GraphQLObjectType(graphql.schema.GraphQLObjectType) HashSet(java.util.HashSet) GraphQLScalarType(graphql.schema.GraphQLScalarType)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Scalars (graphql.Scalars)1 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)1 GraphQLInputObjectType.newInputObject (graphql.schema.GraphQLInputObjectType.newInputObject)1 GraphQLInputType (graphql.schema.GraphQLInputType)1 GraphQLObjectType (graphql.schema.GraphQLObjectType)1 GraphQLScalarType (graphql.schema.GraphQLScalarType)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 LocalDateTime (java.time.LocalDateTime)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 GQLTypeCache (net.nemerosa.ontrack.graphql.schema.GQLTypeCache)1 BeanUtils (org.springframework.beans.BeanUtils)1