Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EmbeddableType;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;
Expand Down Expand Up @@ -194,12 +195,19 @@ private GraphQLFieldDefinition getQueryFieldSelectDefinition(EntityType<?> entit
.build();
}

private Map<EntityType<?>, GraphQLArgument> whereArgumentsMap = new HashMap<>();

private GraphQLArgument getWhereArgument(EntityType<?> entityType) {
String type = namingStrategy.pluralize(entityType.getName())+"CriteriaExpression";
private Map<ManagedType<?>, GraphQLArgument> whereArgumentsMap = new HashMap<>();

private GraphQLArgument getWhereArgument(ManagedType<?> managedType) {
String typeName="";
if (managedType instanceof EmbeddableType){
typeName = managedType.getJavaType().getSimpleName()+"EmbeddableType";
} else if (managedType instanceof EntityType) {
typeName = ((EntityType)managedType).getName();
}

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

GraphQLArgument whereArgument = whereArgumentsMap.get(entityType);
GraphQLArgument whereArgument = whereArgumentsMap.get(managedType);

if(whereArgument != null)
return whereArgument;
Expand All @@ -219,7 +227,7 @@ private GraphQLArgument getWhereArgument(EntityType<?> entityType) {
.type(new GraphQLTypeReference(type))
.build()
)
.fields(entityType.getAttributes().stream()
.fields(managedType.getAttributes().stream()
.filter(this::isValidInput)
.filter(this::isNotIgnored)
.map(this::getWhereInputField)
Expand All @@ -233,7 +241,7 @@ private GraphQLArgument getWhereArgument(EntityType<?> entityType) {
.type(whereInputObject)
.build();

whereArgumentsMap.put(entityType, whereArgument);
whereArgumentsMap.put(managedType, whereArgument);

return whereArgument;

Expand Down Expand Up @@ -457,22 +465,17 @@ private GraphQLFieldDefinition getObjectField(Attribute attribute) {
}

// Get the fields that can be queried on (i.e. Simple Types, no Sub-Objects)
if (attribute instanceof SingularAttribute
&& attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.BASIC
&& attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.EMBEDDED
) {

EntityType foreignType = (EntityType) ((SingularAttribute) attribute).getType();
Stream<Attribute> attributes = findBasicAttributes(foreignType.getAttributes());
if (attribute instanceof SingularAttribute
&& attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.BASIC) {
ManagedType foreignType = (ManagedType) ((SingularAttribute) attribute).getType();

// TODO fix page count query
arguments.add(getWhereArgument(foreignType));

} // Get Sub-Objects fields queries via DataFetcher
} // Get Sub-Objects fields queries via DataFetcher
else if (attribute instanceof PluralAttribute
&& (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ONE_TO_MANY
|| attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.MANY_TO_MANY)
) {
|| attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.MANY_TO_MANY)) {
EntityType declaringType = (EntityType) ((PluralAttribute) attribute).getDeclaringType();
EntityType elementType = (EntityType) ((PluralAttribute) attribute).getElementType();

Expand All @@ -498,9 +501,9 @@ private Stream<Attribute<?,?>> findBasicAttributes(Collection<Attribute<?,?>> at

@SuppressWarnings( "rawtypes" )
private GraphQLType getAttributeType(Attribute<?,?> attribute) {

if (isBasic(attribute)) {
return getGraphQLTypeFromJavaType(attribute.getJavaType());
return getGraphQLTypeFromJavaType(attribute.getJavaType());
}
else if (isEmbeddable(attribute)) {
EmbeddableType embeddableType = (EmbeddableType) ((SingularAttribute) attribute).getType();
Expand All @@ -518,7 +521,7 @@ else if (isElementCollection(attribute)) {
Type foreignType = ((PluralAttribute) attribute).getElementType();

if(foreignType.getPersistenceType() == Type.PersistenceType.BASIC) {
return new GraphQLList(getGraphQLTypeFromJavaType(foreignType.getJavaType()));
return new GraphQLList(getGraphQLTypeFromJavaType(foreignType.getJavaType()));
}
}

Expand All @@ -542,12 +545,12 @@ protected final boolean isElementCollection(Attribute<?,?> attribute) {
}

protected final boolean isToMany(Attribute<?,?> attribute) {
return attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ONE_TO_MANY
return attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ONE_TO_MANY
|| attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.MANY_TO_MANY;
}

protected final boolean isToOne(Attribute<?,?> attribute) {
return attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.MANY_TO_ONE
return attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.MANY_TO_ONE
|| attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ONE_TO_ONE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,21 +375,20 @@ public void queryForEntityWithEmeddableType() {
// then
assertThat(result.toString()).isEqualTo(expected);
}

// // TODO
// @Test
// public void queryForEntityWithEmeddableTypeAndWhere() {
// //given
// String query = "{ Boats { id engine(where: { identification: { EQ: \"12345\"}}) { identification } } }";
//
// String expected = "{Boats={select[id=1, engine={identification=12345}]}}";
//
// //when
// Object result = executor.execute(query).getData();
//
// // then
// assertThat(result.toString()).isEqualTo(expected);
// }

@Test
public void queryForEntityWithEmeddableTypeAndWhere() {
//given
String query = "{ Boats { select { id engine(where: { identification: { EQ: \"12345\"}}) { identification } } } }";

String expected = "{Boats={select=[{id=1, engine={identification=12345}}]}}";

//when
Object result = executor.execute(query).getData();

// then
assertThat(result.toString()).isEqualTo(expected);
}



Expand Down