Search in sources :

Example 6 with ObjectField

use of org.dotwebstack.framework.core.model.ObjectField in project dotwebstack-framework by dotwebstack.

the class FieldPathHelperTest method createObjectFieldPath_returnsListOfObjectField_forPath.

@Test
void createObjectFieldPath_returnsListOfObjectField_forPath() {
    Schema schema = new Schema();
    TestObjectType objectTypeFoo = createFooObjectType();
    TestObjectType objectTypeBar = createBarObjectType();
    TestObjectType objectTypeBaz = createBazObjectType();
    objectTypeFoo.getField("bar").setTargetType(objectTypeBar);
    schema.setObjectTypes(Map.of("Foo", objectTypeFoo, "Bar", objectTypeBar, "Baz", objectTypeBaz));
    var path = "bar.baz";
    List<ObjectField> result = createFieldPath(objectTypeFoo, path);
    assertThat(result.size(), is(2));
    assertThat(result.get(0).getName(), is("bar"));
    assertThat(result.get(1).getName(), is("baz"));
}
Also used : TestObjectType(org.dotwebstack.framework.core.testhelpers.TestObjectType) Schema(org.dotwebstack.framework.core.model.Schema) ObjectField(org.dotwebstack.framework.core.model.ObjectField) TestObjectField(org.dotwebstack.framework.core.testhelpers.TestObjectField) Test(org.junit.jupiter.api.Test)

Example 7 with ObjectField

use of org.dotwebstack.framework.core.model.ObjectField in project dotwebstack-framework by dotwebstack.

the class BackendDataFetcherTest method get_returnsFluxList_ifSourceNull_NotSubscription_and_ListTypeTrue.

@Test
void get_returnsFluxList_ifSourceNull_NotSubscription_and_ListTypeTrue() {
    var requestContext = RequestContext.builder().objectField(mock(ObjectField.class)).source(null).build();
    when(requestFactory.createRequestContext(environment)).thenReturn(requestContext);
    when(environment.getSource()).thenReturn(null);
    graphql.language.Field fieldMock = new Field("aaa");
    lenient().when(environment.getField()).thenReturn(fieldMock);
    GraphQLList listType = mock(GraphQLList.class);
    when(environment.getFieldType()).thenReturn(listType);
    mockOperationDefinition(OperationDefinition.Operation.QUERY);
    CollectionRequest collectionRequestMock = CollectionRequest.builder().objectRequest(ObjectRequest.builder().build()).build();
    DataFetchingFieldSelectionSet selectionSet = mock(DataFetchingFieldSelectionSet.class);
    when(environment.getSelectionSet()).thenReturn(selectionSet);
    var executionStepInfo = mockExecutionStepInfoWithResultPath("fff", "fff");
    lenient().when(requestFactory.createCollectionRequest(eq(executionStepInfo), eq(selectionSet))).thenReturn(collectionRequestMock);
    Map<String, Object> resultMock = new HashMap<>();
    resultMock.put("aa", new String[] { "a", "b" });
    when(backendLoader.loadMany(any(CollectionRequest.class), any(RequestContext.class))).thenReturn(Flux.just(resultMock));
    mockGraphQlFieldDefinition(Map.of());
    var result = ((CompletableFuture<?>) backendDataFetcher.get(environment)).join();
    assertThat(result, CoreMatchers.is(notNullValue()));
    assertTrue(result instanceof List);
    assertThat(((List<?>) result).get(0), is(resultMock));
    verify(requestFactory).createCollectionRequest(any(ExecutionStepInfo.class), any(DataFetchingFieldSelectionSet.class));
    verify(backendLoader).loadMany(any(CollectionRequest.class), any(RequestContext.class));
}
Also used : GraphQLList(graphql.schema.GraphQLList) CollectionRequest(org.dotwebstack.framework.core.query.model.CollectionRequest) HashMap(java.util.HashMap) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) Field(graphql.language.Field) ObjectField(org.dotwebstack.framework.core.model.ObjectField) MergedField(graphql.execution.MergedField) Field(graphql.language.Field) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) ObjectField(org.dotwebstack.framework.core.model.ObjectField) GraphQLObjectType.newObject(graphql.schema.GraphQLObjectType.newObject) List(java.util.List) ArrayList(java.util.ArrayList) GraphQLList(graphql.schema.GraphQLList) RequestContext(org.dotwebstack.framework.core.query.model.RequestContext) Test(org.junit.jupiter.api.Test)

Example 8 with ObjectField

use of org.dotwebstack.framework.core.model.ObjectField in project dotwebstack-framework by dotwebstack.

the class BackendDataFetcherTest method get_returnsMonoMap_ifSourceNull_SubscriptionFalse_and_ListTypeFalse.

