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;
}
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;
}
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;
}
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();
}
Aggregations