Search in sources :

Example 1 with ArrayValue

use of graphql.language.ArrayValue 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 ArrayValue

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

the class GraphqlAntlrToLanguage method getValue.

private Value getValue(GraphqlParser.ValueContext 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.arrayValue() != null) {
        ArrayValue arrayValue = new ArrayValue();
        newNode(arrayValue, ctx);
        for (GraphqlParser.ValueContext valueWithVariableContext : ctx.arrayValue().value()) {
            arrayValue.getValues().add(getValue(valueWithVariableContext));
        }
        return arrayValue;
    } else if (ctx.objectValue() != null) {
        ObjectValue objectValue = new ObjectValue();
        newNode(objectValue, ctx);
        for (GraphqlParser.ObjectFieldContext objectFieldContext : ctx.objectValue().objectField()) {
            ObjectField objectField = new ObjectField(objectFieldContext.name().getText(), getValue(objectFieldContext.value()));
            objectValue.getObjectFields().add(objectField);
        }
        return objectValue;
    }
    return Assert.assertShouldNeverHappen();
}
Also used : 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 3 with ArrayValue

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

the class ValuesResolver method coerceValueAstForList.

private Object coerceValueAstForList(GraphqlFieldVisibility fieldVisibility, GraphQLList graphQLList, Value value, Map<String, Object> variables) {
    if (value instanceof ArrayValue) {
        ArrayValue arrayValue = (ArrayValue) value;
        List<Object> result = new ArrayList<>();
        for (Value singleValue : arrayValue.getValues()) {
            result.add(coerceValueAst(fieldVisibility, graphQLList.getWrappedType(), singleValue, variables));
        }
        return result;
    } else {
        return Collections.singletonList(coerceValueAst(fieldVisibility, graphQLList.getWrappedType(), value, variables));
    }
}
Also used : ArrayList(java.util.ArrayList) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) Value(graphql.language.Value) ArrayValue(graphql.language.ArrayValue) ArrayValue(graphql.language.ArrayValue)

Example 4 with ArrayValue

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

the class SchemaGeneratorHelper method buildValue.

public Object buildValue(Value value, GraphQLType requiredType) {
    Object result = null;
    if (requiredType instanceof GraphQLNonNull) {
        requiredType = ((GraphQLNonNull) requiredType).getWrappedType();
    }
    if (requiredType instanceof GraphQLScalarType) {
        result = parseLiteral(value, (GraphQLScalarType) requiredType);
    } else if (value instanceof EnumValue && requiredType instanceof GraphQLEnumType) {
        result = ((EnumValue) value).getName();
    } else if (value instanceof ArrayValue && requiredType instanceof GraphQLList) {
        ArrayValue arrayValue = (ArrayValue) value;
        GraphQLType wrappedType = ((GraphQLList) requiredType).getWrappedType();
        result = arrayValue.getValues().stream().map(item -> this.buildValue(item, wrappedType)).collect(Collectors.toList());
    } else if (value instanceof ObjectValue && requiredType instanceof GraphQLInputObjectType) {
        result = buildObjectValue((ObjectValue) value, (GraphQLInputObjectType) requiredType);
    } else if (value != null && !(value instanceof NullValue)) {
        Assert.assertShouldNeverHappen("cannot build value of %s from %s", requiredType.getName(), String.valueOf(value));
    }
    return result;
}
Also used : GraphQLList(graphql.schema.GraphQLList) ObjectValue(graphql.language.ObjectValue) NullValue(graphql.language.NullValue) GraphQLEnumType(graphql.schema.GraphQLEnumType) EnumValue(graphql.language.EnumValue) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLType(graphql.schema.GraphQLType) ArrayValue(graphql.language.ArrayValue) GraphQLScalarType(graphql.schema.GraphQLScalarType)

Example 5 with ArrayValue

use of graphql.language.ArrayValue 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)

Aggregations

ArrayValue (graphql.language.ArrayValue)5 ObjectValue (graphql.language.ObjectValue)5 BooleanValue (graphql.language.BooleanValue)3 EnumValue (graphql.language.EnumValue)3 FloatValue (graphql.language.FloatValue)3 IntValue (graphql.language.IntValue)3 ObjectField (graphql.language.ObjectField)3 StringValue (graphql.language.StringValue)3 NullValue (graphql.language.NullValue)2 Value (graphql.language.Value)2 VariableReference (graphql.language.VariableReference)2 GraphqlParser (graphql.parser.antlr.GraphqlParser)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 Argument (graphql.language.Argument)1 Field (graphql.language.Field)1 FragmentDefinition (graphql.language.FragmentDefinition)1 FragmentSpread (graphql.language.FragmentSpread)1 InlineFragment (graphql.language.InlineFragment)1 Selection (graphql.language.Selection)1