Skip to content

fix(GH-198): adedd support for fetching optional element collections elements #200

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 3 commits into from
Oct 5, 2019
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
13 changes: 13 additions & 0 deletions graphql-jpa-query-example-merge/src/main/resources/books.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ insert into book (id, title, author_id, genre) values (5, 'The Cherry Orchard',
insert into book (id, title, author_id, genre) values (6, 'The Seagull', 4, 'PLAY');
insert into book (id, title, author_id, genre) values (7, 'Three Sisters', 4, 'PLAY');
insert into author (id, name, genre) values (8, 'Igor Dianov', 'JAVA');

insert into book_tags (book_id, tags) values (2, 'war'), (2, 'piece');
insert into book_tags (book_id, tags) values (3, 'anna'), (3, 'karenina');
insert into book_tags (book_id, tags) values (5, 'cherry'), (5, 'orchard');
insert into book_tags (book_id, tags) values (6, 'seagull');
insert into book_tags (book_id, tags) values (7, 'three'), (7, 'sisters');

insert into author_phone_numbers(phone_number, author_id) values
('1-123-1234', 1),
('1-123-5678', 1),
('4-123-1234', 4),
('4-123-5678', 4);

Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,29 @@
package com.introproventures.graphql.jpa.query.schema.model.book;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.*;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnore;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreFilter;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreOrder;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@Entity
@EqualsAndHashCode(exclude="author")
@EqualsAndHashCode(exclude= {"author", "tags"})
public class Book {
@Id
Long id;
Expand All @@ -38,6 +49,10 @@ public class Book {
@GraphQLIgnoreOrder
@GraphQLIgnoreFilter
String description;

@ElementCollection(fetch = FetchType.LAZY)
@GraphQLDescription("A set of user-defined tags")
private Set<String> tags = new LinkedHashSet<>();

@ManyToOne(fetch=FetchType.LAZY, optional = false)
Author author;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,20 @@ protected final List<Predicate> getFieldPredicates(Field field, CriteriaQuery<?>
// Let's do fugly conversion
// the many end is a collection, and it is always optional by default (empty collection)
isOptional = optionalArgument.map(it -> getArgumentValue(environment, it, Boolean.class))
.orElse(toManyDefaultOptional);
.orElse(toManyDefaultOptional);

// Let's apply join to retrieve associated collection
fetch = reuseFetch(from, selectedField.getName(), isOptional);

// Let's fetch element collections to avoid filtering their values used where search criteria
GraphQLObjectType objectType = getObjectType(environment);
EntityType<?> entityType = getEntityType(objectType);

PluralAttribute<?, ?, ?> attribute = (PluralAttribute<?, ?, ?>) entityType.getAttribute(selectedField.getName());

// Let's join fetch element collections to avoid filtering their values used where search criteria
if(PersistentAttributeType.ELEMENT_COLLECTION == attribute.getPersistentAttributeType()) {
from.fetch(selectedField.getName());
}
from.fetch(selectedField.getName(), JoinType.LEFT);
} else {
// Let's apply fetch join to retrieve associated plural attributes
fetch = reuseFetch(from, selectedField.getName(), isOptional);
}
}
// Let's build join fetch graph to avoid Hibernate error:
// "query specified join fetching, but the owner of the fetched association was not present in the select list"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1611,4 +1611,41 @@ public void queryWithEnumParameterShouldExecuteWithNoError() {
"War and Peace")
);
}

// https://github.com/introproventures/graphql-jpa-query/issues/198
@Test
public void queryOptionalElementCollections() {
//given
String query = "{ Author(id: 8) { id name phoneNumbers books { id title tags } } }";

String expected = "{Author={id=8, name=Igor Dianov, phoneNumbers=[], books=[]}}";

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

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

@Test
public void queryElementCollectionsWithWhereCriteriaExpression() {
//given:
String query = "query {" +
" Books(where: {tags: {EQ: \"war\"}}) {" +
" select {" +
" id" +
" title" +
" tags" +
" }" +
" }" +
"}";

String expected = "{Books={select=[{id=2, title=War and Peace, tags=[piece, war]}]}}";

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

//then:
assertThat(result.toString()).isEqualTo(expected);
}
}
6 changes: 6 additions & 0 deletions graphql-jpa-query-schema/src/test/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ insert into book (id, title, author_id, genre, publication_date, description)
values (7, 'Three Sisters', 4, 'PLAY', '1900-01-01', 'The play is sometimes included on the short list of Chekhov''s outstanding plays, along with The Cherry Orchard, The Seagull and Uncle Vanya.[1]');
insert into author (id, name, genre) values (8, 'Igor Dianov', 'JAVA');

insert into book_tags (book_id, tags) values (2, 'war'), (2, 'piece');
insert into book_tags (book_id, tags) values (3, 'anna'), (3, 'karenina');
insert into book_tags (book_id, tags) values (5, 'cherry'), (5, 'orchard');
insert into book_tags (book_id, tags) values (6, 'seagull');
insert into book_tags (book_id, tags) values (7, 'three'), (7, 'sisters');

insert into author_phone_numbers(phone_number, author_id) values
('1-123-1234', 1),
('1-123-5678', 1),
Expand Down