Skip to content

fix(GH-38): Support JPA @ElementCollection annotation with @Embeddable generic type #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2020
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 @@ -39,7 +39,7 @@

@Data
@Entity
@EqualsAndHashCode(exclude= {"author", "tags"})
@EqualsAndHashCode(exclude= {"author", "tags", "publishers"})
public class Book {
@Id
Long id;
Expand All @@ -64,6 +64,9 @@ public class Book {
Genre genre;

Date publicationDate;

@ElementCollection(fetch = FetchType.LAZY)
Set<Publisher> publishers;

@Transient
@GraphQLIgnore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.introproventures.graphql.jpa.query.schema.model.book;

import javax.persistence.Embeddable;

import lombok.Data;

@Data
@Embeddable
public class Publisher {
private String name;

private String country;
}
4 changes: 4 additions & 0 deletions graphql-jpa-query-example-relay/src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ insert into author_phone_numbers(phone_number, author_id) values
('1-123-5678', 1),
('4-123-1234', 4),
('4-123-5678', 4);

insert into book_publishers(book_id, name, country) values
(3, 'Independent', 'UK'), (3, 'Amazon', 'US'),
(2, 'Willey', 'US'), (2, 'Simon', 'EU');
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ protected Predicate getObjectFieldPredicate(DataFetchingEnvironment environment,
if(isEntityType(environment)) {
Attribute<?,?> attribute = getAttribute(environment, argument.getName());

// TODO add support for embedded element collection, i.e. attribute.isCollection()
if(attribute.isAssociation()) {
GraphQLFieldDefinition fieldDefinition = getFieldDefinition(environment.getGraphQLSchema(),
this.getObjectType(environment),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.introproventures.graphql.jpa.query.schema.impl.EntityIntrospector.EntityIntrospectionResult.AttributePropertyDescriptor;
import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria;
import com.introproventures.graphql.jpa.query.schema.relay.GraphQLJpaRelayDataFetcher;

import graphql.Assert;
import graphql.Directives;
import graphql.Scalars;
Expand Down Expand Up @@ -418,6 +419,14 @@ private GraphQLArgument computeWhereArgument(ManagedType<?> managedType) {
.map(this::getInputObjectField)
.collect(Collectors.toList())
)
// TODO support embedded element collections
// .fields(managedType.getAttributes().stream()
// .filter(Attribute::isCollection)
// .filter(this::isNotIgnored)
// .filter(this::isNotIgnoredFilter)
// .map(this::getInputObjectField)
// .collect(Collectors.toList())
// )
.build();

return GraphQLArgument.newArgument()
Expand Down Expand Up @@ -1047,6 +1056,13 @@ else if (isElementCollection(attribute)) {

return input ? graphQLType : new GraphQLList(graphQLType);
}
else if (foreignType.getPersistenceType() == Type.PersistenceType.EMBEDDABLE) {
EmbeddableType embeddableType = EmbeddableType.class.cast(foreignType);
GraphQLType graphQLType = getEmbeddableType(embeddableType,
input);

return input ? graphQLType : new GraphQLList(graphQLType);
}
}

final String declaringType = attribute.getDeclaringType().getJavaType().getName(); // fully qualified name of the entity class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.introproventures.graphql.jpa.query.AbstractSpringBootTestSupport;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;

import graphql.ErrorType;
import graphql.ExecutionResult;
import graphql.GraphQLError;
Expand Down Expand Up @@ -662,6 +663,30 @@ public void queryForEntityWithEmbeddedFieldWithWhere() {
// then
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryForEntityWithEmbeddableElementCollection() {
//given
String query = "{ Books(where: { author: {name: {LIKE: \"Leo\"}}}) { select { id title publishers { name country } } } }";

String expected = "{Books={select=["
+ "{id=2, title=War and Peace, publishers=["
+ "{name=Willey, country=US}, "
+ "{name=Simon, country=EU}"
+ "]}, "
+ "{id=3, title=Anna Karenina, publishers=["
+ "{name=Independent, country=UK}, "
+ "{name=Amazon, country=US}"
+ "]}"
+ "]}}";

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

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


@Test
public void queryWithNumericBetweenPredicate() {
Expand Down
4 changes: 4 additions & 0 deletions graphql-jpa-query-schema/src/test/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ insert into author_phone_numbers(phone_number, author_id) values
('1-123-5678', 1),
('4-123-1234', 4),
('4-123-5678', 4);

insert into book_publishers(book_id, name, country) values
(3, 'Independent', 'UK'), (3, 'Amazon', 'US'),
(2, 'Willey', 'US'), (2, 'Simon', 'EU');

-- Car
insert into Car (id, brand, identification) values
Expand Down