Skip to content

fix: Description is not inherited from the parent class #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package com.introproventures.graphql.jpa.query.schema.impl;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -46,14 +41,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnore;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreFilter;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreOrder;
import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
import com.introproventures.graphql.jpa.query.schema.JavaScalars;
import com.introproventures.graphql.jpa.query.schema.NamingStrategy;
import com.introproventures.graphql.jpa.query.schema.impl.IntrospectionUtils.CachedIntrospectionResult.CachedPropertyDescriptor;
import com.introproventures.graphql.jpa.query.schema.impl.IntrospectionUtils.EntityIntrospectionResult.AttributePropertyDescriptor;
import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria;

import graphql.Assert;
Expand Down Expand Up @@ -164,7 +158,7 @@ private GraphQLObjectType getQueryType() {
private GraphQLFieldDefinition getQueryFieldByIdDefinition(EntityType<?> entityType) {
return GraphQLFieldDefinition.newFieldDefinition()
.name(entityType.getName())
.description(getSchemaDescription( entityType.getJavaType()))
.description(getSchemaDescription(entityType))
.type(getObjectType(entityType))
.dataFetcher(new GraphQLJpaSimpleDataFetcher(entityManager, entityType, toManyDefaultOptional))
.argument(entityType.getAttributes().stream()
Expand Down Expand Up @@ -426,7 +420,7 @@ private GraphQLInputObjectField getWhereInputRelationField(Attribute<?,?> attrib
ManagedType<?> foreignType = getForeignType(attribute);

String type = resolveWhereInputTypeName(foreignType);
String description = getSchemaDescription(attribute.getJavaMember());
String description = getSchemaDescription(attribute);

return GraphQLInputObjectField.newInputObjectField()
.name(attribute.getName())
Expand All @@ -437,7 +431,7 @@ private GraphQLInputObjectField getWhereInputRelationField(Attribute<?,?> attrib

private GraphQLInputObjectField getWhereInputField(Attribute<?,?> attribute) {
GraphQLInputType type = getWhereAttributeType(attribute);
String description = getSchemaDescription(attribute.getJavaMember());
String description = getSchemaDescription(attribute);

if (type instanceof GraphQLInputType) {
return GraphQLInputObjectField.newInputObjectField()
Expand Down Expand Up @@ -599,7 +593,7 @@ else if (attribute.getJavaMember().getClass().isAssignableFrom(Field.class)

private GraphQLArgument getArgument(Attribute<?,?> attribute) {
GraphQLInputType type = getAttributeInputType(attribute);
String description = getSchemaDescription(attribute.getJavaMember());
String description = getSchemaDescription(attribute);

return GraphQLArgument.newArgument()
.name(attribute.getName())
Expand All @@ -619,7 +613,7 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
if (input) {
graphQLType = GraphQLInputObjectType.newInputObject()
.name(embeddableTypeName)
.description(getSchemaDescription(embeddableType.getJavaType()))
.description(getSchemaDescription(embeddableType))
.fields(embeddableType.getAttributes().stream()
.filter(this::isNotIgnored)
.map(this::getInputObjectField)
Expand All @@ -629,7 +623,7 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
} else {
graphQLType = GraphQLObjectType.newObject()
.name(embeddableTypeName)
.description(getSchemaDescription(embeddableType.getJavaType()))
.description(getSchemaDescription(embeddableType))
.fields(embeddableType.getAttributes().stream()
.filter(this::isNotIgnored)
.map(this::getObjectField)
Expand All @@ -655,7 +649,7 @@ private GraphQLObjectType getObjectType(EntityType<?> entityType) {
private GraphQLObjectType computeObjectType(EntityType<?> entityType) {
return GraphQLObjectType.newObject()
.name(entityType.getName())
.description(getSchemaDescription(entityType.getJavaType()))
.description(getSchemaDescription(entityType))
.fields(getEntityAttributesFields(entityType))
.fields(getTransientFields(entityType.getJavaType()))
.build();
Expand All @@ -673,27 +667,29 @@ private List<GraphQLFieldDefinition> getEntityAttributesFields(EntityType<?> ent
private List<GraphQLFieldDefinition> getTransientFields(Class<?> clazz) {
return IntrospectionUtils.introspect(clazz)
.getPropertyDescriptors().stream()
.filter(it -> IntrospectionUtils.isTransient(clazz, it.getName()))
.filter(it -> !it.isAnnotationPresent(GraphQLIgnore.class))
.map(CachedPropertyDescriptor::getDelegate)
.filter(AttributePropertyDescriptor::isTransient)
.filter(AttributePropertyDescriptor::isNotIgnored)
.map(this::getJavaFieldDefinition)
.collect(Collectors.toList());
}

@SuppressWarnings( { "rawtypes" } )
private GraphQLFieldDefinition getJavaFieldDefinition(PropertyDescriptor propertyDescriptor) {
private GraphQLFieldDefinition getJavaFieldDefinition(AttributePropertyDescriptor propertyDescriptor) {
GraphQLOutputType type = getGraphQLTypeFromJavaType(propertyDescriptor.getPropertyType());
DataFetcher dataFetcher = PropertyDataFetcher.fetching(propertyDescriptor.getName());

String description = propertyDescriptor.getSchemaDescription()
.orElse(null);

return GraphQLFieldDefinition.newFieldDefinition()
.name(propertyDescriptor.getName())
.description(getSchemaDescription(propertyDescriptor.getPropertyType()))
.type(type)
.dataFetcher(dataFetcher)
.build();
.name(propertyDescriptor.getName())
.description(description)
.type(type)
.dataFetcher(dataFetcher)
.build();
}

private GraphQLFieldDefinition getObjectField(Attribute attribute) {
private GraphQLFieldDefinition getObjectField(Attribute<?,?> attribute) {
return getObjectField(attribute, null);
}

Expand Down Expand Up @@ -751,7 +747,7 @@ else if (attribute instanceof PluralAttribute

return GraphQLFieldDefinition.newFieldDefinition()
.name(attribute.getName())
.description(getSchemaDescription(attribute.getJavaMember()))
.description(getSchemaDescription(attribute))
.type(type)
.dataFetcher(dataFetcher)
.argument(arguments)
Expand Down Expand Up @@ -780,7 +776,7 @@ private GraphQLInputObjectField getInputObjectField(Attribute attribute) {

return GraphQLInputObjectField.newInputObjectField()
.name(attribute.getName())
.description(getSchemaDescription(attribute.getJavaMember()))
.description(getSchemaDescription(attribute))
.type(type)
.build();
}
Expand Down Expand Up @@ -878,86 +874,27 @@ protected final boolean isValidAssociation(Attribute<?,?> attribute) {
return isOneToMany(attribute) || isToOne(attribute);
}



private String getSchemaDescription(Member member) {
if (member instanceof AnnotatedElement) {
String desc = getSchemaDescription((AnnotatedElement) member);
if (desc != null) {
return(desc);
}
}

//The given Member has no @GraphQLDescription set.
//If the Member is a Method it might be a getter/setter, see if the property it represents
//is annotated with @GraphQLDescription
//Alternatively if the Member is a Field its getter might be annotated, see if its getter
//is annotated with @GraphQLDescription
if (member instanceof Method) {
Field fieldMember = getFieldByAccessor((Method)member);
if (fieldMember != null) {
return(getSchemaDescription((AnnotatedElement) fieldMember));
}
} else if (member instanceof Field) {
Method fieldGetter = getGetterOfField((Field)member);
if (fieldGetter != null) {
return(getSchemaDescription((AnnotatedElement) fieldGetter));
}
}

return null;
private String getSchemaDescription(Attribute<?,?> attribute) {
return IntrospectionUtils.introspect(attribute.getDeclaringType()
.getJavaType())
.getPropertyDescriptor(attribute)
.flatMap(AttributePropertyDescriptor::getSchemaDescription)
.orElse(null);
}

private Method getGetterOfField(Field field) {
try {
Class<?> clazz = field.getDeclaringClass();
BeanInfo info = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (PropertyDescriptor pd : props) {
if (pd.getName().equals(field.getName())) {
return(pd.getReadMethod());
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
}

return(null);
}

//from https://stackoverflow.com/questions/13192734/getting-a-property-field-name-using-getter-method-of-a-pojo-java-bean/13514566
private static Field getFieldByAccessor(Method method) {
try {
Class<?> clazz = method.getDeclaringClass();
BeanInfo info = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (PropertyDescriptor pd : props) {
if(method.equals(pd.getWriteMethod()) || method.equals(pd.getReadMethod())) {
String fieldName = pd.getName();
try {
return(clazz.getDeclaredField(fieldName));
} catch (Throwable t) {
log.error("class '" + clazz.getName() + "' contains method '" + method.getName() + "' which is an accessor for a Field named '" + fieldName + "', error getting the field:", t);
return(null);
}
}
}
} catch (Throwable t) {
log.error("error finding Field for accessor with name '" + method.getName() + "'", t);
}

return null;

private String getSchemaDescription(EntityType<?> entityType) {
return IntrospectionUtils.introspect(entityType.getJavaType())
.getSchemaDescription()
.orElse(null);
}

private String getSchemaDescription(AnnotatedElement annotatedElement) {
if (annotatedElement != null) {
GraphQLDescription schemaDocumentation = annotatedElement.getAnnotation(GraphQLDescription.class);
return schemaDocumentation != null ? schemaDocumentation.value() : null;
}

return null;
private String getSchemaDescription(EmbeddableType<?> embeddableType) {
return IntrospectionUtils.introspect(embeddableType.getJavaType())
.getSchemaDescription()
.orElse(null);
}

private boolean isNotIgnored(EmbeddableType<?> attribute) {
return isNotIgnored(attribute.getJavaType());
}
Expand Down
Loading