Search in sources :

Example 41 with DataStoreIterableBuilder

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

the class JPQLTransaction method getToManyRelation.

@Override
public <T, R> DataStoreIterable<R> getToManyRelation(DataStoreTransaction relationTx, T entity, Relationship relation, RequestScope scope) {
    FilterExpression filterExpression = relation.getProjection().getFilterExpression();
    Sorting sorting = relation.getProjection().getSorting();
    Pagination pagination = relation.getProjection().getPagination();
    EntityDictionary dictionary = scope.getDictionary();
    Iterable val = (Iterable) com.yahoo.elide.core.PersistentResource.getValue(entity, relation.getName(), scope);
    // If the query is safe for N+1 and the value is an ORM managed, persistent collection, run a JPQL query...
    if (doInDatabase(entity) && val instanceof Collection && isPersistentCollection().test((Collection<?>) val)) {
        /*
             * If there is no filtering or sorting required in the data store, and the pagination is default,
             * return the proxy and let Hibernate manage the SQL generation.
             */
        if (filterExpression == null && sorting == null && (pagination == null || (pagination.isDefaultInstance()))) {
            return new DataStoreIterableBuilder<R>(addSingleElement(val)).allInMemory().build();
        }
        RelationshipImpl relationship = new RelationshipImpl(dictionary.lookupEntityClass(EntityDictionary.getType(entity)), entity, relation);
        if (pagination != null && pagination.returnPageTotals()) {
            pagination.setPageTotals(getTotalRecords(relationship, scope.getDictionary()));
        }
        final Query query = new SubCollectionFetchQueryBuilder(relationship, dictionary, sessionWrapper).build();
        if (query != null) {
            return new DataStoreIterableBuilder(addSingleElement(query.list())).build();
        }
    }
    return new DataStoreIterableBuilder<R>(addSingleElement(val)).allInMemory().build();
}
Also used : Pagination(com.yahoo.elide.core.request.Pagination) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Query(com.yahoo.elide.datastores.jpql.porting.Query) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) Collection(java.util.Collection) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) Sorting(com.yahoo.elide.core.request.Sorting)

Example 42 with DataStoreIterableBuilder

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

the class LifeCycleTest method testElidePatchRelationshipAddMultiple.

@Test
public void testElidePatchRelationshipAddMultiple() {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel parent = mock(FieldTestModel.class);
    FieldTestModel child1 = mock(FieldTestModel.class);
    FieldTestModel child2 = mock(FieldTestModel.class);
    FieldTestModel child3 = mock(FieldTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
    String body = "{\"data\": {\"type\":\"testModel\",\"id\":\"1\",\"relationships\": { \"models\": { \"data\": [ { \"type\": \"testModel\", \"id\": \"2\" }, {\"type\": \"testModel\", \"id\": \"3\" } ] } } } }";
    dictionary.setValue(parent, "id", "1");
    dictionary.setValue(child1, "id", "2");
    dictionary.setValue(child2, "id", "3");
    dictionary.setValue(child3, "id", "4");
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.loadObject(isA(EntityProjection.class), eq("1"), isA(RequestScope.class))).thenReturn(parent);
    when(tx.loadObject(isA(EntityProjection.class), eq("2"), isA(RequestScope.class))).thenReturn(child1);
    when(tx.loadObject(isA(EntityProjection.class), eq("3"), isA(RequestScope.class))).thenReturn(child2);
    when(tx.loadObject(isA(EntityProjection.class), eq("4"), isA(RequestScope.class))).thenReturn(child3);
    DataStoreIterable iterable = new DataStoreIterableBuilder(List.of(child3)).build();
    when(tx.getToManyRelation(any(), any(), isA(Relationship.class), isA(RequestScope.class))).thenReturn(iterable);
    String contentType = JSONAPI_CONTENT_TYPE;
    ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/testModel/1", body, null, NO_VERSION);
    assertEquals(HttpStatus.SC_NO_CONTENT, response.getResponseCode());
    verify(parent, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), notNull());
    verify(parent, times(4)).classCallback(eq(UPDATE), any());
    verify(parent, never()).classAllFieldsCallback(any(), any());
    verify(parent, never()).attributeCallback(any(), any(), any());
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) Relationship(com.yahoo.elide.core.request.Relationship) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Elide(com.yahoo.elide.Elide) RequestScope(com.yahoo.elide.core.RequestScope) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) Test(org.junit.jupiter.api.Test)

Example 43 with DataStoreIterableBuilder

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

the class SubscriptionDataFetcherTest method testRootNonSchemaQuery.

@Test
void testRootNonSchemaQuery() {
    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\":null}");
    List<String> errors = List.of("QUERY not supported for subscription models");
    String graphQLRequest = "query {book(topic: ADDED) {id title}}";
    assertSubscriptionEquals(graphQLRequest, responses, errors);
}
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 44 with DataStoreIterableBuilder

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

the class SubscriptionDataFetcherTest method testComplexAttribute.

@Test
void testComplexAttribute() {
    Author author1 = new Author();
    author1.setId(1L);
    author1.setHomeAddress(new Address());
    Author author2 = new Author();
    author2.setId(2L);
    Address address = new Address();
    address.setStreet1("123");
    address.setStreet2("XYZ");
    author2.setHomeAddress(address);
    when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(author1, author2)).build());
    List<String> responses = List.of("{\"author\":{\"id\":\"1\",\"homeAddress\":{\"street1\":null,\"street2\":null}}}", "{\"author\":{\"id\":\"2\",\"homeAddress\":{\"street1\":\"123\",\"street2\":\"XYZ\"}}}");
    String graphQLRequest = "subscription {author(topic: UPDATED) {id homeAddress { street1 street2 }}}";
    assertSubscriptionEquals(graphQLRequest, responses);
}
Also used : Address(example.Address) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Author(example.Author) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Example 45 with DataStoreIterableBuilder

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

the class SubscriptionDataFetcherTest method testErrorInSubscriptionStream.

@Test
void testErrorInSubscriptionStream() {
    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());
    List<String> responses = List.of("{\"book\":{\"id\":\"1\",\"title\":null}}", "{\"book\":{\"id\":\"2\",\"title\":null}}");
    List<String> errors = List.of("Bad Request", "Bad Request");
    String graphQLRequest = "subscription {book(topic: ADDED) {id title}}";
    assertSubscriptionEquals(graphQLRequest, responses, errors);
}
Also used : DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) 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