private static bool GetIfArgumentValue( ISchema schema, Directive directive, IReadOnlyDictionary <string, object?> coercedVariableValues, Language.Nodes.Argument argument) { if (directive == null) { throw new ArgumentNullException(nameof(directive)); } if (coercedVariableValues == null) { throw new ArgumentNullException(nameof(coercedVariableValues)); } switch (argument.Value.Kind) { case NodeKind.BooleanValue: return((bool)Values.CoerceValue(schema.GetInputFields, schema.GetValueConverter, (BooleanValue)argument.Value, ScalarType.NonNullBoolean)); case NodeKind.Variable: var variable = (Variable)argument.Value; var variableValue = coercedVariableValues[variable.Name]; if (variableValue == null) { throw new QueryExecutionException( $"If argument of {directive} is null. Variable value should not be null", new NodePath(), argument); } return((bool)variableValue); default: return(false); } }
public static IReadOnlyDictionary <string, object?> CoerceVariableValues( ISchema schema, OperationDefinition operation, Dictionary <string, object> variableValues) { var coercedValues = new Dictionary <string, object?>(); var variableDefinitions = operation.VariableDefinitions; if (variableDefinitions == null) { return(coercedValues); } foreach (var variableDefinition in variableDefinitions) { var variableName = variableDefinition.Variable.Name; var variableType = Ast.TypeFromAst(schema, variableDefinition.Type); // should be assert? if (!TypeIs.IsInputType(variableType)) { throw new VariableException($"Variable is not of input type", variableName, variableType); } var defaultValue = variableDefinition.DefaultValue; var hasValue = variableValues.ContainsKey(variableName); var value = hasValue ? variableValues[variableName]: null; if (!hasValue && defaultValue != null) { coercedValues[variableName] = defaultValue; } if (variableType is NonNull && (!hasValue || value == null)) { throw new ValueCoercionException( $"Variable {variableName} type is non-nullable but value is null or not set", value, variableType); } if (hasValue) { if (value == null) { coercedValues[variableName] = null; } else { coercedValues[variableName] = Values.CoerceValue( schema.GetInputFields, schema.GetValueConverter, value, variableType); } } } return(coercedValues); }
public static object?CoerceArgumentValue( ISchema schema, IReadOnlyDictionary <string, object> coercedVariableValues, string argumentName, Argument argumentDefinition, Language.Nodes.Argument argument) { var argumentType = argumentDefinition.Type; var defaultValue = argumentDefinition.DefaultValue; var argumentValue = argument?.Value; var hasValue = argumentValue != null; object?value = null; if (argumentValue is Variable variable) { if (coercedVariableValues == null) { hasValue = false; } else { string variableName = variable.Name; hasValue = coercedVariableValues.ContainsKey(variableName); if (hasValue) { value = coercedVariableValues[variableName]; } } } else { value = argumentValue; } if (argumentType is NonNull && (!hasValue || value == null)) { throw new ValueCoercionException( $"Argument '{argumentName}' is non-null but no value could be coerced", null, argumentType); } if (hasValue) { if (value == null) { return(null); } if (argumentValue is Variable) { return(value); } var coercedValue = Values.CoerceValue( schema.GetInputFields, schema.GetValueConverter, value, argumentType); return(coercedValue); } return(defaultValue); }