Пример #1
0
        public IReadOnlyDictionary <NameString, ArgumentValue> CoerceArguments(
            IVariableValueCollection 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, ArgumentVariableValue> var in _vars)
            {
                IError error = null;

                if (variables.TryGetVariable(
                        var.Value.VariableName,
                        out object value))
                {
                    value = var.Value.CoerceValue(value);
                }
                else
                {
                    value = var.Value.DefaultValue;

                    if (var.Value.Type.NamedType().IsLeafType() &&
                        value is IValueNode literal)
                    {
                        value = var.Value.Type.ParseLiteral(literal);
                    }

                    if (var.Value.Type.IsNonNullType() &&
                        (value is null || value is NullValueNode))
                    {
                        error = ErrorBuilder.New()
                                .SetMessage(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();
                    }
                }

                if (error is null)
                {
                    ValueKind kind = ValueKind.Unknown;

                    if (value is IValueNode literal)
                    {
                        kind          = literal.GetValueKind();
                        args[var.Key] = new ArgumentValue(var.Value.Argument, kind, literal);
                    }
                    else
                    {
                        Scalars.TryGetKind(value, out kind);
                        args[var.Key] = new ArgumentValue(var.Value.Argument, kind, value);
                    }
                }
                else
                {
                    throw new QueryException(error);
                }
            }

            return(args);
        }