16
16
17
17
package com .introproventures .graphql .jpa .query .schema .impl ;
18
18
19
- import java .beans .BeanInfo ;
20
- import java .beans .IntrospectionException ;
21
- import java .beans .Introspector ;
22
- import java .beans .PropertyDescriptor ;
23
19
import java .lang .reflect .AnnotatedElement ;
24
20
import java .lang .reflect .Field ;
25
21
import java .lang .reflect .Member ;
26
- import java .lang .reflect .Method ;
27
22
import java .util .ArrayList ;
28
23
import java .util .Collection ;
29
24
import java .util .HashMap ;
46
41
import org .slf4j .Logger ;
47
42
import org .slf4j .LoggerFactory ;
48
43
49
- import com .introproventures .graphql .jpa .query .annotation .GraphQLDescription ;
50
44
import com .introproventures .graphql .jpa .query .annotation .GraphQLIgnore ;
51
45
import com .introproventures .graphql .jpa .query .annotation .GraphQLIgnoreFilter ;
52
46
import com .introproventures .graphql .jpa .query .annotation .GraphQLIgnoreOrder ;
53
47
import com .introproventures .graphql .jpa .query .schema .GraphQLSchemaBuilder ;
54
48
import com .introproventures .graphql .jpa .query .schema .JavaScalars ;
55
49
import com .introproventures .graphql .jpa .query .schema .NamingStrategy ;
56
- import com .introproventures .graphql .jpa .query .schema .impl .IntrospectionUtils .CachedIntrospectionResult . CachedPropertyDescriptor ;
50
+ import com .introproventures .graphql .jpa .query .schema .impl .IntrospectionUtils .EntityIntrospectionResult . AttributePropertyDescriptor ;
57
51
import com .introproventures .graphql .jpa .query .schema .impl .PredicateFilter .Criteria ;
58
52
59
53
import graphql .Assert ;
@@ -164,7 +158,7 @@ private GraphQLObjectType getQueryType() {
164
158
private GraphQLFieldDefinition getQueryFieldByIdDefinition (EntityType <?> entityType ) {
165
159
return GraphQLFieldDefinition .newFieldDefinition ()
166
160
.name (entityType .getName ())
167
- .description (getSchemaDescription ( entityType . getJavaType () ))
161
+ .description (getSchemaDescription (entityType ))
168
162
.type (getObjectType (entityType ))
169
163
.dataFetcher (new GraphQLJpaSimpleDataFetcher (entityManager , entityType , toManyDefaultOptional ))
170
164
.argument (entityType .getAttributes ().stream ()
@@ -426,7 +420,7 @@ private GraphQLInputObjectField getWhereInputRelationField(Attribute<?,?> attrib
426
420
ManagedType <?> foreignType = getForeignType (attribute );
427
421
428
422
String type = resolveWhereInputTypeName (foreignType );
429
- String description = getSchemaDescription (attribute . getJavaMember () );
423
+ String description = getSchemaDescription (attribute );
430
424
431
425
return GraphQLInputObjectField .newInputObjectField ()
432
426
.name (attribute .getName ())
@@ -437,7 +431,7 @@ private GraphQLInputObjectField getWhereInputRelationField(Attribute<?,?> attrib
437
431
438
432
private GraphQLInputObjectField getWhereInputField (Attribute <?,?> attribute ) {
439
433
GraphQLInputType type = getWhereAttributeType (attribute );
440
- String description = getSchemaDescription (attribute . getJavaMember () );
434
+ String description = getSchemaDescription (attribute );
441
435
442
436
if (type instanceof GraphQLInputType ) {
443
437
return GraphQLInputObjectField .newInputObjectField ()
@@ -599,7 +593,7 @@ else if (attribute.getJavaMember().getClass().isAssignableFrom(Field.class)
599
593
600
594
private GraphQLArgument getArgument (Attribute <?,?> attribute ) {
601
595
GraphQLInputType type = getAttributeInputType (attribute );
602
- String description = getSchemaDescription (attribute . getJavaMember () );
596
+ String description = getSchemaDescription (attribute );
603
597
604
598
return GraphQLArgument .newArgument ()
605
599
.name (attribute .getName ())
@@ -619,7 +613,7 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
619
613
if (input ) {
620
614
graphQLType = GraphQLInputObjectType .newInputObject ()
621
615
.name (embeddableTypeName )
622
- .description (getSchemaDescription (embeddableType . getJavaType () ))
616
+ .description (getSchemaDescription (embeddableType ))
623
617
.fields (embeddableType .getAttributes ().stream ()
624
618
.filter (this ::isNotIgnored )
625
619
.map (this ::getInputObjectField )
@@ -629,7 +623,7 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
629
623
} else {
630
624
graphQLType = GraphQLObjectType .newObject ()
631
625
.name (embeddableTypeName )
632
- .description (getSchemaDescription (embeddableType . getJavaType () ))
626
+ .description (getSchemaDescription (embeddableType ))
633
627
.fields (embeddableType .getAttributes ().stream ()
634
628
.filter (this ::isNotIgnored )
635
629
.map (this ::getObjectField )
@@ -655,7 +649,7 @@ private GraphQLObjectType getObjectType(EntityType<?> entityType) {
655
649
private GraphQLObjectType computeObjectType (EntityType <?> entityType ) {
656
650
return GraphQLObjectType .newObject ()
657
651
.name (entityType .getName ())
658
- .description (getSchemaDescription (entityType . getJavaType () ))
652
+ .description (getSchemaDescription (entityType ))
659
653
.fields (getEntityAttributesFields (entityType ))
660
654
.fields (getTransientFields (entityType .getJavaType ()))
661
655
.build ();
@@ -673,27 +667,29 @@ private List<GraphQLFieldDefinition> getEntityAttributesFields(EntityType<?> ent
673
667
private List <GraphQLFieldDefinition > getTransientFields (Class <?> clazz ) {
674
668
return IntrospectionUtils .introspect (clazz )
675
669
.getPropertyDescriptors ().stream ()
676
- .filter (it -> IntrospectionUtils .isTransient (clazz , it .getName ()))
677
- .filter (it -> !it .isAnnotationPresent (GraphQLIgnore .class ))
678
- .map (CachedPropertyDescriptor ::getDelegate )
670
+ .filter (AttributePropertyDescriptor ::isTransient )
671
+ .filter (AttributePropertyDescriptor ::isNotIgnored )
679
672
.map (this ::getJavaFieldDefinition )
680
673
.collect (Collectors .toList ());
681
674
}
682
675
683
676
@ SuppressWarnings ( { "rawtypes" } )
684
- private GraphQLFieldDefinition getJavaFieldDefinition (PropertyDescriptor propertyDescriptor ) {
677
+ private GraphQLFieldDefinition getJavaFieldDefinition (AttributePropertyDescriptor propertyDescriptor ) {
685
678
GraphQLOutputType type = getGraphQLTypeFromJavaType (propertyDescriptor .getPropertyType ());
686
679
DataFetcher dataFetcher = PropertyDataFetcher .fetching (propertyDescriptor .getName ());
680
+
681
+ String description = propertyDescriptor .getSchemaDescription ()
682
+ .orElse (null );
687
683
688
684
return GraphQLFieldDefinition .newFieldDefinition ()
689
- .name (propertyDescriptor .getName ())
690
- .description (getSchemaDescription ( propertyDescriptor . getPropertyType ()) )
691
- .type (type )
692
- .dataFetcher (dataFetcher )
693
- .build ();
685
+ .name (propertyDescriptor .getName ())
686
+ .description (description )
687
+ .type (type )
688
+ .dataFetcher (dataFetcher )
689
+ .build ();
694
690
}
695
691
696
- private GraphQLFieldDefinition getObjectField (Attribute attribute ) {
692
+ private GraphQLFieldDefinition getObjectField (Attribute <?,?> attribute ) {
697
693
return getObjectField (attribute , null );
698
694
}
699
695
@@ -751,7 +747,7 @@ else if (attribute instanceof PluralAttribute
751
747
752
748
return GraphQLFieldDefinition .newFieldDefinition ()
753
749
.name (attribute .getName ())
754
- .description (getSchemaDescription (attribute . getJavaMember () ))
750
+ .description (getSchemaDescription (attribute ))
755
751
.type (type )
756
752
.dataFetcher (dataFetcher )
757
753
.argument (arguments )
@@ -780,7 +776,7 @@ private GraphQLInputObjectField getInputObjectField(Attribute attribute) {
780
776
781
777
return GraphQLInputObjectField .newInputObjectField ()
782
778
.name (attribute .getName ())
783
- .description (getSchemaDescription (attribute . getJavaMember () ))
779
+ .description (getSchemaDescription (attribute ))
784
780
.type (type )
785
781
.build ();
786
782
}
@@ -878,86 +874,27 @@ protected final boolean isValidAssociation(Attribute<?,?> attribute) {
878
874
return isOneToMany (attribute ) || isToOne (attribute );
879
875
}
880
876
881
-
882
-
883
- private String getSchemaDescription (Member member ) {
884
- if (member instanceof AnnotatedElement ) {
885
- String desc = getSchemaDescription ((AnnotatedElement ) member );
886
- if (desc != null ) {
887
- return (desc );
888
- }
889
- }
890
-
891
- //The given Member has no @GraphQLDescription set.
892
- //If the Member is a Method it might be a getter/setter, see if the property it represents
893
- //is annotated with @GraphQLDescription
894
- //Alternatively if the Member is a Field its getter might be annotated, see if its getter
895
- //is annotated with @GraphQLDescription
896
- if (member instanceof Method ) {
897
- Field fieldMember = getFieldByAccessor ((Method )member );
898
- if (fieldMember != null ) {
899
- return (getSchemaDescription ((AnnotatedElement ) fieldMember ));
900
- }
901
- } else if (member instanceof Field ) {
902
- Method fieldGetter = getGetterOfField ((Field )member );
903
- if (fieldGetter != null ) {
904
- return (getSchemaDescription ((AnnotatedElement ) fieldGetter ));
905
- }
906
- }
907
877
908
- return null ;
878
+ private String getSchemaDescription (Attribute <?,?> attribute ) {
879
+ return IntrospectionUtils .introspect (attribute .getDeclaringType ()
880
+ .getJavaType ())
881
+ .getPropertyDescriptor (attribute )
882
+ .flatMap (AttributePropertyDescriptor ::getSchemaDescription )
883
+ .orElse (null );
909
884
}
910
-
911
- private Method getGetterOfField (Field field ) {
912
- try {
913
- Class <?> clazz = field .getDeclaringClass ();
914
- BeanInfo info = Introspector .getBeanInfo (clazz );
915
- PropertyDescriptor [] props = info .getPropertyDescriptors ();
916
- for (PropertyDescriptor pd : props ) {
917
- if (pd .getName ().equals (field .getName ())) {
918
- return (pd .getReadMethod ());
919
- }
920
- }
921
- } catch (IntrospectionException e ) {
922
- e .printStackTrace ();
923
- }
924
-
925
- return (null );
926
- }
927
-
928
- //from https://stackoverflow.com/questions/13192734/getting-a-property-field-name-using-getter-method-of-a-pojo-java-bean/13514566
929
- private static Field getFieldByAccessor (Method method ) {
930
- try {
931
- Class <?> clazz = method .getDeclaringClass ();
932
- BeanInfo info = Introspector .getBeanInfo (clazz );
933
- PropertyDescriptor [] props = info .getPropertyDescriptors ();
934
- for (PropertyDescriptor pd : props ) {
935
- if (method .equals (pd .getWriteMethod ()) || method .equals (pd .getReadMethod ())) {
936
- String fieldName = pd .getName ();
937
- try {
938
- return (clazz .getDeclaredField (fieldName ));
939
- } catch (Throwable t ) {
940
- log .error ("class '" + clazz .getName () + "' contains method '" + method .getName () + "' which is an accessor for a Field named '" + fieldName + "', error getting the field:" , t );
941
- return (null );
942
- }
943
- }
944
- }
945
- } catch (Throwable t ) {
946
- log .error ("error finding Field for accessor with name '" + method .getName () + "'" , t );
947
- }
948
-
949
- return null ;
885
+
886
+ private String getSchemaDescription (EntityType <?> entityType ) {
887
+ return IntrospectionUtils .introspect (entityType .getJavaType ())
888
+ .getSchemaDescription ()
889
+ .orElse (null );
950
890
}
951
891
952
- private String getSchemaDescription (AnnotatedElement annotatedElement ) {
953
- if (annotatedElement != null ) {
954
- GraphQLDescription schemaDocumentation = annotatedElement .getAnnotation (GraphQLDescription .class );
955
- return schemaDocumentation != null ? schemaDocumentation .value () : null ;
956
- }
957
-
958
- return null ;
892
+ private String getSchemaDescription (EmbeddableType <?> embeddableType ) {
893
+ return IntrospectionUtils .introspect (embeddableType .getJavaType ())
894
+ .getSchemaDescription ()
895
+ .orElse (null );
959
896
}
960
-
897
+
961
898
private boolean isNotIgnored (EmbeddableType <?> attribute ) {
962
899
return isNotIgnored (attribute .getJavaType ());
963
900
}
0 commit comments