3030
3131import javax .persistence .EntityManager ;
3232import javax .persistence .metamodel .Attribute ;
33+ import javax .persistence .metamodel .EmbeddableType ;
3334import javax .persistence .metamodel .EntityType ;
3435import javax .persistence .metamodel .PluralAttribute ;
3536import javax .persistence .metamodel .SingularAttribute ;
@@ -90,6 +91,7 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
9091
9192 private Map <Class <?>, GraphQLType > classCache = new HashMap <>();
9293 private Map <EntityType <?>, GraphQLObjectType > entityCache = new HashMap <>();
94+ private Map <EmbeddableType <?>, GraphQLObjectType > embeddableCache = new HashMap <>();
9395
9496 private static final Logger log = LoggerFactory .getLogger (GraphQLJpaSchemaBuilder .class );
9597
@@ -127,14 +129,6 @@ private GraphQLObjectType getQueryType() {
127129 .collect (Collectors .toList ())
128130 );
129131
130- // queryType.fields(
131- // entityManager.getMetamodel()
132- // .getEntities().stream()
133- // .filter(this::isNotIgnored)
134- // .map(this::getQueryFieldDefinition)
135- // .collect(Collectors.toList())
136- // );
137-
138132 queryType .fields (
139133 entityManager .getMetamodel ()
140134 .getEntities ().stream ()
@@ -146,21 +140,6 @@ private GraphQLObjectType getQueryType() {
146140 return queryType .build ();
147141 }
148142
149- private GraphQLFieldDefinition getQueryFieldDefinition (EntityType <?> entityType ) {
150- return GraphQLFieldDefinition .newFieldDefinition ()
151- .name (namingStrategy .pluralize (entityType .getName ()))
152- .description (getSchemaDescription ( entityType .getJavaType ()))
153- .type (new GraphQLList (getObjectType (entityType )))
154- .dataFetcher (new QraphQLJpaBaseDataFetcher (entityManager , entityType ))
155- .argument (entityType .getAttributes ().stream ()
156- .filter (this ::isValidInput )
157- .filter (this ::isNotIgnored )
158- .map (this ::getArgument )
159- .collect (Collectors .toList ())
160- )
161- .build ();
162- }
163-
164143 private GraphQLFieldDefinition getQueryFieldByIdDefinition (EntityType <?> entityType ) {
165144 return GraphQLFieldDefinition .newFieldDefinition ()
166145 .name (entityType .getName ())
@@ -179,13 +158,6 @@ private GraphQLFieldDefinition getQueryFieldByIdDefinition(EntityType<?> entityT
179158
180159 private GraphQLFieldDefinition getQueryFieldSelectDefinition (EntityType <?> entityType ) {
181160
182- GraphQLArgument distinctArgument = GraphQLArgument .newArgument ()
183- .name (SELECT_DISTINCT_PARAM_NAME )
184- .description ("JPA Query DISTINCT specification" )
185- .type (Scalars .GraphQLBoolean )
186- .defaultValue (true )
187- .build ();
188-
189161 GraphQLObjectType pageType = GraphQLObjectType .newObject ()
190162 .name (namingStrategy .pluralize (entityType .getName ()))
191163 .description ("Query response wrapper object for " + entityType .getName () + ". When page is requested, this object will be returned with query metadata." )
@@ -205,7 +177,6 @@ private GraphQLFieldDefinition getQueryFieldSelectDefinition(EntityType<?> entit
205177 .name (GraphQLJpaSchemaBuilder .QUERY_SELECT_PARAM_NAME )
206178 .description ("The queried records container" )
207179 .type (new GraphQLList (getObjectType (entityType )))
208- //.argument(distinctArgument)
209180 .build ()
210181 )
211182 .build ();
@@ -286,12 +257,13 @@ private GraphQLInputObjectField getWhereInputField(Attribute<?,?> attribute) {
286257 private Map <String , GraphQLInputType > whereAttributesMap = new HashMap <>();
287258
288259 private GraphQLInputType getWhereAttributeType (Attribute <?,?> attribute ) {
289- String type = namingStrategy .singularize (attribute .getName ())+((EntityType <?>)attribute .getDeclaringType ()).getName ()+"Criteria" ;
260+ //String type = namingStrategy.singularize(attribute.getName())+((EntityType<?>)attribute.getDeclaringType()).getName()+"Criteria";
261+ String type = namingStrategy .singularize (attribute .getName ())+attribute .getDeclaringType ().getJavaType ().getSimpleName ()+"Criteria" ;
290262
291- if (whereAttributesMap .containsKey (type ))
263+ if (whereAttributesMap .containsKey (type ))
292264 return whereAttributesMap .get (type );
293265
294- GraphQLInputObjectType .Builder builder = GraphQLInputObjectType .newInputObject ()
266+ GraphQLInputObjectType .Builder builder = GraphQLInputObjectType .newInputObject ()
295267 .name (type )
296268 .description ("Criteria expression specification of " +namingStrategy .singularize (attribute .getName ())+" attribute in entity " + attribute .getDeclaringType ().getJavaType ())
297269 .field (GraphQLInputObjectField .newInputObjectField ()
@@ -422,6 +394,28 @@ private GraphQLArgument getArgument(Attribute<?,?> attribute) {
422394
423395 throw new IllegalArgumentException ("Attribute " + attribute + " cannot be mapped as an Input Argument" );
424396 }
397+
398+ private GraphQLObjectType getEmbeddableType (EmbeddableType <?> embeddableType ) {
399+ if (embeddableCache .containsKey (embeddableType ))
400+ return embeddableCache .get (embeddableType );
401+
402+ String embeddableTypeName = namingStrategy .singularize (embeddableType .getJavaType ().getSimpleName ())+"EmbeddableType" ;
403+
404+ GraphQLObjectType objectType = GraphQLObjectType .newObject ()
405+ .name (embeddableTypeName )
406+ .description (getSchemaDescription ( embeddableType .getJavaType ()))
407+ .fields (embeddableType .getAttributes ().stream ()
408+ .filter (this ::isNotIgnored )
409+ .map (this ::getObjectField )
410+ .collect (Collectors .toList ())
411+ )
412+ .build ();
413+
414+ embeddableCache .putIfAbsent (embeddableType , objectType );
415+
416+ return objectType ;
417+ }
418+
425419
426420 private GraphQLObjectType getObjectType (EntityType <?> entityType ) {
427421 if (entityCache .containsKey (entityType ))
@@ -465,6 +459,7 @@ private GraphQLFieldDefinition getObjectField(Attribute attribute) {
465459 // Get the fields that can be queried on (i.e. Simple Types, no Sub-Objects)
466460 if (attribute instanceof SingularAttribute
467461 && attribute .getPersistentAttributeType () != Attribute .PersistentAttributeType .BASIC
462+ && attribute .getPersistentAttributeType () != Attribute .PersistentAttributeType .EMBEDDED
468463 ) {
469464
470465 EntityType foreignType = (EntityType ) ((SingularAttribute ) attribute ).getType ();
@@ -507,6 +502,10 @@ private GraphQLType getAttributeType(Attribute<?,?> attribute) {
507502 if (isBasic (attribute )) {
508503 return getGraphQLTypeFromJavaType (attribute .getJavaType ());
509504 }
505+ else if (isEmbeddable (attribute )) {
506+ EmbeddableType embeddableType = (EmbeddableType ) ((SingularAttribute ) attribute ).getType ();
507+ return getEmbeddableType (embeddableType );
508+ }
510509 else if (isToMany (attribute )) {
511510 EntityType foreignType = (EntityType ) ((PluralAttribute ) attribute ).getElementType ();
512511 return new GraphQLList (new GraphQLTypeReference (foreignType .getName ()));
@@ -530,6 +529,10 @@ else if (isElementCollection(attribute)) {
530529 "Attribute could not be mapped to GraphQL: field '" + declaringMember + "' of entity class '" + declaringType +"'" );
531530 }
532531
532+ protected final boolean isEmbeddable (Attribute <?,?> attribute ) {
533+ return attribute .getPersistentAttributeType () == Attribute .PersistentAttributeType .EMBEDDED ;
534+ }
535+
533536 protected final boolean isBasic (Attribute <?,?> attribute ) {
534537 return attribute .getPersistentAttributeType () == Attribute .PersistentAttributeType .BASIC ;
535538 }
@@ -571,6 +574,10 @@ private String getSchemaDescription(AnnotatedElement annotatedElement) {
571574 return null ;
572575 }
573576
577+ private boolean isNotIgnored (EmbeddableType <?> attribute ) {
578+ return isNotIgnored (attribute .getJavaType ());
579+ }
580+
574581 private boolean isNotIgnored (Attribute <?,?> attribute ) {
575582 return isNotIgnored (attribute .getJavaMember ()) && isNotIgnored (attribute .getJavaType ());
576583 }
0 commit comments