@Test
void get_returnsMonoMap_ifSourceNull_SubscriptionFalse_and_ListTypeFalse() {
    var requestContext = RequestContext.builder().objectField(mock(ObjectField.class)).source(null).build();
    lenient().when(requestFactory.createRequestContext(environment)).thenReturn(requestContext);
    when(environment.getSource()).thenReturn(null);
    mockOperationDefinition(OperationDefinition.Operation.QUERY);
    var objectRequest = ObjectRequest.builder().build();
    DataFetchingFieldSelectionSet selectionSet = mock(DataFetchingFieldSelectionSet.class);
    when(environment.getSelectionSet()).thenReturn(selectionSet);
    var executionStepInfo = mockExecutionStepInfo("fff", "fff");
    lenient().when(requestFactory.createObjectRequest(eq(executionStepInfo), eq(selectionSet))).thenReturn(objectRequest);
    Map<String, Object> data = new HashMap<>();
    data.put("aa", new String[] { "a", "b" });
    when(backendLoader.loadSingle(objectRequest, requestContext)).thenReturn(Mono.just(data));
    mockGraphQlFieldDefinition(Map.of());
    var result = ((CompletableFuture<?>) backendDataFetcher.get(environment)).join();
    assertTrue(result instanceof Map);
    assertThat(((Map<?, ?>) result).get("aa"), is(data.get("aa")));
    verify(requestFactory).createObjectRequest(any(ExecutionStepInfo.class), any(DataFetchingFieldSelectionSet.class));
    verify(backendLoader).loadSingle(any(ObjectRequest.class), any(RequestContext.class));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ObjectRequest(org.dotwebstack.framework.core.query.model.ObjectRequest) HashMap(java.util.HashMap) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) ObjectField(org.dotwebstack.framework.core.model.ObjectField) GraphQLObjectType.newObject(graphql.schema.GraphQLObjectType.newObject) RequestContext(org.dotwebstack.framework.core.query.model.RequestContext) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 9 with ObjectField

use of org.dotwebstack.framework.core.model.ObjectField in project dotwebstack-framework by dotwebstack.

the class ModelHelperTest method createObjectFieldPath_returnsListOfObjectField_forPath.

@Test
void createObjectFieldPath_returnsListOfObjectField_forPath() {
    Schema schema = new Schema();
    TestObjectType objectTypeFoo = createFooObjectType();
    TestObjectType objectTypeBar = createBarObjectType();
    TestObjectType objectTypeBaz = createBazObjectType();
    schema.setObjectTypes(Map.of("Foo", objectTypeFoo, "Bar", objectTypeBar, "Baz", objectTypeBaz));
    var path = "bar.baz";
    List<ObjectField> result = ModelHelper.createObjectFieldPath(schema, objectTypeFoo, path);
    assertThat(result.size(), is(2));
    assertThat(result.get(0).getName(), is("bar"));
    assertThat(result.get(1).getName(), is("baz"));
}
Also used : TestObjectType(org.dotwebstack.framework.core.testhelpers.TestObjectType) Schema(org.dotwebstack.framework.core.model.Schema) TestObjectField(org.dotwebstack.framework.core.testhelpers.TestObjectField) ObjectField(org.dotwebstack.framework.core.model.ObjectField) Test(org.junit.jupiter.api.Test)

Example 10 with ObjectField

use of org.dotwebstack.framework.core.model.ObjectField in project dotwebstack-framework by dotwebstack.

the class ObjectRequestHelper method addSortFields.

private static void addSortFields(CollectionRequest collectionRequest, SortCriteria sortCriteria) {
    ObjectRequest objectRequest = collectionRequest.getObjectRequest();
    for (int index = 0; index < sortCriteria.getFieldPath().size(); index++) {
        ObjectField sortField = sortCriteria.getFieldPath().get(index);
        if (index == (sortCriteria.getFieldPath().size() - 1)) {
            findOrAddScalarField(objectRequest, sortField);
        } else {
            ObjectField nextSortField = sortCriteria.getFieldPath().get(index + 1);
            objectRequest = findOrAddObjectRequest(objectRequest.getObjectFields(), sortField, nextSortField);
        }
    }
}
Also used : ObjectRequest(org.dotwebstack.framework.core.query.model.ObjectRequest) ObjectField(org.dotwebstack.framework.core.model.ObjectField)

Aggregations

ObjectField (org.dotwebstack.framework.core.model.ObjectField)13 Test (org.junit.jupiter.api.Test)10 HashMap (java.util.HashMap)6 Map (java.util.Map)5 ObjectRequest (org.dotwebstack.framework.core.query.model.ObjectRequest)5 RequestContext (org.dotwebstack.framework.core.query.model.RequestContext)5 ExecutionStepInfo (graphql.execution.ExecutionStepInfo)4 DataFetchingFieldSelectionSet (graphql.schema.DataFetchingFieldSelectionSet)4 GraphQLObjectType.newObject (graphql.schema.GraphQLObjectType.newObject)4 CollectionRequest (org.dotwebstack.framework.core.query.model.CollectionRequest)4 MergedField (graphql.execution.MergedField)3 Field (graphql.language.Field)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Schema (org.dotwebstack.framework.core.model.Schema)3 GraphQLList (graphql.schema.GraphQLList)2 Collections (java.util.Collections)2 JoinTable (org.dotwebstack.framework.backend.postgres.model.JoinTable)2 PostgresObjectField (org.dotwebstack.framework.backend.postgres.model.PostgresObjectField)2 PostgresObjectType (org.dotwebstack.framework.backend.postgres.model.PostgresObjectType)2