Search in sources :

Example 16 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class SearchDataTransaction method loadObjects.

@Override
public <T> DataStoreIterable<T> loadObjects(EntityProjection projection, RequestScope requestScope) {
    if (projection.getFilterExpression() == null) {
        return super.loadObjects(projection, requestScope);
    }
    FilterSupport filterSupport = canSearch(projection.getType(), projection.getFilterExpression());
    boolean canSearch = (filterSupport != FilterSupport.NONE);
    if (mustSort(Optional.ofNullable(projection.getSorting()))) {
        canSearch = canSearch && canSort(projection.getSorting(), projection.getType());
    }
    if (canSearch) {
        Iterable<T> result = search(projection.getType(), projection.getFilterExpression(), Optional.ofNullable(projection.getSorting()), Optional.ofNullable(projection.getPagination()));
        if (filterSupport == FilterSupport.PARTIAL) {
            return new DataStoreIterableBuilder(result).allInMemory().build();
        } else {
            return new DataStoreIterableBuilder(result).build();
        }
    }
    return super.loadObjects(projection, requestScope);
}
Also used : DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder)

Example 17 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class InMemoryStoreTransaction method fetchData.

private DataStoreIterable<Object> fetchData(DataFetcher fetcher, EntityProjection projection, boolean filterInMemory, RequestScope scope) {
    Optional<FilterExpression> filterExpression = Optional.ofNullable(projection.getFilterExpression());
    Pair<Optional<FilterExpression>, Optional<FilterExpression>> expressionSplit = splitFilterExpression(scope, projection, filterInMemory);
    Optional<FilterExpression> dataStoreFilter = expressionSplit.getLeft();
    Optional<FilterExpression> inMemoryFilter = expressionSplit.getRight();
    Optional<Sorting> dataStoreSorting = getDataStoreSorting(scope, projection, filterInMemory);
    boolean sortingInMemory = dataStoreSorting.isEmpty() && projection.getSorting() != null;
    Optional<Pagination> dataStorePagination = inMemoryFilter.isPresent() || sortingInMemory ? Optional.empty() : Optional.ofNullable(projection.getPagination());
    DataStoreIterable<Object> loadedRecords = fetcher.fetch(dataStoreFilter, dataStoreSorting, dataStorePagination, scope);
    if (loadedRecords == null) {
        return new DataStoreIterableBuilder().build();
    }
    if (inMemoryFilter.isPresent() || (loadedRecords.needsInMemoryFilter() && projection.getFilterExpression() != null)) {
        loadedRecords = filterLoadedData(loadedRecords, filterExpression, scope);
    }
    return sortAndPaginateLoadedData(loadedRecords, sortingInMemory, projection.getSorting(), projection.getPagination(), scope);
}
Also used : Pagination(com.yahoo.elide.core.request.Pagination) Optional(java.util.Optional) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Sorting(com.yahoo.elide.core.request.Sorting)

Example 18 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class SubscriptionDataFetcherTest method testRootSubscription.

@Test
void testRootSubscription() {
    Book book1 = new Book();
    book1.setTitle("Book 1");
    book1.setId(1);
    Book book2 = new Book();
    book2.setTitle("Book 2");
    book2.setId(2);
    when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(book1, book2)).build());
    List<String> responses = List.of("{\"book\":{\"id\":\"1\",\"title\":\"Book 1\"}}", "{\"book\":{\"id\":\"2\",\"title\":\"Book 2\"}}");
    String graphQLRequest = "subscription {book(topic: ADDED) {id title}}";
    assertSubscriptionEquals(graphQLRequest, responses);
}
Also used : DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Example 19 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class SubscriptionDataFetcherTest method testRelationshipSubscription.

