30
30
31
31
import javax .persistence .EntityManager ;
32
32
import javax .persistence .metamodel .Attribute ;
33
+ import javax .persistence .metamodel .EmbeddableType ;
33
34
import javax .persistence .metamodel .EntityType ;
34
35
import javax .persistence .metamodel .PluralAttribute ;
35
36
import javax .persistence .metamodel .SingularAttribute ;
@@ -90,6 +91,7 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
90
91
91
92
private Map <Class <?>, GraphQLType > classCache = new HashMap <>();
92
93
private Map <EntityType <?>, GraphQLObjectType > entityCache = new HashMap <>();
94
+ private Map <EmbeddableType <?>, GraphQLObjectType > embeddableCache = new HashMap <>();
93
95
94
96
private static final Logger log = LoggerFactory .getLogger (GraphQLJpaSchemaBuilder .class );
95
97
@@ -127,14 +129,6 @@ private GraphQLObjectType getQueryType() {
127
129
.collect (Collectors .toList ())
128
130
);
129
131
130
- // queryType.fields(
131
- // entityManager.getMetamodel()
132
- // .getEntities().stream()
133
- // .filter(this::isNotIgnored)
134
- // .map(this::getQueryFieldDefinition)
135
- // .collect(Collectors.toList())
136
- // );
137
-
138
132
queryType .fields (
139
133
entityManager .getMetamodel ()
140
134
.getEntities ().stream ()
@@ -146,21 +140,6 @@ private GraphQLObjectType getQueryType() {
146
140
return queryType .build ();
147
141
}
148
142
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
-
164
143
private GraphQLFieldDefinition getQueryFieldByIdDefinition (EntityType <?> entityType ) {
165
144
return GraphQLFieldDefinition .newFieldDefinition ()
166
145
.name (entityType .getName ())
@@ -179,13 +158,6 @@ private GraphQLFieldDefinition getQueryFieldByIdDefinition(EntityType<?> entityT
179
158
180
159
private GraphQLFieldDefinition getQueryFieldSelectDefinition (EntityType <?> entityType ) {
181
160
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
-
189
161
GraphQLObjectType pageType = GraphQLObjectType .newObject ()
190
162
.name (namingStrategy .pluralize (entityType .getName ()))
191
163
.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
205
177
.name (GraphQLJpaSchemaBuilder .QUERY_SELECT_PARAM_NAME )
206
178
.description ("The queried records container" )
207
179
.type (new GraphQLList (getObjectType (entityType )))
208
- //.argument(distinctArgument)
209
180
.build ()
210
181
)
211
182
.build ();
@@ -286,12 +257,13 @@ private GraphQLInputObjectField getWhereInputField(Attribute<?,?> attribute) {
286
257
private Map <String , GraphQLInputType > whereAttributesMap = new HashMap <>();
287
258
288
259
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" ;
290
262
291
- if (whereAttributesMap .containsKey (type ))
263
+ if (whereAttributesMap .containsKey (type ))
292
264
return whereAttributesMap .get (type );
293
265
294
- GraphQLInputObjectType .Builder builder = GraphQLInputObjectType .newInputObject ()
266
+ GraphQLInputObjectType .Builder builder = GraphQLInputObjectType .newInputObject ()
295
267
.name (type )
296
268
.description ("Criteria expression specification of " +namingStrategy .singularize (attribute .getName ())+" attribute in entity " + attribute .getDeclaringType ().getJavaType ())
297
269
.field (GraphQLInputObjectField .newInputObjectField ()
@@ -422,6 +394,28 @@ private GraphQLArgument getArgument(Attribute<?,?> attribute) {
422
394
423
395
throw new IllegalArgumentException ("Attribute " + attribute + " cannot be mapped as an Input Argument" );
424
396
}
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
+
425
419
426
420
private GraphQLObjectType getObjectType (EntityType <?> entityType ) {
427
421
if (entityCache .containsKey (entityType ))
@@ -465,6 +459,7 @@ private GraphQLFieldDefinition getObjectField(Attribute attribute) {
465
459
// Get the fields that can be queried on (i.e. Simple Types, no Sub-Objects)
466
460
if (attribute instanceof SingularAttribute
467
461
&& attribute .getPersistentAttributeType () != Attribute .PersistentAttributeType .BASIC
462
+ && attribute .getPersistentAttributeType () != Attribute .PersistentAttributeType .EMBEDDED
468
463
) {
469
464
470
465
EntityType foreignType = (EntityType ) ((SingularAttribute ) attribute ).getType ();
@@ -507,6 +502,10 @@ private GraphQLType getAttributeType(Attribute<?,?> attribute) {
507
502
if (isBasic (attribute )) {
508
503
return getGraphQLTypeFromJavaType (attribute .getJavaType ());
509
504
}
505
+ else if (isEmbeddable (attribute )) {
506
+ EmbeddableType embeddableType = (EmbeddableType ) ((SingularAttribute ) attribute ).getType ();
507
+ return getEmbeddableType (embeddableType );
508
+ }
510
509
else if (isToMany (attribute )) {
511
510
EntityType foreignType = (EntityType ) ((PluralAttribute ) attribute ).getElementType ();
512
511
return new GraphQLList (new GraphQLTypeReference (foreignType .getName ()));
@@ -530,6 +529,10 @@ else if (isElementCollection(attribute)) {
530
529
"Attribute could not be mapped to GraphQL: field '" + declaringMember + "' of entity class '" + declaringType +"'" );
531
530
}
532
531
532
+ protected final boolean isEmbeddable (Attribute <?,?> attribute ) {
533
+ return attribute .getPersistentAttributeType () == Attribute .PersistentAttributeType .EMBEDDED ;
534
+ }
535
+
533
536
protected final boolean isBasic (Attribute <?,?> attribute ) {
534
537
return attribute .getPersistentAttributeType () == Attribute .PersistentAttributeType .BASIC ;
535
538
}
@@ -571,6 +574,10 @@ private String getSchemaDescription(AnnotatedElement annotatedElement) {
571
574
return null ;
572
575
}
573
576
577
+ private boolean isNotIgnored (EmbeddableType <?> attribute ) {
578
+ return isNotIgnored (attribute .getJavaType ());
579
+ }
580
+
574
581
private boolean isNotIgnored (Attribute <?,?> attribute ) {
575
582
return isNotIgnored (attribute .getJavaMember ()) && isNotIgnored (attribute .getJavaType ());
576
583
}
0 commit comments