17
17
package com .introproventures .graphql .jpa .query .schema .impl ;
18
18
19
19
import static graphql .Scalars .GraphQLBoolean ;
20
- import static graphql .introspection .Introspection .DirectiveLocation .FIELD ;
21
20
import static graphql .schema .GraphQLArgument .newArgument ;
22
21
import static graphql .schema .GraphQLInputObjectField .newInputObjectField ;
23
22
import static graphql .schema .GraphQLInputObjectType .newInputObject ;
24
- import static graphql .schema .GraphQLNonNull .nonNull ;
25
23
26
24
import java .beans .Introspector ;
27
25
import java .lang .reflect .AnnotatedElement ;
49
47
import javax .persistence .metamodel .SingularAttribute ;
50
48
import javax .persistence .metamodel .Type ;
51
49
52
- import graphql .schema .GraphQLDirective ;
53
50
import org .dataloader .MappedBatchLoaderWithContext ;
54
51
import org .slf4j .Logger ;
55
52
import org .slf4j .LoggerFactory ;
64
61
import com .introproventures .graphql .jpa .query .schema .impl .EntityIntrospector .EntityIntrospectionResult .AttributePropertyDescriptor ;
65
62
import com .introproventures .graphql .jpa .query .schema .impl .PredicateFilter .Criteria ;
66
63
import com .introproventures .graphql .jpa .query .schema .relay .GraphQLJpaRelayDataFetcher ;
67
-
68
64
import graphql .Assert ;
69
- import graphql .Directives ;
70
65
import graphql .Scalars ;
71
66
import graphql .relay .Relay ;
72
67
import graphql .schema .Coercing ;
86
81
import graphql .schema .GraphQLTypeReference ;
87
82
import graphql .schema .PropertyDataFetcher ;
88
83
84
+
89
85
/**
90
86
* JPA specific schema builder implementation of {code #GraphQLSchemaBuilder} interface
91
87
*
@@ -136,7 +132,7 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
136
132
private int defaultFetchSize = 100 ;
137
133
private int defaultPageLimitSize = 100 ;
138
134
private boolean enableDefaultMaxResults = true ;
139
-
135
+
140
136
private RestrictedKeysProvider restrictedKeysProvider = (entityDescriptor ) -> Optional .of (Collections .emptyList ());
141
137
142
138
private final Relay relay = new Relay ();
@@ -505,7 +501,7 @@ private String resolveTypeName(ManagedType<?> managedType) {
505
501
String typeName ="" ;
506
502
507
503
if (managedType instanceof EmbeddableType ){
508
- typeName = managedType .getJavaType ().getSimpleName ()+ "EmbeddableType" ;
504
+ typeName = managedType .getJavaType ().getSimpleName ();
509
505
} else if (managedType instanceof EntityType ) {
510
506
typeName = ((EntityType <?>)managedType ).getName ();
511
507
}
@@ -514,7 +510,14 @@ private String resolveTypeName(ManagedType<?> managedType) {
514
510
}
515
511
516
512
private GraphQLInputObjectType getWhereInputType (ManagedType <?> managedType ) {
517
- return inputObjectCache .computeIfAbsent (managedType , this ::computeWhereInputType );
513
+ GraphQLInputObjectType type = inputObjectCache .get (managedType );
514
+ if (type == null ) {
515
+ type = computeWhereInputType (managedType );
516
+ inputObjectCache .put (managedType , type );
517
+ return type ;
518
+ }
519
+ return type ;
520
+
518
521
}
519
522
520
523
private String resolveWhereInputTypeName (ManagedType <?> managedType ) {
@@ -610,6 +613,11 @@ private GraphQLInputType getWhereAttributeType(Attribute<?,?> attribute) {
610
613
if (whereAttributesMap .containsKey (type ))
611
614
return whereAttributesMap .get (type );
612
615
616
+ if (isEmbeddable (attribute )) {
617
+ EmbeddableType <?> embeddableType = (EmbeddableType <?>) ((SingularAttribute <?, ?>) attribute ).getType ();
618
+ return getWhereInputType (embeddableType );
619
+ }
620
+
613
621
GraphQLInputObjectType .Builder builder = GraphQLInputObjectType .newInputObject ()
614
622
.name (type )
615
623
.description ("Criteria expression specification of " +namingStrategy .singularize (attribute .getName ())+" attribute in entity " + attribute .getDeclaringType ().getJavaType ())
@@ -786,7 +794,7 @@ else if (attribute.getJavaMember().getClass().isAssignableFrom(Field.class)
786
794
}
787
795
788
796
private GraphQLArgument getArgument (Attribute <?,?> attribute ) {
789
- GraphQLInputType type = getAttributeInputType (attribute );
797
+ GraphQLInputType type = getAttributeInputTypeForSearchByIdArg (attribute );
790
798
String description = getSchemaDescription (attribute );
791
799
792
800
return GraphQLArgument .newArgument ()
@@ -796,25 +804,33 @@ private GraphQLArgument getArgument(Attribute<?,?> attribute) {
796
804
.build ();
797
805
}
798
806
799
- private GraphQLType getEmbeddableType (EmbeddableType <?> embeddableType , boolean input ) {
800
- if (input && embeddableInputCache .containsKey (embeddableType .getJavaType ()))
801
- return embeddableInputCache .get (embeddableType .getJavaType ());
802
-
803
- if (!input && embeddableOutputCache .containsKey (embeddableType .getJavaType ()))
804
- return embeddableOutputCache .get (embeddableType .getJavaType ());
805
- String embeddableTypeName = namingStrategy .singularize (embeddableType .getJavaType ().getSimpleName ())+ (input ? "Input" : "" ) +"EmbeddableType" ;
806
- GraphQLType graphQLType =null ;
807
+ private GraphQLType getEmbeddableType (EmbeddableType <?> embeddableType , boolean input , boolean searchByIdArg ) {
808
+ GraphQLType graphQLType ;
807
809
if (input ) {
808
- graphQLType = GraphQLInputObjectType .newInputObject ()
809
- .name (embeddableTypeName )
810
+
811
+ if (searchByIdArg ) {
812
+ if (embeddableInputCache .containsKey (embeddableType .getJavaType ())) {
813
+ return embeddableInputCache .get (embeddableType .getJavaType ());
814
+ }
815
+ graphQLType = GraphQLInputObjectType .newInputObject ()
816
+ .name (namingStrategy .singularize (embeddableType .getJavaType ().getSimpleName ())+ "InputEmbeddableIdType" )
810
817
.description (getSchemaDescription (embeddableType ))
811
818
.fields (embeddableType .getAttributes ().stream ()
812
- .filter (this ::isNotIgnored )
813
- .map (this ::getInputObjectField )
814
- .collect (Collectors .toList ())
819
+ .filter (this ::isNotIgnored )
820
+ .map (this ::getInputObjectField )
821
+ .collect (Collectors .toList ())
815
822
)
816
823
.build ();
824
+ embeddableInputCache .put (embeddableType .getJavaType (), (GraphQLInputObjectType ) graphQLType );
825
+ return graphQLType ;
826
+ }
827
+
828
+ graphQLType = getWhereInputType (embeddableType );
817
829
} else {
830
+ if (embeddableOutputCache .containsKey (embeddableType .getJavaType ())) {
831
+ return embeddableOutputCache .get (embeddableType .getJavaType ());
832
+ }
833
+ String embeddableTypeName = namingStrategy .singularize (embeddableType .getJavaType ().getSimpleName ()) + "EmbeddableType" ;
818
834
graphQLType = GraphQLObjectType .newObject ()
819
835
.name (embeddableTypeName )
820
836
.description (getSchemaDescription (embeddableType ))
@@ -824,13 +840,8 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
824
840
.collect (Collectors .toList ())
825
841
)
826
842
.build ();
827
- }
828
- if (input ) {
829
- embeddableInputCache .putIfAbsent (embeddableType .getJavaType (), (GraphQLInputObjectType ) graphQLType );
830
- } else {
831
843
embeddableOutputCache .putIfAbsent (embeddableType .getJavaType (), (GraphQLObjectType ) graphQLType );
832
844
}
833
-
834
845
return graphQLType ;
835
846
}
836
847
@@ -1020,31 +1031,39 @@ private Stream<Attribute<?,?>> findBasicAttributes(Collection<Attribute<?,?>> at
1020
1031
}
1021
1032
1022
1033
private GraphQLInputType getAttributeInputType (Attribute <?,?> attribute ) {
1034
+ return getAttributeInputType (attribute , false );
1035
+ }
1036
+
1037
+ private GraphQLInputType getAttributeInputTypeForSearchByIdArg (Attribute <?,?> attribute ) {
1038
+ return getAttributeInputType (attribute , true );
1039
+ }
1040
+
1041
+ private GraphQLInputType getAttributeInputType (Attribute <?,?> attribute , boolean searchByIdArgType ) {
1023
1042
1024
1043
try {
1025
- return (GraphQLInputType ) getAttributeType (attribute , true );
1044
+ return (GraphQLInputType ) getAttributeType (attribute , true , searchByIdArgType );
1026
1045
} catch (ClassCastException e ){
1027
1046
throw new IllegalArgumentException ("Attribute " + attribute + " cannot be mapped as an Input Argument" );
1028
1047
}
1029
1048
}
1030
1049
1031
1050
private GraphQLOutputType getAttributeOutputType (Attribute <?,?> attribute ) {
1032
1051
try {
1033
- return (GraphQLOutputType ) getAttributeType (attribute , false );
1052
+ return (GraphQLOutputType ) getAttributeType (attribute , false , false );
1034
1053
} catch (ClassCastException e ) {
1035
1054
throw new IllegalArgumentException ("Attribute " + attribute + " cannot be mapped as an Output Argument" );
1036
1055
}
1037
1056
}
1038
1057
1039
1058
@ SuppressWarnings ( "rawtypes" )
1040
- protected GraphQLType getAttributeType (Attribute <?,?> attribute , boolean input ) {
1059
+ protected GraphQLType getAttributeType (Attribute <?,?> attribute , boolean input , boolean searchByIdArgType ) {
1041
1060
1042
1061
if (isBasic (attribute )) {
1043
1062
return getGraphQLTypeFromJavaType (attribute .getJavaType ());
1044
1063
}
1045
1064
else if (isEmbeddable (attribute )) {
1046
1065
EmbeddableType embeddableType = (EmbeddableType ) ((SingularAttribute ) attribute ).getType ();
1047
- return getEmbeddableType (embeddableType , input );
1066
+ return getEmbeddableType (embeddableType , input , searchByIdArgType );
1048
1067
}
1049
1068
else if (isToMany (attribute )) {
1050
1069
EntityType foreignType = (EntityType ) ((PluralAttribute ) attribute ).getElementType ();
@@ -1066,8 +1085,7 @@ else if (isElementCollection(attribute)) {
1066
1085
}
1067
1086
else if (foreignType .getPersistenceType () == Type .PersistenceType .EMBEDDABLE ) {
1068
1087
EmbeddableType embeddableType = EmbeddableType .class .cast (foreignType );
1069
- GraphQLType graphQLType = getEmbeddableType (embeddableType ,
1070
- input );
1088
+ GraphQLType graphQLType = getEmbeddableType (embeddableType , input , searchByIdArgType );
1071
1089
1072
1090
return input ? graphQLType : new GraphQLList (graphQLType );
1073
1091
}
0 commit comments