Search in sources :

Example 1 with NormalizedInputValue

use of graphql.normalized.NormalizedInputValue in project graphql-java by graphql-java.

the class ValuesResolver method literalToNormalizedValueForInputObject.

private Object literalToNormalizedValueForInputObject(GraphqlFieldVisibility fieldVisibility, GraphQLInputObjectType type, ObjectValue inputObjectLiteral, Map<String, NormalizedInputValue> normalizedVariables) {
    Map<String, Object> result = new LinkedHashMap<>();
    for (ObjectField field : inputObjectLiteral.getObjectFields()) {
        // If a variable doesn't exist then we can't put it into the result Map
        if (isVariableAbsent(field.getValue(), normalizedVariables)) {
            continue;
        }
        GraphQLInputType fieldType = type.getField(field.getName()).getType();
        Object fieldValue = literalToNormalizedValue(fieldVisibility, fieldType, field.getValue(), normalizedVariables);
        result.put(field.getName(), new NormalizedInputValue(simplePrint(fieldType), fieldValue));
    }
    return result;
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) ObjectField(graphql.language.ObjectField) ObjectField.newObjectField(graphql.language.ObjectField.newObjectField) NormalizedInputValue(graphql.normalized.NormalizedInputValue) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with NormalizedInputValue

use of graphql.normalized.NormalizedInputValue in project graphql-java by graphql-java.

the class ValuesResolver method getNormalizedArgumentValues.

/**
 * No validation as the arguments are assumed valid
 *
 * @param argumentTypes       the list of argument types
 * @param arguments           the AST arguments
 * @param normalizedVariables the normalised variables
 *
 * @return a map of named normalised values
 */
public Map<String, NormalizedInputValue> getNormalizedArgumentValues(List<GraphQLArgument> argumentTypes, List<Argument> arguments, Map<String, NormalizedInputValue> normalizedVariables) {
    if (argumentTypes.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String, NormalizedInputValue> result = new LinkedHashMap<>();
    Map<String, Argument> argumentMap = argumentMap(arguments);
    for (GraphQLArgument argumentDefinition : argumentTypes) {
        String argumentName = argumentDefinition.getName();
        Argument argument = argumentMap.get(argumentName);
        if (argument == null) {
            continue;
        }
        // If a variable doesn't exist then we can't put it into the result Map
        if (isVariableAbsent(argument.getValue(), normalizedVariables)) {
            continue;
        }
        GraphQLInputType argumentType = argumentDefinition.getType();
        Object value = literalToNormalizedValue(DEFAULT_FIELD_VISIBILITY, argumentType, argument.getValue(), normalizedVariables);
        result.put(argumentName, new NormalizedInputValue(simplePrint(argumentType), value));
    }
    return result;
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLArgument(graphql.schema.GraphQLArgument) Argument(graphql.language.Argument) NormalizedInputValue(graphql.normalized.NormalizedInputValue) GraphQLArgument(graphql.schema.GraphQLArgument) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with NormalizedInputValue

use of graphql.normalized.NormalizedInputValue in project graphql-java by graphql-java.

the class ValuesResolver method getNormalizedVariableValues.

/**
 * Normalized variables values are Literals with type information. No validation here!
 *
 * @param schema              the schema to use
 * @param variableDefinitions the list of variable definitions
 * @param rawVariables        the raw variables
 *
 * @return a map of the normalised values
 */
public Map<String, NormalizedInputValue> getNormalizedVariableValues(GraphQLSchema schema, List<VariableDefinition> variableDefinitions, Map<String, Object> rawVariables) {
    GraphqlFieldVisibility fieldVisibility = schema.getCodeRegistry().getFieldVisibility();
    Map<String, NormalizedInputValue> result = new LinkedHashMap<>();
    for (VariableDefinition variableDefinition : variableDefinitions) {
        String variableName = variableDefinition.getName();
        GraphQLType variableType = TypeFromAST.getTypeFromAST(schema, variableDefinition.getType());
        assertTrue(variableType instanceof GraphQLInputType);
        // can be NullValue
        Value defaultValue = variableDefinition.getDefaultValue();
        boolean hasValue = rawVariables.containsKey(variableName);
        Object value = rawVariables.get(variableName);
        if (!hasValue && defaultValue != null) {
            result.put(variableName, new NormalizedInputValue(simplePrint(variableType), defaultValue));
        } else if (isNonNull(variableType) && (!hasValue || value == null)) {
            return assertShouldNeverHappen("variable values are expected to be valid");
        } else if (hasValue) {
            if (value == null) {
                result.put(variableName, new NormalizedInputValue(simplePrint(variableType), null));
            } else {
                Object literal = externalValueToLiteral(fieldVisibility, value, (GraphQLInputType) variableType, NORMALIZED);
                result.put(variableName, new NormalizedInputValue(simplePrint(variableType), literal));
            }
        }
    }
    return result;
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) DefaultGraphqlFieldVisibility(graphql.schema.visibility.DefaultGraphqlFieldVisibility) VariableDefinition(graphql.language.VariableDefinition) NormalizedInputValue(graphql.normalized.NormalizedInputValue) GraphQLType(graphql.schema.GraphQLType) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) NormalizedInputValue(graphql.normalized.NormalizedInputValue) ArrayValue(graphql.language.ArrayValue) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) EnumValue(graphql.language.EnumValue) NullValue.newNullValue(graphql.language.NullValue.newNullValue) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with NormalizedInputValue

