Search in sources :

Example 1 with HQueryCondition

use of io.hdocdb.store.HQueryCondition in project kgiraffe by rayokota.

the class GraphQLQueryFactory method getArgumentsPredicate.

protected HQueryCondition getArgumentsPredicate(DataFetchingEnvironment env, Argument argument) {
    ArrayValue whereValue = getValue(argument, env);
    if (whereValue.getValues().isEmpty()) {
        return new HQueryCondition().build();
    }
    Logical logical = extractLogical(argument);
    List<HQueryCondition> predicates = new ArrayList<>();
    List<Map<String, Object>> arguments = env.getArgument(logical.symbol());
    List<ObjectValue> values = whereValue.getValues().stream().map(ObjectValue.class::cast).collect(Collectors.toList());
    List<SimpleEntry<ObjectValue, Map<String, Object>>> tuples = IntStream.range(0, values.size()).mapToObj(i -> new SimpleEntry<>(values.get(i), arguments.get(i))).collect(Collectors.toList());
    tuples.stream().flatMap(e -> e.getKey().getObjectFields().stream().filter(it -> Logical.symbols().contains(it.getName())).map(it -> {
        Map<String, Object> args = e.getValue();
        Argument arg = new Argument(it.getName(), it.getValue());
        if (it.getValue() instanceof ArrayValue) {
            return getArgumentsPredicate(argumentEnvironment(env, args), arg);
        }
        return getArgumentPredicate(argumentEnvironment(env, args), arg);
    })).forEach(predicates::add);
    tuples.stream().flatMap(e -> e.getKey().getObjectFields().stream().filter(it -> !Logical.symbols().contains(it.getName())).map(it -> {
        Map<String, Object> args = e.getValue();
        Argument arg = new Argument(it.getName(), it.getValue());
        return getObjectFieldPredicate(env, logical, it, arg, args);
    })).filter(Objects::nonNull).forEach(predicates::add);
    return getCompoundPredicate(predicates, logical);
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) ValuesResolver(graphql.execution.ValuesResolver) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) DocumentStore(org.ojai.store.DocumentStore) LoggerFactory(org.slf4j.LoggerFactory) ConditionNode(io.hdocdb.store.ConditionNode) LIMIT_PARAM_NAME(io.kgraph.kgiraffe.schema.GraphQLSchemaBuilder.LIMIT_PARAM_NAME) InputValueWithState(graphql.schema.InputValueWithState) Map(java.util.Map) Document(org.ojai.Document) GraphQLException(graphql.GraphQLException) TypeMetaFieldDef(graphql.introspection.Introspection.TypeMetaFieldDef) ConditionLeaf(io.hdocdb.store.ConditionLeaf) DataFetchingEnvironmentBuilder(io.kgraph.kgiraffe.schema.util.DataFetchingEnvironmentBuilder) Collection(java.util.Collection) ObjectField(graphql.language.ObjectField) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ArrayValue(graphql.language.ArrayValue) Optional(java.util.Optional) GraphQLSupport(io.kgraph.kgiraffe.schema.util.GraphQLSupport) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) IntStream(java.util.stream.IntStream) Iterables(com.google.common.collect.Iterables) HQueryCondition(io.hdocdb.store.HQueryCondition) EnumValue(graphql.language.EnumValue) GraphQLType(graphql.schema.GraphQLType) Streams(io.kcache.utils.Streams) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ORDER_BY_PARAM_NAME(io.kgraph.kgiraffe.schema.GraphQLSchemaBuilder.ORDER_BY_PARAM_NAME) VariableReference(graphql.language.VariableReference) GraphQLSchema(graphql.schema.GraphQLSchema) ConditionParent(io.hdocdb.store.ConditionParent) SimpleEntry(java.util.AbstractMap.SimpleEntry) TypeNameMetaFieldDef(graphql.introspection.Introspection.TypeNameMetaFieldDef) OFFSET_PARAM_NAME(io.kgraph.kgiraffe.schema.GraphQLSchemaBuilder.OFFSET_PARAM_NAME) Criteria(io.kgraph.kgiraffe.schema.PredicateFilter.Criteria) Logger(org.slf4j.Logger) KGiraffeEngine(io.kgraph.kgiraffe.KGiraffeEngine) FieldPath(org.ojai.FieldPath) GraphQLOutputType(graphql.schema.GraphQLOutputType) Field(graphql.language.Field) GraphQLImplementingType(graphql.schema.GraphQLImplementingType) QueryCondition(org.ojai.store.QueryCondition) Argument(graphql.language.Argument) GraphQLList(graphql.schema.GraphQLList) StringValue(graphql.language.StringValue) Tuple2(io.vavr.Tuple2) HValue(io.hdocdb.HValue) IntValue(graphql.language.IntValue) SchemaMetaFieldDef(graphql.introspection.Introspection.SchemaMetaFieldDef) Comparator(java.util.Comparator) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Collections(java.util.Collections) BooleanValue(graphql.language.BooleanValue) GraphQLArgument(graphql.schema.GraphQLArgument) Argument(graphql.language.Argument) SimpleEntry(java.util.AbstractMap.SimpleEntry) ArrayList(java.util.ArrayList) ObjectValue(graphql.language.ObjectValue) ArrayValue(graphql.language.ArrayValue) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HQueryCondition(io.hdocdb.store.HQueryCondition)

