Exemple #1
0
        public Dictionary <string, ArgumentValue> CoerceArgumentValues(
            ObjectType objectType,
            FieldSelection fieldSelection,
            VariableCollection variables)
        {
            Dictionary <string, ArgumentValue> coercedArgumentValues =
                new Dictionary <string, ArgumentValue>();

            Dictionary <string, IValueNode> argumentValues =
                fieldSelection.Node.Arguments
                .Where(t => t.Value != null)
                .ToDictionary(t => t.Name.Value, t => t.Value);

            foreach (InputField argument in fieldSelection.Field.Arguments.Values)
            {
                string     argumentName  = argument.Name;
                IInputType argumentType  = argument.Type;
                IValueNode defaultValue  = argument.DefaultValue;
                object     argumentValue = CoerceArgumentValue(
                    argumentName, argumentType, defaultValue,
                    variables, argumentValues);

                if (argumentType is NonNullType && argumentValue == null)
                {
                    throw new QueryException(new ArgumentError(
                                                 $"The argument type of '{argumentName}' is a non-null type.",
                                                 argumentName, fieldSelection.Node));
                }

                coercedArgumentValues[argumentName] = new ArgumentValue(
                    argumentType, argumentType.NativeType, argumentValue);
            }

            return(coercedArgumentValues);
        }
        private T ConvertArgumentValue <T>(string name, ArgumentValue argumentValue)
        {
            if (argumentValue.Value is T value)
            {
                return(value);
            }

            Type type = typeof(T);

            if (argumentValue.Value == null)
            {
                return(default(T));
            }

            if (TryConvertValue(argumentValue, out value))
            {
                return(value);
            }

            throw new QueryException(
                      new FieldError(
                          $"Could not convert argument {name} from " +
                          $"{argumentValue.NativeType.FullName} to " +
                          $"{typeof(T).FullName}.",
                          _resolverTask.FieldSelection.Node));
        }
 public ArgumentValueSnapshot(
     string name,
     ArgumentValue argumentValue)
 {
     Name  = name;
     Type  = TypeVisualizer.Visualize(argumentValue.Type);
     Value = argumentValue.Value;
 }
Exemple #4
0
        private T ConvertArgumentValue <T>(string name, ArgumentValue argumentValue)
        {
            if (argumentValue.Value is T value)
            {
                return(value);
            }

            if (argumentValue.Value == null)
            {
                return(default);
        private bool TryConvertValue <T>(ArgumentValue argumentValue, out T value)
        {
            foreach (IInputValueConverter converter in _converters
                     .Where(t => t.CanConvert(argumentValue.Type)))
            {
                if (converter.TryConvert(argumentValue.NativeType, typeof(T),
                                         argumentValue.Value, out object cv))
                {
                    value = (T)cv;
                    return(true);
                }
            }

            value = default(T);
            return(false);
        }