Search in sources :

Example 21 with Sorting

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

the class DataStoreLoadTest method testSortingAscending.

@Test
public void testSortingAscending() throws Exception {
    DataStoreTransaction testTransaction = searchStore.beginReadTransaction();
    Map<String, Sorting.SortOrder> sortRules = new HashMap<>();
    sortRules.put("name", Sorting.SortOrder.asc);
    sortRules.put("modifiedDate", Sorting.SortOrder.desc);
    Sorting sorting = new SortingImpl(sortRules, Item.class, dictionary);
    FilterExpression filter = filterParser.parseFilterExpression("name==cymbal*", ClassType.of(Item.class), false);
    Iterable<Object> loaded = testTransaction.loadObjects(EntityProjection.builder().type(Item.class).filterExpression(filter).sorting(sorting).build(), mockScope);
    assertListContains(loaded, Lists.newArrayList(4L, 5L, 2L));
    verify(wrappedTransaction, never()).loadObjects(any(), any());
}
Also used : Item(com.yahoo.elide.datastores.search.models.Item) HashMap(java.util.HashMap) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Sorting(com.yahoo.elide.core.request.Sorting) Test(org.junit.jupiter.api.Test)

Example 22 with Sorting

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

the class GraphQLEntityProjectionMaker method addSorting.

/**
 * Creates a {@link Sorting} object from sorting GraphQL argument value and attaches it to the entity sorted
 * according to the newly created {@link Sorting} object.
 *
 * @param argument An argument that contains the value of sorting spec
 * @param projectionBuilder projection that is being built
 */
private void addSorting(Argument argument, EntityProjectionBuilder projectionBuilder) {
    String sortRule = (String) variableResolver.resolveValue(argument.getValue());
    try {
        Sorting sorting = SortingImpl.parseSortRule(sortRule, projectionBuilder.getType(), projectionBuilder.getAttributes(), entityDictionary);
        projectionBuilder.sorting(sorting);
    } catch (InvalidValueException e) {
        throw new BadRequestException("Invalid sorting clause " + sortRule + " for type " + entityDictionary.getJsonAliasFor(projectionBuilder.getType()));
    }
}
Also used : InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Sorting(com.yahoo.elide.core.request.Sorting)

Aggregations

Sorting (com.yahoo.elide.core.request.Sorting)22 Test (org.junit.jupiter.api.Test)13 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)12 EntityProjection (com.yahoo.elide.core.request.EntityProjection)11 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)10 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)10 HashMap (java.util.HashMap)10 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)9 Path (com.yahoo.elide.core.Path)8 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)8 Collection (java.util.Collection)7 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)6 ClassType (com.yahoo.elide.core.type.ClassType)6 Map (java.util.Map)6 RequestScope (com.yahoo.elide.core.RequestScope)5 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)5 PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)5 Pagination (com.yahoo.elide.core.request.Pagination)5 Book (example.Book)5 Editor (example.Editor)5