Search in sources :

Example 1 with VariableReference

use of graphql.language.VariableReference in project graphql-java by graphql-java.

the class GraphqlAntlrToLanguage method getValue.

private Value getValue(GraphqlParser.ValueWithVariableContext ctx) {
    if (ctx.IntValue() != null) {
        IntValue intValue = new IntValue(new BigInteger(ctx.IntValue().getText()));
        newNode(intValue, ctx);
        return intValue;
    } else if (ctx.FloatValue() != null) {
        FloatValue floatValue = new FloatValue(new BigDecimal(ctx.FloatValue().getText()));
        newNode(floatValue, ctx);
        return floatValue;
    } else if (ctx.BooleanValue() != null) {
        BooleanValue booleanValue = new BooleanValue(Boolean.parseBoolean(ctx.BooleanValue().getText()));
        newNode(booleanValue, ctx);
        return booleanValue;
    } else if (ctx.NullValue() != null) {
        newNode(Null, ctx);
        return Null;
    } else if (ctx.stringValue() != null) {
        StringValue stringValue = new StringValue(quotedString(ctx.stringValue()));
        newNode(stringValue, ctx);
        return stringValue;
    } else if (ctx.enumValue() != null) {
        EnumValue enumValue = new EnumValue(ctx.enumValue().getText());
        newNode(enumValue, ctx);
        return enumValue;
    } else if (ctx.arrayValueWithVariable() != null) {
        ArrayValue arrayValue = new ArrayValue();
        newNode(arrayValue, ctx);
        for (GraphqlParser.ValueWithVariableContext valueWithVariableContext : ctx.arrayValueWithVariable().valueWithVariable()) {
            arrayValue.getValues().add(getValue(valueWithVariableContext));
        }
        return arrayValue;
    } else if (ctx.objectValueWithVariable() != null) {
        ObjectValue objectValue = new ObjectValue();
        newNode(objectValue, ctx);
        for (GraphqlParser.ObjectFieldWithVariableContext objectFieldWithVariableContext : ctx.objectValueWithVariable().objectFieldWithVariable()) {
            ObjectField objectField = new ObjectField(objectFieldWithVariableContext.name().getText(), getValue(objectFieldWithVariableContext.valueWithVariable()));
            objectValue.getObjectFields().add(objectField);
        }
        return objectValue;
    } else if (ctx.variable() != null) {
        VariableReference variableReference = new VariableReference(ctx.variable().name().getText());
        newNode(variableReference, ctx);
        return variableReference;
    }
    return Assert.assertShouldNeverHappen();
}
Also used : VariableReference(graphql.language.VariableReference) EnumValue(graphql.language.EnumValue) BigDecimal(java.math.BigDecimal) ObjectValue(graphql.language.ObjectValue) GraphqlParser(graphql.parser.antlr.GraphqlParser) BooleanValue(graphql.language.BooleanValue) ObjectField(graphql.language.ObjectField) BigInteger(java.math.BigInteger) FloatValue(graphql.language.FloatValue) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) ArrayValue(graphql.language.ArrayValue)

Example 2 with VariableReference

use of graphql.language.VariableReference in project graphql-java by graphql-java.

the class ValuesResolver method coerceValueAstForInputObject.

private Object coerceValueAstForInputObject(GraphqlFieldVisibility fieldVisibility, GraphQLInputObjectType type, ObjectValue inputValue, Map<String, Object> variables) {
    Map<String, Object> result = new LinkedHashMap<>();
    Map<String, ObjectField> inputValueFieldsByName = mapObjectValueFieldsByName(inputValue);
    List<GraphQLInputObjectField> inputFields = fieldVisibility.getFieldDefinitions(type);
    for (GraphQLInputObjectField inputTypeField : inputFields) {
        if (inputValueFieldsByName.containsKey(inputTypeField.getName())) {
            boolean putObjectInMap = true;
            ObjectField field = inputValueFieldsByName.get(inputTypeField.getName());
            Value fieldInputValue = field.getValue();
            Object fieldObject = null;
            if (fieldInputValue instanceof VariableReference) {
                String varName = ((VariableReference) fieldInputValue).getName();
                if (!variables.containsKey(varName)) {
                    putObjectInMap = false;
                } else {
                    fieldObject = variables.get(varName);
                }
            } else {
                fieldObject = coerceValueAst(fieldVisibility, inputTypeField.getType(), fieldInputValue, variables);
            }
            if (fieldObject == null) {
                if (!field.getValue().isEqualTo(NullValue.Null)) {
                    fieldObject = inputTypeField.getDefaultValue();
                }
            }
            if (putObjectInMap) {
                result.put(field.getName(), fieldObject);
            } else {
                assertNonNullInputField(inputTypeField);
            }
        } else if (inputTypeField.getDefaultValue() != null) {
            result.put(inputTypeField.getName(), inputTypeField.getDefaultValue());
        } else {
            assertNonNullInputField(inputTypeField);
        }
    }
    return result;
}
Also used : VariableReference(graphql.language.VariableReference) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) ObjectField(graphql.language.ObjectField) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) Value(graphql.language.Value) ArrayValue(graphql.language.ArrayValue) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with VariableReference

