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);
}
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);
}
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);
}
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);
}
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());
}
Aggregations