Skip to content

Commit ce4f85a

Browse files
authored
fix: use embeddableType javaType to cache corresponding GraphQL type (#98)
* fix: use embeddableType javaType to cache corresponding GraphQL type * fix: use managed type Java type class to cache generated GraphQL types
1 parent b296d8a commit ce4f85a

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
102102
private Map<Class<?>, GraphQLOutputType> classCache = new HashMap<>();
103103
private Map<EntityType<?>, GraphQLObjectType> entityCache = new HashMap<>();
104104
private Map<ManagedType<?>, GraphQLInputObjectType> inputObjectCache = new HashMap<>();
105-
private Map<EmbeddableType<?>, GraphQLObjectType> embeddableOutputCache = new HashMap<>();
106-
private Map<EmbeddableType<?>, GraphQLInputObjectType> embeddableInputCache = new HashMap<>();
105+
private Map<Class<?>, GraphQLObjectType> embeddableOutputCache = new HashMap<>();
106+
private Map<Class<?>, GraphQLInputObjectType> embeddableInputCache = new HashMap<>();
107107

108108
private static final Logger log = LoggerFactory.getLogger(GraphQLJpaSchemaBuilder.class);
109109

@@ -206,7 +206,7 @@ private GraphQLFieldDefinition getQueryFieldSelectDefinition(EntityType<?> entit
206206
.build();
207207
}
208208

209-
private Map<ManagedType<?>, GraphQLArgument> whereArgumentsMap = new HashMap<>();
209+
private Map<Class<?>, GraphQLArgument> whereArgumentsMap = new HashMap<>();
210210

211211
private GraphQLArgument getWhereArgument(ManagedType<?> managedType) {
212212
String typeName="";
@@ -218,7 +218,7 @@ private GraphQLArgument getWhereArgument(ManagedType<?> managedType) {
218218

219219
String type = namingStrategy.pluralize(typeName)+"CriteriaExpression";
220220

221-
GraphQLArgument whereArgument = whereArgumentsMap.get(managedType);
221+
GraphQLArgument whereArgument = whereArgumentsMap.get(managedType.getJavaType());
222222

223223
if(whereArgument != null)
224224
return whereArgument;
@@ -259,12 +259,12 @@ private GraphQLArgument getWhereArgument(ManagedType<?> managedType) {
259259
.build();
260260

261261
whereArgument = GraphQLArgument.newArgument()
262-
.name(QUERY_WHERE_PARAM_NAME)
263-
.description("Where logical specification")
264-
.type(whereInputObject)
265-
.build();
262+
.name(QUERY_WHERE_PARAM_NAME)
263+
.description("Where logical specification")
264+
.type(whereInputObject)
265+
.build();
266266

267-
whereArgumentsMap.put(managedType, whereArgument);
267+
whereArgumentsMap.put(managedType.getJavaType(), whereArgument);
268268

269269
return whereArgument;
270270

@@ -475,11 +475,11 @@ private GraphQLArgument getArgument(Attribute<?,?> attribute) {
475475
}
476476

477477
private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean input) {
478-
if (input && embeddableInputCache.containsKey(embeddableType))
479-
return embeddableInputCache.get(embeddableType);
478+
if (input && embeddableInputCache.containsKey(embeddableType.getJavaType()))
479+
return embeddableInputCache.get(embeddableType.getJavaType());
480480

481-
if (!input && embeddableOutputCache.containsKey(embeddableType))
482-
return embeddableOutputCache.get(embeddableType);
481+
if (!input && embeddableOutputCache.containsKey(embeddableType.getJavaType()))
482+
return embeddableOutputCache.get(embeddableType.getJavaType());
483483
String embeddableTypeName = namingStrategy.singularize(embeddableType.getJavaType().getSimpleName())+ (input ? "Input" : "") +"EmbeddableType";
484484
GraphQLType graphQLType=null;
485485
if (input) {
@@ -504,9 +504,9 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
504504
.build();
505505
}
506506
if (input) {
507-
embeddableInputCache.putIfAbsent(embeddableType, (GraphQLInputObjectType) graphQLType);
507+
embeddableInputCache.putIfAbsent(embeddableType.getJavaType(), (GraphQLInputObjectType) graphQLType);
508508
} else{
509-
embeddableOutputCache.putIfAbsent(embeddableType, (GraphQLObjectType) graphQLType);
509+
embeddableOutputCache.putIfAbsent(embeddableType.getJavaType(), (GraphQLObjectType) graphQLType);
510510
}
511511

512512
return graphQLType;

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/model/embedded/Car.java

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
package com.introproventures.graphql.jpa.query.schema.model.embedded;
33

4+
import javax.persistence.Embedded;
45
import javax.persistence.Entity;
56

67
import lombok.Data;
@@ -12,5 +13,9 @@
1213
public class Car extends Vehicle {
1314

1415
String brand;
16+
17+
// Shared between Car and Boat. Fixes https://github.com/introproventures/graphql-jpa-query/issues/91
18+
@Embedded
19+
Engine engine;
1520

1621
}

graphql-jpa-query-schema/src/test/resources/data.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ insert into author_phone_numbers(phone_number, author_id) values
122122
('4-123-5678', 4);
123123

124124
-- Car
125-
insert into Car (id, brand) values
126-
(1, 'Ford'),
127-
(2, 'Cadillac'),
128-
(3, 'Toyota');
125+
insert into Car (id, brand, identification) values
126+
(1, 'Ford', 'xxxxx'),
127+
(2, 'Cadillac', 'yyyyy'),
128+
(3, 'Toyota', 'zzzzz');
129129

130130

131131
-- Boat

0 commit comments

Comments
 (0)