use of graphql.language.VariableReference in project engine by craftercms.

the class ContentTypeBasedDataFetcher method addFieldFilterFromObjectField.

protected void addFieldFilterFromObjectField(String path, ObjectField filter, BoolQueryBuilder query, DataFetchingEnvironment env) {
    boolean isVariable = filter.getValue() instanceof VariableReference;
    switch(filter.getName()) {
        case ARG_NAME_NOT:
            BoolQueryBuilder notQuery = boolQuery();
            if (isVariable) {
                ((List<Map<String, Object>>) env.getVariables().get(((VariableReference) filter.getValue()).getName())).forEach(notFilter -> notFilter.entrySet().forEach(entry -> addFieldFilterFromMapEntry(path, entry, notQuery, env)));
            } else {
                ((ArrayValue) filter.getValue()).getValues().forEach(notFilter -> ((ObjectValue) notFilter).getObjectFields().forEach(notField -> addFieldFilterFromObjectField(path, notField, notQuery, env)));
            }
            if (!notQuery.filter().isEmpty()) {
                notQuery.filter().forEach(query::mustNot);
            }
            break;
        case ARG_NAME_AND:
            if (isVariable) {
                ((List<Map<String, Object>>) env.getVariables().get(((VariableReference) filter.getValue()).getName())).forEach(andFilter -> andFilter.entrySet().forEach(entry -> addFieldFilterFromMapEntry(path, entry, query, env)));
            } else {
                ((ArrayValue) filter.getValue()).getValues().forEach(andFilter -> ((ObjectValue) andFilter).getObjectFields().forEach(andField -> addFieldFilterFromObjectField(path, andField, query, env)));
            }
            break;
        case ARG_NAME_OR:
            BoolQueryBuilder tempQuery = boolQuery();
            if (isVariable) {
                ((List<Map<String, Object>>) env.getVariables().get(((VariableReference) filter.getValue()).getName())).forEach(orFilter -> orFilter.entrySet().forEach(entry -> addFieldFilterFromMapEntry(path, entry, tempQuery, env)));
            } else {
                ((ArrayValue) filter.getValue()).getValues().forEach(orFilter -> ((ObjectValue) orFilter).getObjectFields().forEach(orField -> addFieldFilterFromObjectField(path, orField, tempQuery, env)));
            }
            if (!tempQuery.filter().isEmpty()) {
                BoolQueryBuilder orQuery = boolQuery();
                tempQuery.filter().forEach(orQuery::should);
                query.filter(boolQuery().must(orQuery));
            }
            break;
        default:
            QueryBuilder builder = getFilterQueryFromObjectField(path, filter, env);
            if (builder != null) {
                query.filter(builder);
            }
    }
}
Also used : ObjectValue(graphql.language.ObjectValue) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) LoggerFactory(org.slf4j.LoggerFactory) FragmentSpread(graphql.language.FragmentSpread) HashMap(java.util.HashMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) StringUtils(org.apache.commons.lang3.StringUtils) ElasticsearchWrapper(org.craftercms.search.elasticsearch.ElasticsearchWrapper) LinkedHashMap(java.util.LinkedHashMap) Selection(graphql.language.Selection) VariableReference(graphql.language.VariableReference) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) DataFetcher(graphql.schema.DataFetcher) LinkedList(java.util.LinkedList) SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) MapUtils(org.apache.commons.collections.MapUtils) ObjectField(graphql.language.ObjectField) StopWatch(org.springframework.util.StopWatch) Field(graphql.language.Field) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Argument(graphql.language.Argument) List(java.util.List) StringValue(graphql.language.StringValue) ArrayValue(graphql.language.ArrayValue) IntValue(graphql.language.IntValue) SortOrder(org.elasticsearch.search.sort.SortOrder) Optional(java.util.Optional) FragmentDefinition(graphql.language.FragmentDefinition) Required(org.springframework.beans.factory.annotation.Required) InlineFragment(graphql.language.InlineFragment) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collections(java.util.Collections) SchemaUtils(org.craftercms.engine.graphql.SchemaUtils) BooleanValue(graphql.language.BooleanValue) ObjectValue(graphql.language.ObjectValue) VariableReference(graphql.language.VariableReference) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) LinkedList(java.util.LinkedList) List(java.util.List) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 4 with VariableReference

use of graphql.language.VariableReference in project engine by craftercms.

the class ContentTypeBasedDataFetcher method processSelection.

/**
 * Adds the required filters to the ES query for the given field
 */