Example 2 with HQueryCondition

use of io.hdocdb.store.HQueryCondition in project kgiraffe by rayokota.

the class GraphQLQueryFactory method getLogicalPredicate.

private HQueryCondition getLogicalPredicate(String fieldName, ObjectField objectField, DataFetchingEnvironment env, Argument argument) {
    ObjectValue expressionValue;
    if (objectField.getValue() instanceof ObjectValue) {
        expressionValue = (ObjectValue) objectField.getValue();
    } else {
        expressionValue = new ObjectValue(Collections.singletonList(objectField));
    }
    if (expressionValue.getChildren().isEmpty()) {
        return new HQueryCondition().build();
    }
    Logical logical = extractLogical(argument);
    List<HQueryCondition> predicates = new ArrayList<>();
    // Let's parse relation criteria expressions if present, i.e. books, author, etc.
    if (expressionValue.getObjectFields().stream().anyMatch(it -> !Criteria.symbols().contains(it.getName()))) {
        GraphQLFieldDefinition fieldDefinition = getFieldDefinition(env.getGraphQLSchema(), this.getImplementingType(env), new Field(fieldName));
        Map<String, Object> args = new LinkedHashMap<>();
        Argument arg = new Argument(logical.symbol(), expressionValue);
        if (Logical.symbols().contains(argument.getName())) {
            args.put(logical.symbol(), env.getArgument(argument.getName()));
        } else {
            args.put(logical.symbol(), env.getArgument(fieldName));
        }
        HQueryCondition predicate = getArgumentPredicate(wherePredicateEnvironment(env, fieldDefinition, args), arg);
        // Augment the FieldPath instances in the condition
        if (objectField.getValue() instanceof ObjectValue) {
            predicate = addNewParentToPaths(objectField.getName(), predicate);
        }
        return predicate;
    }
    // Let's parse simple Criteria expressions, i.e. EQ, LIKE, etc.
    expressionValue.getObjectFields().stream().filter(it -> Criteria.symbols().contains(it.getName())).map(it -> getPredicateFilter(new ObjectField(fieldName, it.getValue()), argumentEnvironment(env, argument), new Argument(it.getName(), it.getValue()))).sorted().map(it -> it.toQueryCondition(env)).filter(Objects::nonNull).forEach(predicates::add);
    return getCompoundPredicate(predicates, logical);
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) ValuesResolver(graphql.execution.ValuesResolver) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) DocumentStore(org.ojai.store.DocumentStore) LoggerFactory(org.slf4j.LoggerFactory) ConditionNode(io.hdocdb.store.ConditionNode) LIMIT_PARAM_NAME(io.kgraph.kgiraffe.schema.GraphQLSchemaBuilder.LIMIT_PARAM_NAME) InputValueWithState(graphql.schema.InputValueWithState) Map(java.util.Map) Document(org.ojai.Document) GraphQLException(graphql.GraphQLException) TypeMetaFieldDef(graphql.introspection.Introspection.TypeMetaFieldDef) ConditionLeaf(io.hdocdb.store.ConditionLeaf) DataFetchingEnvironmentBuilder(io.kgraph.kgiraffe.schema.util.DataFetchingEnvironmentBuilder) Collection(java.util.Collection) ObjectField(graphql.language.ObjectField) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ArrayValue(graphql.language.ArrayValue) Optional(java.util.Optional) GraphQLSupport(io.kgraph.kgiraffe.schema.util.GraphQLSupport) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) IntStream(java.util.stream.IntStream) Iterables(com.google.common.collect.Iterables) HQueryCondition(io.hdocdb.store.HQueryCondition) EnumValue(graphql.language.EnumValue) GraphQLType(graphql.schema.GraphQLType) Streams(io.kcache.utils.Streams) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ORDER_BY_PARAM_NAME(io.kgraph.kgiraffe.schema.GraphQLSchemaBuilder.ORDER_BY_PARAM_NAME) VariableReference(graphql.language.VariableReference) GraphQLSchema(graphql.schema.GraphQLSchema) ConditionParent(io.hdocdb.store.ConditionParent) SimpleEntry(java.util.AbstractMap.SimpleEntry) TypeNameMetaFieldDef(graphql.introspection.Introspection.TypeNameMetaFieldDef) OFFSET_PARAM_NAME(io.kgraph.kgiraffe.schema.GraphQLSchemaBuilder.OFFSET_PARAM_NAME) Criteria(io.kgraph.kgiraffe.schema.PredicateFilter.Criteria) Logger(org.slf4j.Logger) KGiraffeEngine(io.kgraph.kgiraffe.KGiraffeEngine) FieldPath(org.ojai.FieldPath) GraphQLOutputType(graphql.schema.GraphQLOutputType) Field(graphql.language.Field) GraphQLImplementingType(graphql.schema.GraphQLImplementingType) QueryCondition(org.ojai.store.QueryCondition) Argument(graphql.language.Argument) GraphQLList(graphql.schema.GraphQLList) StringValue(graphql.language.StringValue) Tuple2(io.vavr.Tuple2) HValue(io.hdocdb.HValue) IntValue(graphql.language.IntValue) SchemaMetaFieldDef(graphql.introspection.Introspection.SchemaMetaFieldDef) Comparator(java.util.Comparator) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Collections(java.util.Collections) BooleanValue(graphql.language.BooleanValue) GraphQLArgument(graphql.schema.GraphQLArgument) Argument(graphql.language.Argument) ArrayList(java.util.ArrayList) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) LinkedHashMap(java.util.LinkedHashMap) ObjectField(graphql.language.ObjectField) Field(graphql.language.Field) ObjectValue(graphql.language.ObjectValue) ObjectField(graphql.language.ObjectField) HQueryCondition(io.hdocdb.store.HQueryCondition)

