Search in sources :

Example 21 with Relationship

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

the class SubCollectionPageTotalsQueryBuilderTest method testSubCollectionPageTotals.

@Test
public void testSubCollectionPageTotals() {
    Author author = new Author();
    author.setId(1L);
    Book book = new Book();
    book.setId(2);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).build();
    Relationship relationshipProjection = Relationship.builder().projection(entityProjection).name(BOOKS).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
    SubCollectionPageTotalsQueryBuilder builder = new SubCollectionPageTotalsQueryBuilder(relationship, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    actual = actual.replaceFirst(":id_\\w+", ":id_XXX");
    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)";
    assertEquals(expected, actual);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) 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 22 with Relationship

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

the class NodeContainer method processFetch.

@Override
public Object processFetch(Environment context) {
    EntityDictionary entityDictionary = context.requestScope.getDictionary();
    NonEntityDictionary nonEntityDictionary = context.nonEntityDictionary;
    Type parentClass = context.parentResource.getResourceType();
    String fieldName = context.field.getName();
    String idFieldName = entityDictionary.getIdFieldName(parentClass);
    if (entityDictionary.isAttribute(parentClass, fieldName)) {
        /* fetch attribute properties */
        Attribute requested = context.requestScope.getProjectionInfo().getAttributeMap().getOrDefault(context.field.getSourceLocation(), null);
        Object attribute = context.parentResource.getAttribute(requested);
        if (attribute != null && nonEntityDictionary.hasBinding(EntityDictionary.getType(attribute))) {
            return new NonEntityContainer(attribute);
        }
        if (attribute instanceof Map) {
            return ((Map<Object, Object>) attribute).entrySet().stream().map(MapEntryContainer::new).collect(Collectors.toList());
        }
        if (attribute instanceof Collection) {
            Type<?> innerType = entityDictionary.getParameterizedType(parentClass, fieldName);
            if (nonEntityDictionary.hasBinding(innerType)) {
                return ((Collection) attribute).stream().map(NonEntityContainer::new).collect(Collectors.toList());
            }
        }
        return attribute;
    }
    if (entityDictionary.isRelation(parentClass, fieldName)) {
        /* fetch relationship properties */
        // get the relationship from constructed projections
        Relationship relationship = context.requestScope.getProjectionInfo().getRelationshipMap().getOrDefault(context.field.getSourceLocation(), null);
        if (relationship == null) {
            throw new BadRequestException("Relationship doesn't have projection " + context.parentResource.getTypeName() + "." + fieldName);
        }
        return fetchRelationship(context, relationship);
    }
    if (Objects.equals(idFieldName, fieldName)) {
        return new DeferredId(context.parentResource);
    }
    throw new BadRequestException("Unrecognized object: " + fieldName + " for: " + parentClass.getName() + " in node");
}
Also used : Attribute(com.yahoo.elide.core.request.Attribute) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) Type(com.yahoo.elide.core.type.Type) Relationship(com.yahoo.elide.core.request.Relationship) DeferredId(com.yahoo.elide.graphql.DeferredId) Collection(java.util.Collection) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) NonEntityDictionary(com.yahoo.elide.graphql.NonEntityDictionary) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Map(java.util.Map)

Aggregations

Relationship (com.yahoo.elide.core.request.Relationship)22 Book (example.Book)15 Test (org.junit.jupiter.api.Test)15 EntityProjection (com.yahoo.elide.core.request.EntityProjection)14 RelationshipImpl (com.yahoo.elide.datastores.jpql.query.RelationshipImpl)12 Author (example.Author)12 SubCollectionFetchQueryBuilder (com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder)8 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)5 Publisher (example.Publisher)5 HashMap (java.util.HashMap)5 Path (com.yahoo.elide.core.Path)4 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)4 SubCollectionPageTotalsQueryBuilder (com.yahoo.elide.datastores.jpql.query.SubCollectionPageTotalsQueryBuilder)4 PersistentResource (com.yahoo.elide.core.PersistentResource)3 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)3 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)3 RequestScope (com.yahoo.elide.core.RequestScope)2 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)2 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)2 PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)2