Example #1
0
        private static ArgumentValue CreateArgumentValue(
            InputField argument,
            IDictionary <string, IValueNode> argumentValues,
            IVariableCollection variables,
            Func <string, IError> createError)
        {
            object argumentValue;

            try
            {
                argumentValue = CoerceArgumentValue(
                    argument, variables, argumentValues);
            }
            catch (ScalarSerializationException ex)
            {
                throw new QueryException(createError(ex.Message));
            }

            if (argument.Type is NonNullType && argumentValue == null)
            {
                throw new QueryException(createError(string.Format(
                                                         CultureInfo.InvariantCulture,
                                                         TypeResources.ArgumentValueBuilder_NonNull,
                                                         argument.Name,
                                                         TypeVisualizer.Visualize(argument.Type))));
            }

            InputTypeNonNullCheck.CheckForNullValueViolation(
                argument.Type, argumentValue, createError);

            return(new ArgumentValue(argument.Type, argumentValue));
        }
        private Variable CoerceVariableValue(
            VariableDefinitionNode variableDefinition,
            IReadOnlyDictionary <string, object> variableValues,
            Variable variable)
        {
            var value = variableValues.TryGetValue(
                variable.Name, out var rawValue)
                ? Normalize(variableDefinition, variable, rawValue)
                : variable.DefaultValue;

            variable = variable.WithValue(value);

            if (variable.Type.IsNonNullType() && variable.Value is null)
            {
                throw new QueryException(ErrorBuilder.New()
                                         .SetMessage(string.Format(
                                                         CultureInfo.InvariantCulture,
                                                         TypeResources.VariableValueBuilder_NonNull,
                                                         variable.Name,
                                                         TypeVisualizer.Visualize(variable.Type)))
                                         .AddLocation(variableDefinition)
                                         .Build());
            }

            InputTypeNonNullCheck.CheckForNullValueViolation(
                variable.Type, variable.Value, _converter,
                message => ErrorBuilder.New()
                .SetMessage(message)
                .AddLocation(variableDefinition)
                .Build());
            CheckForInvalidValueType(variableDefinition, variable);

            return(variable);
        }
Example #3
0
        public IReadOnlyDictionary <NameString, ArgumentValue> CoerceArguments(
            IVariableCollection variables,
            ITypeConversion converter)
        {
            if (_hasArgumentErrors)
            {
                throw new QueryException(_args.Values.Select(t => t.Error));
            }

            if (_vars == null)
            {
                return(_args);
            }

            var args = _args.ToDictionary(t => t.Key, t => t.Value);

            foreach (KeyValuePair <NameString, VariableValue> var in _vars)
            {
                if (!variables.TryGetVariable(
                        var.Value.VariableName,
                        out object value))
                {
                    value = var.Value.DefaultValue;
                }

                IError error = InputTypeNonNullCheck.CheckForNullValueViolation(
                    var.Key,
                    var.Value.Type,
                    value,
                    converter,
                    message => ErrorBuilder.New()
                    .SetMessage(message)
                    .SetPath(_path.AppendOrCreate(ResponseName))
                    .AddLocation(Selection)
                    .SetExtension("argument", var.Key)
                    .Build());

                if (error is null)
                {
                    args[var.Key] = new ArgumentValue(var.Value.Type, value);
                }
                else
                {
                    throw new QueryException(error);
                }
            }

            return(args);
        }
Example #4
0
        private void CreateArgumentValue(
            FieldInfo fieldInfo,
            IInputField argument,
            IValueNode literal)
        {
            if (fieldInfo.Arguments == null)
            {
                fieldInfo.Arguments =
                    new Dictionary <NameString, ArgumentValue>();
            }

            object value = ParseLiteral(argument.Type, literal);

            fieldInfo.Arguments[argument.Name] = new ArgumentValue(
                argument.Type,
                value);

            IError error = InputTypeNonNullCheck.CheckForNullValueViolation(
                argument.Name,
                argument.Type,
                value,
                _converter,
                message => ErrorBuilder.New()
                .SetMessage(message)
                .AddLocation(fieldInfo.Selection)
                .SetExtension(_argumentProperty, argument.Name)
                .SetPath(fieldInfo.Path.AppendOrCreate(
                             fieldInfo.ResponseName))
                .Build());

            if (error != null)
            {
                fieldInfo.Arguments[argument.Name] =
                    new ArgumentValue(
                        argument.Type,
                        error);
            }
        }
Example #5
0
        public IReadOnlyDictionary <NameString, ArgumentValue> CoerceArguments(
            IVariableCollection variables,
            ITypeConversion converter)
        {
            if (_hasArgumentErrors)
            {
                throw new QueryException(_args.Values.Select(t => t.Error));
            }

            if (_vars == null)
            {
                return(_args);
            }

            var args = _args.ToDictionary(t => t.Key, t => t.Value);

            foreach (KeyValuePair <NameString, VariableValue> var in _vars)
            {
                IError error = null;

                if (!variables.TryGetVariable(
                        var.Value.VariableName,
                        out object value))
                {
                    value = var.Value.DefaultValue is IValueNode literal
                        ? var.Value.Type.ParseLiteral(literal)
                        : value = var.Value.DefaultValue;

                    if (var.Value.Type.IsNonNullType() && value is null)
                    {
                        error = ErrorBuilder.New()
                                .SetMessage(string.Format(string.Format(
                                                              CultureInfo.InvariantCulture,
                                                              TypeResources.ArgumentValueBuilder_NonNull,
                                                              var.Key,
                                                              TypeVisualizer.Visualize(var.Value.Type))))
                                .AddLocation(Selection)
                                .SetExtension(_argumentProperty, Path.New(var.Key))
                                .SetPath(_path)
                                .Build();
                    }
                }
                else
                {
                    error = InputTypeNonNullCheck.CheckForNullValueViolation(
                        var.Key,
                        var.Value.Type,
                        value,
                        converter,
                        message => ErrorBuilder.New()
                        .SetMessage(message)
                        .SetPath(_path.AppendOrCreate(ResponseName))
                        .AddLocation(Selection)
                        .SetExtension("argument", var.Key)
                        .Build());
                }

                if (error is null)
                {
                    args[var.Key] = new ArgumentValue(var.Value.Type, value);
                }
                else
                {
                    throw new QueryException(error);
                }
            }

            return(args);
        }