Example 3 with HQueryCondition

use of io.hdocdb.store.HQueryCondition in project kgiraffe by rayokota.

the class SubscriptionFetcher method get.

@Override
public Object get(DataFetchingEnvironment env) {
    try {
        HQueryCondition query = queryFactory.getCriteriaQuery(env, env.getField());
        Flowable<Document> publisher = engine.getNotifier().consumer(topic).filter(doc -> query == null || query.isEmpty() || query.evaluate(doc));
        return publisher;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Document(org.ojai.Document) HQueryCondition(io.hdocdb.store.HQueryCondition)

Example 4 with HQueryCondition

use of io.hdocdb.store.HQueryCondition in project kgiraffe by rayokota.

the class GraphQLQueryFactory method getWherePredicate.

protected HQueryCondition getWherePredicate(DataFetchingEnvironment env, Argument argument) {
    ObjectValue whereValue = getValue(argument, env);
    if (whereValue.getChildren().isEmpty()) {
        return new HQueryCondition().build();
    }
    Logical logical = extractLogical(argument);
    Map<String, Object> predicateArguments = new LinkedHashMap<>();
    predicateArguments.put(logical.symbol(), env.getArguments());
    DataFetchingEnvironment predicateDataFetchingEnv = DataFetchingEnvironmentBuilder.newDataFetchingEnvironment(env).arguments(predicateArguments).build();
    Argument predicateArgument = new Argument(logical.symbol(), whereValue);
    return getArgumentPredicate(predicateDataFetchingEnv, predicateArgument);
}
Also used : ObjectValue(graphql.language.ObjectValue) GraphQLArgument(graphql.schema.GraphQLArgument) Argument(graphql.language.Argument) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) HQueryCondition(io.hdocdb.store.HQueryCondition) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with HQueryCondition

use of io.hdocdb.store.HQueryCondition in project kgiraffe by rayokota.

the class GraphQLQueryFactory method getArgumentPredicate.

protected HQueryCondition getArgumentPredicate(DataFetchingEnvironment env, Argument argument) {
    ObjectValue whereValue = getValue(argument, env);
    if (whereValue.getChildren().isEmpty()) {
        return new HQueryCondition().build();
    }
    Logical logical = extractLogical(argument);
    List<HQueryCondition> predicates = new ArrayList<>();
    whereValue.getObjectFields().stream().filter(it -> Logical.symbols().contains(it.getName())).map(it -> {
        Map<String, Object> arguments = getFieldArguments(env, it, argument);
        if (it.getValue() instanceof ArrayValue) {
            return getArgumentsPredicate(argumentEnvironment(env, arguments), new Argument(it.getName(), it.getValue()));
        }
        return getArgumentPredicate(argumentEnvironment(env, arguments), new Argument(it.getName(), it.getValue()));
    }).forEach(predicates::add);
    whereValue.getObjectFields().stream().filter(it -> !Logical.symbols().contains(it.getName())).map(it -> {
        Map<String, Object> args = getFieldArguments(env, it, argument);
        Argument arg = new Argument(it.getName(), it.getValue());
        return getObjectFieldPredicate(env, logical, it, arg, args);
    }).filter(Objects::nonNull).forEach(predicates::add);
    return getCompoundPredicate(predicates, logical);
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) ValuesResolver(graphql.execution.ValuesResolver) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) DocumentStore(org.ojai.store.DocumentStore) LoggerFactory(org.slf4j.LoggerFactory) ConditionNode(io.hdocdb.store.ConditionNode) LIMIT_PARAM_NAME(io.kgraph.kgiraffe.schema.GraphQLSchemaBuilder.LIMIT_PARAM_NAME) InputValueWithState(graphql.schema.InputValueWithState) Map(java.util.Map) Document(org.ojai.Document) GraphQLException(graphql.GraphQLException) TypeMetaFieldDef(graphql.introspection.Introspection.TypeMetaFieldDef) ConditionLeaf(io.hdocdb.store.ConditionLeaf) DataFetchingEnvironmentBuilder(io.kgraph.kgiraffe.schema.util.DataFetchingEnvironmentBuilder) Collection(java.util.Collection) ObjectField(graphql.language.ObjectField) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ArrayValue(graphql.language.ArrayValue) Optional(java.util.Optional) GraphQLSupport(io.kgraph.kgiraffe.schema.util.GraphQLSupport) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) IntStream(java.util.stream.IntStream) Iterables(com.google.common.collect.Iterables) HQueryCondition(io.hdocdb.store.HQueryCondition) EnumValue(graphql.language.EnumValue) GraphQLType(graphql.schema.GraphQLType) Streams(io.kcache.utils.Streams) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ORDER_BY_PARAM_NAME(io.kgraph.kgiraffe.schema.GraphQLSchemaBuilder.ORDER_BY_PARAM_NAME) VariableReference(graphql.language.VariableReference) GraphQLSchema(graphql.schema.GraphQLSchema) ConditionParent(io.hdocdb.store.ConditionParent) SimpleEntry(java.util.AbstractMap.SimpleEntry) TypeNameMetaFieldDef(graphql.introspection.Introspection.TypeNameMetaFieldDef) OFFSET_PARAM_NAME(io.kgraph.kgiraffe.schema.GraphQLSchemaBuilder.OFFSET_PARAM_NAME) Criteria(io.kgraph.kgiraffe.schema.PredicateFilter.Criteria) Logger(org.slf4j.Logger) KGiraffeEngine(io.kgraph.kgiraffe.KGiraffeEngine) FieldPath(org.ojai.FieldPath) GraphQLOutputType(graphql.schema.GraphQLOutputType) Field(graphql.language.Field) GraphQLImplementingType(graphql.schema.GraphQLImplementingType) QueryCondition(org.ojai.store.QueryCondition) Argument(graphql.language.Argument) GraphQLList(graphql.schema.GraphQLList) StringValue(graphql.language.StringValue) Tuple2(io.vavr.Tuple2) HValue(io.hdocdb.HValue) IntValue(graphql.language.IntValue) SchemaMetaFieldDef(graphql.introspection.Introspection.SchemaMetaFieldDef) Comparator(java.util.Comparator) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Collections(java.util.Collections) BooleanValue(graphql.language.BooleanValue) GraphQLArgument(graphql.schema.GraphQLArgument) Argument(graphql.language.Argument) ArrayList(java.util.ArrayList) ObjectValue(graphql.language.ObjectValue) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ArrayValue(graphql.language.ArrayValue) HQueryCondition(io.hdocdb.store.HQueryCondition)

Aggregations

HQueryCondition (io.hdocdb.store.HQueryCondition)5 Argument (graphql.language.Argument)4 ObjectValue (graphql.language.ObjectValue)4 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)4 GraphQLArgument (graphql.schema.GraphQLArgument)4 Iterables (com.google.common.collect.Iterables)3 GraphQLException (graphql.GraphQLException)3 ValuesResolver (graphql.execution.ValuesResolver)3 SchemaMetaFieldDef (graphql.introspection.Introspection.SchemaMetaFieldDef)3 TypeMetaFieldDef (graphql.introspection.Introspection.TypeMetaFieldDef)3 TypeNameMetaFieldDef (graphql.introspection.Introspection.TypeNameMetaFieldDef)3 ArrayValue (graphql.language.ArrayValue)3 BooleanValue (graphql.language.BooleanValue)3 EnumValue (graphql.language.EnumValue)3 Field (graphql.language.Field)3 FloatValue (graphql.language.FloatValue)3 IntValue (graphql.language.IntValue)3 NullValue (graphql.language.NullValue)3 ObjectField (graphql.language.ObjectField)3 StringValue (graphql.language.StringValue)3