protected void processSelection(String path, Selection currentSelection, BoolQueryBuilder query, List<String> queryFieldIncludes, DataFetchingEnvironment env) {
    if (currentSelection instanceof Field) {
        // If the current selection is a field
        Field currentField = (Field) currentSelection;
        // Get the original field name
        String propertyName = getOriginalName(currentField.getName());
        // Build the ES-friendly path
        String fullPath = StringUtils.isEmpty(path) ? propertyName : path + "." + propertyName;
        // If the field has sub selection
        if (Objects.nonNull(currentField.getSelectionSet())) {
            // If the field is a flattened component
            if (fullPath.matches(COMPONENT_INCLUDE_REGEX)) {
                // Include the 'content-type' field to make sure the type can be resolved during runtime
                String contentTypeFieldPath = fullPath + "." + QUERY_FIELD_NAME_CONTENT_TYPE;
                if (!queryFieldIncludes.contains(contentTypeFieldPath)) {
                    queryFieldIncludes.add(contentTypeFieldPath);
                }
            }
            // Process recursively and finish
            currentField.getSelectionSet().getSelections().forEach(selection -> processSelection(fullPath, selection, query, queryFieldIncludes, env));
            return;
        }
        // Add the field to the list
        logger.debug("Adding selected field '{}' to query", fullPath);
        queryFieldIncludes.add(fullPath);
        // Check the filters to build the ES query
        Optional<Argument> arg = currentField.getArguments().stream().filter(a -> a.getName().equals(FILTER_NAME)).findFirst();
        if (arg.isPresent()) {
            logger.debug("Adding filters for field {}", fullPath);
            Value<?> argValue = arg.get().getValue();
            if (argValue instanceof ObjectValue) {
                List<ObjectField> filters = ((ObjectValue) argValue).getObjectFields();
                filters.forEach((filter) -> addFieldFilterFromObjectField(fullPath, filter, query, env));
            } else if (argValue instanceof VariableReference && env.getVariables().containsKey(((VariableReference) argValue).getName())) {
                Map<String, Object> map = (Map<String, Object>) env.getVariables().get(((VariableReference) argValue).getName());
                map.entrySet().forEach(filter -> addFieldFilterFromMapEntry(fullPath, filter, query, env));
            }
        }
    } else if (currentSelection instanceof InlineFragment) {
        // If the current selection is an inline fragment, process recursively
        InlineFragment fragment = (InlineFragment) currentSelection;
        fragment.getSelectionSet().getSelections().forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
    } else if (currentSelection instanceof FragmentSpread) {
        // If the current selection is a fragment spread, find the fragment and process recursively
        FragmentSpread fragmentSpread = (FragmentSpread) currentSelection;
        FragmentDefinition fragmentDefinition = env.getFragmentsByName().get(fragmentSpread.getName());
        fragmentDefinition.getSelectionSet().getSelections().forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
    }
}
Also used : ObjectValue(graphql.language.ObjectValue) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) LoggerFactory(org.slf4j.LoggerFactory) FragmentSpread(graphql.language.FragmentSpread) HashMap(java.util.HashMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) StringUtils(org.apache.commons.lang3.StringUtils) ElasticsearchWrapper(org.craftercms.search.elasticsearch.ElasticsearchWrapper) LinkedHashMap(java.util.LinkedHashMap) Selection(graphql.language.Selection) VariableReference(graphql.language.VariableReference) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) DataFetcher(graphql.schema.DataFetcher) LinkedList(java.util.LinkedList) SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) MapUtils(org.apache.commons.collections.MapUtils) ObjectField(graphql.language.ObjectField) StopWatch(org.springframework.util.StopWatch) Field(graphql.language.Field) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Argument(graphql.language.Argument) List(java.util.List) StringValue(graphql.language.StringValue) ArrayValue(graphql.language.ArrayValue) IntValue(graphql.language.IntValue) SortOrder(org.elasticsearch.search.sort.SortOrder) Optional(java.util.Optional) FragmentDefinition(graphql.language.FragmentDefinition) Required(org.springframework.beans.factory.annotation.Required) InlineFragment(graphql.language.InlineFragment) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collections(java.util.Collections) SchemaUtils(org.craftercms.engine.graphql.SchemaUtils) BooleanValue(graphql.language.BooleanValue) Argument(graphql.language.Argument) VariableReference(graphql.language.VariableReference) FragmentDefinition(graphql.language.FragmentDefinition) FragmentSpread(graphql.language.FragmentSpread) ObjectField(graphql.language.ObjectField) Field(graphql.language.Field) ObjectValue(graphql.language.ObjectValue) ObjectField(graphql.language.ObjectField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) InlineFragment(graphql.language.InlineFragment)

Aggregations

ArrayValue (graphql.language.ArrayValue)4 ObjectField (graphql.language.ObjectField)4 ObjectValue (graphql.language.ObjectValue)4 VariableReference (graphql.language.VariableReference)4 BooleanValue (graphql.language.BooleanValue)3 FloatValue (graphql.language.FloatValue)3 IntValue (graphql.language.IntValue)3 StringValue (graphql.language.StringValue)3 Value (graphql.language.Value)3 LinkedHashMap (java.util.LinkedHashMap)3 Argument (graphql.language.Argument)2 Field (graphql.language.Field)2 FragmentDefinition (graphql.language.FragmentDefinition)2 FragmentSpread (graphql.language.FragmentSpread)2 InlineFragment (graphql.language.InlineFragment)2 Selection (graphql.language.Selection)2 DataFetcher (graphql.schema.DataFetcher)2 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2