use of graphql.normalized.NormalizedInputValue in project graphql-java by graphql-java.

the class ValuesResolver method externalValueToLiteralForObject.

/**
 * No validation
 */
@SuppressWarnings("unchecked")
private Object externalValueToLiteralForObject(GraphqlFieldVisibility fieldVisibility, GraphQLInputObjectType inputObjectType, Object inputValue, ValueMode valueMode) {
    assertTrue(inputValue instanceof Map, () -> "Expect Map as input");
    Map<String, Object> inputMap = (Map<String, Object>) inputValue;
    List<GraphQLInputObjectField> fieldDefinitions = fieldVisibility.getFieldDefinitions(inputObjectType);
    Map<String, Object> normalizedResult = new LinkedHashMap<>();
    ImmutableList.Builder<ObjectField> objectFields = ImmutableList.builder();
    for (GraphQLInputObjectField inputFieldDefinition : fieldDefinitions) {
        GraphQLInputType fieldType = inputFieldDefinition.getType();
        String fieldName = inputFieldDefinition.getName();
        boolean hasValue = inputMap.containsKey(fieldName);
        Object fieldValue = inputMap.getOrDefault(fieldName, null);
        if (!hasValue && inputFieldDefinition.hasSetDefaultValue()) {
            // TODO: consider valueMode
            Object defaultValueLiteral = valueToLiteral(fieldVisibility, inputFieldDefinition.getInputFieldDefaultValue(), fieldType);
            if (valueMode == ValueMode.LITERAL) {
                normalizedResult.put(fieldName, new NormalizedInputValue(simplePrint(fieldType), defaultValueLiteral));
            } else {
                objectFields.add(newObjectField().name(fieldName).value((Value) defaultValueLiteral).build());
            }
        } else if (hasValue) {
            if (fieldValue == null) {
                if (valueMode == NORMALIZED) {
                    normalizedResult.put(fieldName, new NormalizedInputValue(simplePrint(fieldType), null));
                } else {
                    objectFields.add(newObjectField().name(fieldName).value(newNullValue().build()).build());
                }
            } else {
                Object literal = externalValueToLiteral(fieldVisibility, fieldValue, fieldType, valueMode);
                if (valueMode == NORMALIZED) {
                    normalizedResult.put(fieldName, new NormalizedInputValue(simplePrint(fieldType), literal));
                } else {
                    objectFields.add(newObjectField().name(fieldName).value((Value) literal).build());
                }
            }
        }
    }
    if (valueMode == NORMALIZED) {
        return normalizedResult;
    }
    return ObjectValue.newObjectValue().objectFields(objectFields.build()).build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) NormalizedInputValue(graphql.normalized.NormalizedInputValue) LinkedHashMap(java.util.LinkedHashMap) GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) ObjectField(graphql.language.ObjectField) ObjectField.newObjectField(graphql.language.ObjectField.newObjectField) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) NormalizedInputValue(graphql.normalized.NormalizedInputValue) ArrayValue(graphql.language.ArrayValue) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) EnumValue(graphql.language.EnumValue) NullValue.newNullValue(graphql.language.NullValue.newNullValue) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ImmutableKit.emptyMap(graphql.collect.ImmutableKit.emptyMap)

Aggregations

NormalizedInputValue (graphql.normalized.NormalizedInputValue)4 GraphQLInputType (graphql.schema.GraphQLInputType)4 LinkedHashMap (java.util.LinkedHashMap)4 ArrayValue (graphql.language.ArrayValue)2 BooleanValue (graphql.language.BooleanValue)2 EnumValue (graphql.language.EnumValue)2 FloatValue (graphql.language.FloatValue)2 IntValue (graphql.language.IntValue)2 NullValue (graphql.language.NullValue)2 NullValue.newNullValue (graphql.language.NullValue.newNullValue)2 ObjectField (graphql.language.ObjectField)2 ObjectField.newObjectField (graphql.language.ObjectField.newObjectField)2 ObjectValue (graphql.language.ObjectValue)2 StringValue (graphql.language.StringValue)2 Value (graphql.language.Value)2 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableKit.emptyMap (graphql.collect.ImmutableKit.emptyMap)1 Argument (graphql.language.Argument)1 VariableDefinition (graphql.language.VariableDefinition)1