Search in sources :

Example 1 with ObjectField

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

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

use of graphql.language.ObjectField 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 4 with ObjectField

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

the class ValidationUtil method isValidLiteralValue.

private boolean isValidLiteralValue(Value value, GraphQLInputObjectType type, GraphQLSchema schema) {
    if (!(value instanceof ObjectValue)) {
        handleNotObjectError(value, type);
        return false;
    }
    GraphqlFieldVisibility fieldVisibility = schema.getFieldVisibility();
    ObjectValue objectValue = (ObjectValue) value;
    Map<String, ObjectField> objectFieldMap = fieldMap(objectValue);
    Set<String> missingFields = getMissingFields(type, objectFieldMap, fieldVisibility);
    if (!missingFields.isEmpty()) {
        handleMissingFieldsError(value, type, missingFields);
        return false;
    }
    for (ObjectField objectField : objectValue.getObjectFields()) {
        GraphQLInputObjectField inputObjectField = fieldVisibility.getFieldDefinition(type, objectField.getName());
        if (inputObjectField == null) {
            handleExtraFieldError(value, type, objectField);
            return false;
        }
        if (!isValidLiteralValue(objectField.getValue(), inputObjectField.getType(), schema)) {
            handleFieldNotValidError(objectField, type);
            return false;
        }
    }
    return true;
}
Also used : GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) ObjectValue(graphql.language.ObjectValue) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) ObjectField(graphql.language.ObjectField)

Aggregations

ObjectField (graphql.language.ObjectField)4 ObjectValue (graphql.language.ObjectValue)4 ArrayValue (graphql.language.ArrayValue)3 BooleanValue (graphql.language.BooleanValue)2 EnumValue (graphql.language.EnumValue)2 FloatValue (graphql.language.FloatValue)2 IntValue (graphql.language.IntValue)2 StringValue (graphql.language.StringValue)2 VariableReference (graphql.language.VariableReference)2 GraphqlParser (graphql.parser.antlr.GraphqlParser)2 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 NullValue (graphql.language.NullValue)1 Value (graphql.language.Value)1 GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)1 LinkedHashMap (java.util.LinkedHashMap)1