Search in sources :

Example 11 with ObjectField

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

the class ObjectRequestHelper method addKeyFields.

public static void addKeyFields(final ObjectRequest objectRequest) {
    var keyCriterias = objectRequest.getKeyCriterias();
    keyCriterias.forEach(keyCriteria -> {
        final AtomicReference<ObjectRequest> current = new AtomicReference<>(objectRequest);
        var fieldPath = keyCriteria.getFieldPath();
        for (int index = 0; index < fieldPath.size(); index++) {
            ObjectField keyField = fieldPath.get(index);
            if (index == (fieldPath.size() - 1)) {
                findOrAddScalarField(current.get(), keyField);
            } else {
                ObjectField nextKeyField = fieldPath.get(index + 1);
                current.set(findOrAddObjectRequest(current.get().getObjectFields(), keyField, nextKeyField));
            }
        }
    });
}
Also used : ObjectRequest(org.dotwebstack.framework.core.query.model.ObjectRequest) ObjectField(org.dotwebstack.framework.core.model.ObjectField) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 12 with ObjectField

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

the class FilterValidator method validateFilterField.

private void validateFilterField(Schema schema, String objectTypeName, Map.Entry<String, FilterConfiguration> filterEntry) {
    String filterFieldName = getFilterField(filterEntry);
    ObjectField objectField = schema.getObjectType(objectTypeName).filter(objectType -> objectType.getFields().containsKey(filterFieldName)).map(objectType -> objectType.getField(filterFieldName)).orElseThrow(() -> invalidConfigurationException("Filter field '{}' not found in object type '{}'.", filterFieldName, objectTypeName));
    var filterConfiguration = filterEntry.getValue();
    if (!filterConfiguration.isCaseSensitive()) {
        if (objectField.isEnumeration()) {
            throw invalidConfigurationException("Filter '{}' with property 'caseSensitive' is 'false' not valid for enumerations.", filterEntry.getKey());
        }
        if (!Objects.equals(objectField.getType(), Scalars.GraphQLString.getName())) {
            throw invalidConfigurationException("Filter '{}' with property 'caseSensitive' is 'false' not valid for type '{}'.", filterEntry.getKey(), objectField.getType());
        }
    }
    if (FilterType.PARTIAL.equals(filterConfiguration.getType()) && !Objects.equals(objectField.getType(), Scalars.GraphQLString.getName())) {
        throw invalidConfigurationException("Filter '{}' of type 'Partial' in object type '{}' doesn´t refer to a 'String' field type.", filterEntry.getKey(), objectTypeName);
    }
}
Also used : Schema(org.dotwebstack.framework.core.model.Schema) OnLocalSchema(org.dotwebstack.framework.core.OnLocalSchema) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Scalars(graphql.Scalars) ObjectField(org.dotwebstack.framework.core.model.ObjectField) Component(org.springframework.stereotype.Component) ExceptionHelper.invalidConfigurationException(org.dotwebstack.framework.core.helpers.ExceptionHelper.invalidConfigurationException) FilterConfiguration(org.dotwebstack.framework.core.config.FilterConfiguration) Map(java.util.Map) ObjectType(org.dotwebstack.framework.core.model.ObjectType) Optional(java.util.Optional) FilterType(org.dotwebstack.framework.core.config.FilterType) Conditional(org.springframework.context.annotation.Conditional) ObjectField(org.dotwebstack.framework.core.model.ObjectField)

Example 13 with ObjectField

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

the class PostgresBackendLoaderTest method loadSingle_returnsMonoObject.

@Test
void loadSingle_returnsMonoObject() {
    Map<String, Object> source = new HashMap<>();
    source.put("a", "bbb");
    RequestContext requestContext = RequestContext.builder().objectField(mock(ObjectField.class)).source(source).build();
    FetchSpec fetchSpec = mock(FetchSpec.class);
    when(fetchSpec.all()).thenReturn(Flux.just(Collections.emptyMap()));
    DatabaseClient.GenericExecuteSpec anotherSpec = mock(DatabaseClient.GenericExecuteSpec.class);
    when(anotherSpec.fetch()).thenReturn(fetchSpec);
    DatabaseClient.GenericExecuteSpec spec = mock(DatabaseClient.GenericExecuteSpec.class);
    when(spec.bind(anyInt(), any())).thenReturn(anotherSpec);
    when(databaseClient.sql(anyString())).thenReturn(spec);
    ObjectRequest objectRequest = initObjectRequest();
    var res = backendLoader.loadSingle(objectRequest, requestContext);
    assertThat(res, CoreMatchers.is(notNullValue()));
    res.doOnNext(result -> assertTrue(result.isEmpty())).subscribe();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) CollectionBatchRequest(org.dotwebstack.framework.core.query.model.CollectionBatchRequest) Mock(org.mockito.Mock) RequestContext(org.dotwebstack.framework.core.query.model.RequestContext) HashMap(java.util.HashMap) FieldRequest(org.dotwebstack.framework.core.query.model.FieldRequest) Disabled(org.junit.jupiter.api.Disabled) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) ContextField(org.dotwebstack.framework.core.model.ContextField) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) PostgresObjectType(org.dotwebstack.framework.backend.postgres.model.PostgresObjectType) Context(org.dotwebstack.framework.core.model.Context) Mockito.when(org.mockito.Mockito.when) CollectionRequest(org.dotwebstack.framework.core.query.model.CollectionRequest) ObjectRequest(org.dotwebstack.framework.core.query.model.ObjectRequest) FetchSpec(org.springframework.r2dbc.core.FetchSpec) JoinCriteria(org.dotwebstack.framework.core.query.model.JoinCriteria) PostgresObjectField(org.dotwebstack.framework.backend.postgres.model.PostgresObjectField) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) ObjectField(org.dotwebstack.framework.core.model.ObjectField) ContextCriteria(org.dotwebstack.framework.core.query.model.ContextCriteria) JoinTable(org.dotwebstack.framework.backend.postgres.model.JoinTable) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ObjectRequest(org.dotwebstack.framework.core.query.model.ObjectRequest) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) HashMap(java.util.HashMap) FetchSpec(org.springframework.r2dbc.core.FetchSpec) PostgresObjectField(org.dotwebstack.framework.backend.postgres.model.PostgresObjectField) ObjectField(org.dotwebstack.framework.core.model.ObjectField) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RequestContext(org.dotwebstack.framework.core.query.model.RequestContext) Test(org.junit.jupiter.api.Test)

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