@Test
void testRelationshipSubscription() {
    Book book1 = new Book();
    book1.setTitle("Book 1");
    book1.setId(1);
    Author author1 = new Author();
    author1.setName("John Doe");
    Author author2 = new Author();
    author1.setName("Jane Doe");
    book1.setAuthors(List.of(author1, author2));
    Book book2 = new Book();
    book2.setTitle("Book 2");
    book2.setId(2);
    when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(book1, book2)).build());
    List<String> responses = List.of("{\"bookAdded\":{\"id\":\"1\",\"title\":\"Book 1\",\"authors\":[{\"name\":\"Jane Doe\"},{\"name\":null}]}}", "{\"bookAdded\":{\"id\":\"2\",\"title\":\"Book 2\",\"authors\":[]}}");
    String graphQLRequest = "subscription {bookAdded: book(topic:ADDED) {id title authors { name }}}";
    assertSubscriptionEquals(graphQLRequest, responses);
}
Also used : DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) Author(example.Author) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Example 20 with DataStoreIterableBuilder

use of com.yahoo.elide.core.datastore.DataStoreIterableBuilder in project elide by yahoo.

the class SubscriptionWebSocketTest method testErrorInStream.

@Test
void testErrorInStream() throws IOException {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
    Book book1 = new Book();
    book1.setTitle("Book 1");
    book1.setId(1);
    Book book2 = new Book();
    book2.setTitle("Book 2");
    book2.setId(2);
    reset(dataStoreTransaction);
    when(dataStoreTransaction.getAttribute(any(), any(), any())).thenThrow(new BadRequestException("Bad Request"));
    when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(book1, book2)).build());
    ConnectionInit init = new ConnectionInit();
    endpoint.onOpen(session);
    endpoint.onMessage(session, mapper.writeValueAsString(init));
    Subscribe subscribe = Subscribe.builder().id("1").payload(Subscribe.Payload.builder().query("subscription {book(topic: ADDED) {id title}}").build()).build();
    endpoint.onMessage(session, mapper.writeValueAsString(subscribe));
    List<String> expected = List.of("{\"type\":\"connection_ack\"}", "{\"type\":\"next\",\"id\":\"1\",\"payload\":{\"data\":{\"book\":{\"id\":\"1\",\"title\":null}},\"errors\":[{\"message\":\"Exception while fetching data (/book/title) : Bad Request\",\"locations\":[{\"line\":1,\"column\":38}],\"path\":[\"book\",\"title\"],\"extensions\":{\"classification\":\"DataFetchingException\"}}]}}", "{\"type\":\"next\",\"id\":\"1\",\"payload\":{\"data\":{\"book\":{\"id\":\"2\",\"title\":null}},\"errors\":[{\"message\":\"Exception while fetching data (/book/title) : Bad Request\",\"locations\":[{\"line\":1,\"column\":38}],\"path\":[\"book\",\"title\"],\"extensions\":{\"classification\":\"DataFetchingException\"}}]}}", "{\"type\":\"complete\",\"id\":\"1\"}");
    ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
    verify(remote, times(4)).sendText(message.capture());
    assertEquals(expected, message.getAllValues());
}
Also used : SubscriptionWebSocket(com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) ConnectionInit(com.yahoo.elide.graphql.subscriptions.websocket.protocol.ConnectionInit) Subscribe(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Aggregations

DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)46 Test (org.junit.jupiter.api.Test)41 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)20 Child (example.Child)16 Book (example.Book)15 EntityProjection (com.yahoo.elide.core.request.EntityProjection)13 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)12 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)11 Parent (example.Parent)11 Path (com.yahoo.elide.core.Path)8 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)7 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)7 Sorting (com.yahoo.elide.core.request.Sorting)7 GraphQLTest (com.yahoo.elide.graphql.GraphQLTest)7 Author (example.Author)7 ArrayList (java.util.ArrayList)7 RequestScope (com.yahoo.elide.core.RequestScope)6 PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)6 Relationship (com.yahoo.elide.core.request.Relationship)6 HashMap (java.util.HashMap)6