Search in sources :

Example 21 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class SubCollectionFetchQueryBuilderTest method testSubCollectionFetchWithIncludedToManyRelation.

@Test
public void testSubCollectionFetchWithIncludedToManyRelation() {
    Author author = new Author();
    author.setId(1L);
    Book book = new Book();
    book.setId(2);
    Map<String, Sorting.SortOrder> sorting = new HashMap<>();
    sorting.put(TITLE, Sorting.SortOrder.asc);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).relationship(Relationship.builder().name(AUTHORS).projection(EntityProjection.builder().type(Author.class).build()).build()).sorting(new SortingImpl(sorting, Book.class, dictionary)).build();
    Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
    SubCollectionFetchQueryBuilder builder = new SubCollectionFetchQueryBuilder(relationship, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String expected = "SELECT example_Book FROM example.Author example_Author__fetch " + "JOIN example_Author__fetch.books example_Book " + "WHERE example_Author__fetch=:example_Author__fetch order by example_Book.title asc";
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    assertEquals(expected, actual);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) HashMap(java.util.HashMap) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Author(example.Author) Test(org.junit.jupiter.api.Test)

Example 22 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class SubCollectionFetchQueryBuilderTest method testFetchJoinExcludesParent.

@Test
public void testFetchJoinExcludesParent() {
    Publisher publisher = new Publisher();
    publisher.setId(1);
    Book book = new Book();
    book.setId(2);
    Pagination pagination = new PaginationImpl(Book.class, 0, 10, 10, 10, false, false);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).pagination(pagination).build();
    Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Publisher.class), publisher, relationshipProjection);
    SubCollectionFetchQueryBuilder builder = new SubCollectionFetchQueryBuilder(relationship, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String expected = "SELECT example_Book FROM example.Publisher example_Publisher__fetch " + "JOIN example_Publisher__fetch.books example_Book " + "WHERE example_Publisher__fetch=:example_Publisher__fetch";
    String actual = query.getQueryText();
    actual = actual.replaceFirst(":publisher_name_\\w+", ":publisher_name_XXX");
    assertEquals(expected, actual);
}
Also used : Pagination(com.yahoo.elide.core.request.Pagination) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Publisher(example.Publisher) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) Test(org.junit.jupiter.api.Test)

Example 23 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class SubCollectionFetchQueryBuilderTest method testSubCollectionFetchWithSorting.

@Test
public void testSubCollectionFetchWithSorting() {
    Author author = new Author();
    author.setId(1L);
    Book book = new Book();
    book.setId(2);
    Map<String, Sorting.SortOrder> sorting = new HashMap<>();
    sorting.put(TITLE, Sorting.SortOrder.asc);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).sorting(new SortingImpl(sorting, Book.class, dictionary)).build();
    Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
    SubCollectionFetchQueryBuilder builder = new SubCollectionFetchQueryBuilder(relationship, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String expected = "SELECT example_Book FROM example.Author example_Author__fetch " + "JOIN example_Author__fetch.books example_Book " + "WHERE example_Author__fetch=:example_Author__fetch order by example_Book.title asc";
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    assertEquals(expected, actual);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) HashMap(java.util.HashMap) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Author(example.Author) Test(org.junit.jupiter.api.Test)

Example 24 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class SubCollectionPageTotalsQueryBuilderTest method testSubCollectionPageTotalsWithPagination.

@Test
public void testSubCollectionPageTotalsWithPagination() {
    Author author = new Author();
    author.setId(1L);
    Book book = new Book();
    book.setId(2);
    PaginationImpl pagination = mock(PaginationImpl.class);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).pagination(pagination).build();
    Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
    TestQueryWrapper query = (TestQueryWrapper) new SubCollectionPageTotalsQueryBuilder(relationship, dictionary, new TestSessionWrapper()).build();
    String expected = "SELECT COUNT(DISTINCT example_Author_books) " + "FROM example.Author AS example_Author " + "JOIN example_Author.books example_Author_books " + "WHERE example_Author.id IN (:id_XXX)";
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    actual = actual.replaceFirst(":id_\\w+", ":id_XXX");
    assertEquals(expected, actual);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Author(example.Author) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) SubCollectionPageTotalsQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionPageTotalsQueryBuilder) Test(org.junit.jupiter.api.Test)

Example 25 with EntityProjection

use of com.yahoo.elide.core.request.EntityProjection in project elide by yahoo.

the class PersistentResourceFetcher method fetchObject.

/**
 * Fetches a root-level entity.
 * @param requestScope Request scope
 * @param projection constructed entityProjection for a class
 * @param ids List of ids (can be NULL)
 * @return {@link PersistentResource} object(s)
 */
public static ConnectionContainer fetchObject(RequestScope requestScope, EntityProjection projection, Optional<List<String>> ids) {
    EntityDictionary dictionary = requestScope.getDictionary();
    String typeName = dictionary.getJsonAliasFor(projection.getType());
    /* fetching a collection */
    Observable<PersistentResource> records = ids.map((idList) -> {
        /* handle empty list of ids */
        if (idList.isEmpty()) {
            throw new BadRequestException("Empty list passed to ids");
        }
        return PersistentResource.loadRecords(projection, idList, requestScope);
    }).orElseGet(() -> PersistentResource.loadRecords(projection, new ArrayList<>(), requestScope));
    return new ConnectionContainer(records.toList(LinkedHashSet::new).blockingGet(), Optional.ofNullable(projection.getPagination()), typeName);
}
Also used : MapEntryContainer(com.yahoo.elide.graphql.containers.MapEntryContainer) OperationDefinition(graphql.language.OperationDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) ClassType(com.yahoo.elide.core.type.ClassType) ArrayList(java.util.ArrayList) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) DataFetcher(graphql.schema.DataFetcher) Relationship(com.yahoo.elide.core.request.Relationship) Observable(io.reactivex.Observable) LinkedHashSet(java.util.LinkedHashSet) RequestScope(com.yahoo.elide.core.RequestScope) ARGUMENT_OPERATION(com.yahoo.elide.graphql.ModelBuilder.ARGUMENT_OPERATION) FETCH(com.yahoo.elide.graphql.RelationshipOp.FETCH) InvalidObjectIdentifierException(com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException) Set(java.util.Set) EntityProjection(com.yahoo.elide.core.request.EntityProjection) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) Type(com.yahoo.elide.core.type.Type) Optional(java.util.Optional) GraphQLContainer(com.yahoo.elide.graphql.containers.GraphQLContainer) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) ArrayList(java.util.ArrayList) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Aggregations

EntityProjection (com.yahoo.elide.core.request.EntityProjection)108 Test (org.junit.jupiter.api.Test)90 Book (example.Book)41 RequestScope (com.yahoo.elide.core.RequestScope)27 Author (example.Author)27 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)22 Publisher (example.Publisher)22 Relationship (com.yahoo.elide.core.request.Relationship)19 Path (com.yahoo.elide.core.Path)18 TestRequestScope (com.yahoo.elide.core.TestRequestScope)18 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)18 HashMap (java.util.HashMap)18 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)16 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)15 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)15 Collection (java.util.Collection)15 LinkedHashSet (java.util.LinkedHashSet)14 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)13 Editor (example.Editor)13