Ejemplo n.º 1
0
        private List <GraphQLArgument> MergeArgumentsWithDefault(IEnumerable <GraphQLArgument> arguments,
                                                                 IDictionary <string, GraphQLObjectTypeArgumentInfo> argumentInfos)
        {
            var combinedArguments = arguments.ToDictionary(e => e.Name.Value, e => e);
            var schemaRepository  = this.context.SchemaRepository;

            foreach (var argument in argumentInfos)
            {
                var argumentName  = argument.Key;
                var argumentValue = argument.Value;

                if (!combinedArguments.ContainsKey(argumentName) && argumentValue.DefaultValue.IsSet)
                {
                    var defaultArgument = new GraphQLArgument()
                    {
                        Name = new GraphQLName()
                        {
                            Value = argumentName
                        },
                        Value = argumentValue.DefaultValue.GetAstValue(
                            (GraphQLInputType)argumentValue.GetGraphQLType(schemaRepository), schemaRepository)
                    };
                    combinedArguments.Add(argumentName, defaultArgument);
                }
            }

            return(combinedArguments.Values.ToList());
        }
Ejemplo n.º 2
0
        private GraphQLException ComposeError(GraphQLArgument argument, GraphQLValue astValue, IEnumerable <GraphQLException> innerExceptions)
        {
            var innerMessage     = string.Join("\n", innerExceptions.Select(e => e.Message));
            var exceptionMessage = $"Argument \"{argument.Name.Value}\" has invalid value {astValue}.\n{innerMessage}";

            return(new GraphQLException(exceptionMessage, new[] { argument.Value }));
        }
Ejemplo n.º 3
0
        private static bool GetIfArgumentValue(
            ISchema schema,
            GraphQLDirective directive,
            Dictionary <string, object> coercedVariableValues,
            GraphQLArgument argument)
        {
            if (directive == null)
            {
                throw new ArgumentNullException(nameof(directive));
            }
            if (coercedVariableValues == null)
            {
                throw new ArgumentNullException(nameof(coercedVariableValues));
            }
            if (argument == null)
            {
                throw new GraphQLError(
                          "Directive is missing argument which is required", directive);
            }

            switch (argument.Value)
            {
            case GraphQLScalarValue scalarValue:
                return((bool)Values.CoerceValue(schema.GetInputFields, scalarValue, ScalarType.NonNullBoolean));

            case GraphQLVariable variable:
                var variableValue = coercedVariableValues[variable.Name.Value];
                return((bool)variableValue);

            default:
                return(false);
            }
        }
Ejemplo n.º 4
0
        private string PrintArgument(GraphQLArgument argument)
        {
            var name  = PrintName(argument.Name);
            var value = Print(argument.Value);

            return($"{name}: {value}");
        }
Ejemplo n.º 5
0
        public override GraphQLArgument EndVisitArgument(GraphQLArgument argument)
        {
            {
                Tracker.LeaveArgument?.Invoke(argument);
            }

            return(base.EndVisitArgument(argument));
        }
Ejemplo n.º 6
0
        public override GraphQLArgument BeginVisitArgument(GraphQLArgument argument)
        {
            {
                Tracker.EnterArgument?.Invoke(argument);
            }

            return(base.BeginVisitArgument(argument));
        }
Ejemplo n.º 7
0
        public override GraphQLArgument EndVisitArgument(GraphQLArgument argument)
        {
            var argumentType = this.GetArgumentDefinition();

            this.Errors.AddRange(this.Schema.TypeTranslator.IsValidLiteralValue(argumentType, argument.Value));

            return(base.EndVisitArgument(argument));
        }
        public override GraphQLArgument BeginVisitArgument(GraphQLArgument argument)
        {
            var fieldName = this.fieldStack.Peek();

            this.argumentType = this.translator.GetObjectTypeTranslatorFor(this.typeStack.Peek()).GetField(fieldName).Arguments
                                .SingleOrDefault(e => e.Key == argument.Name.Value).Value;
            this.argumentTypes.Remove(argument.Name.Value);

            return(base.BeginVisitArgument(argument));
        }
Ejemplo n.º 9
0
 public virtual GraphQLArgument BeginVisitArgument(GraphQLArgument argument)
 {
     if (argument.Name != null)
     {
         BeginVisitNode(argument.Name);
     }
     if (argument.Value != null)
     {
         BeginVisitNode(argument.Value);
     }
     return(EndVisitArgument(argument));
 }
Ejemplo n.º 10
0
        public override GraphQLArgument EndVisitArgument(GraphQLArgument argument)
        {
            var argumentType = this.GetLastArgumentType(argument);
            var astValue     = argument.Value;

            var errors = this.LiteralValueValidator.IsValid(argumentType, astValue);

            if (errors.Any())
            {
                this.Errors.Add(this.ComposeError(argument, astValue, errors));
            }

            return(base.EndVisitArgument(argument));
        }
Ejemplo n.º 11
0
        public override GraphQLArgument BeginVisitArgument(GraphQLArgument argument)
        {
            var argumentName = argument.Name.Value;

            if (this.knownArgumentNames.ContainsKey(argumentName))
            {
                this.ReportDuplicateArgumentsError(argumentName, new[] { this.knownArgumentNames[argumentName], argument.Name });
            }
            else
            {
                this.knownArgumentNames.Add(argumentName, argument.Name);
            }

            return(base.BeginVisitArgument(argument));
        }
        private void CheckFieldArgument(GraphQLArgument node, GraphQLFieldInfo fieldInfo, string argumentName)
        {
            if (!fieldInfo.Arguments.ContainsKey(argumentName))
            {
                var parentType = this.GetParentType();

                var errorMessage = this.ComposeUnknownArgumentMessage(
                    argumentName,
                    fieldInfo.Name,
                    parentType,
                    StringUtils.SuggestionList(argumentName, fieldInfo.Arguments.Select(e => e.Value.Name)));

                this.Errors.Add(new GraphQLException(errorMessage, new[] { node }));
            }
        }
        private void CheckDirectiveArgument(GraphQLArgument node, GraphQLDirectiveType directiveType, string argumentName)
        {
            var arguments = directiveType.GetArguments();

            if (arguments.Any(e => e.Name == argumentName))
            {
                return;
            }

            var errorMessage = this.ComposeUnknownDirectiveArgumentMessage(
                argumentName,
                directiveType.Name,
                StringUtils.SuggestionList(argumentName, arguments.Select(e => e.Name)));

            this.Errors.Add(new GraphQLException(errorMessage, new[] { node }));
        }
Ejemplo n.º 14
0
        public override GraphQLArgument BeginVisitArgument(GraphQLArgument argument)
        {
            var argumentType = this.GetLastArgumentType(argument);

            if (argumentType != null)
            {
                this.inputTypeStack.Push(argumentType);

                argument = base.BeginVisitArgument(argument);
                this.inputTypeStack.Pop();

                return(argument);
            }

            return(base.BeginVisitArgument(argument));
        }
        public override GraphQLArgument BeginVisitArgument(GraphQLArgument argument)
        {
            var argumentName = argument.Name.Value;
            var directive    = this.GetDirective();

            if (directive != null)
            {
                var directiveDefinition = this.schemaRepository.GetDirective(directive.Name.Value);

                this.CheckDirectiveArgument(argument, directiveDefinition, argumentName);
            }
            else
            {
                var fieldDefinition = this.GetLastField();

                if (fieldDefinition != null)
                {
                    this.CheckFieldArgument(argument, fieldDefinition, argumentName);
                }
            }

            return(base.BeginVisitArgument(argument));
        }
        protected GraphQLBaseType GetLastArgumentType(GraphQLArgument argument)
        {
            if (this.directive != null)
            {
                var directiveType = this.SchemaRepository.GetDirective(this.directive.Name.Value);
                return(directiveType?.GetArgument(argument.Name.Value)?
                       .GetGraphQLType(this.SchemaRepository));
            }
            else
            {
                var field = this.GetLastField();

                if (field == null)
                {
                    return(null);
                }

                return(field
                       .Arguments
                       .SingleOrDefault(e => e.Key == argument.Name.Value)
                       .Value?.GetGraphQLType(this.SchemaRepository));
            }
        }
Ejemplo n.º 17
0
 public SelectionArgument(GraphQLArgument op)
 {
     this.op   = op;
     this.Name = op.Name.Value;
 }
Ejemplo n.º 18
0
        public static object CoerceArgumentValue(
            ISchema schema,
            IReadOnlyDictionary <string, object> coercedVariableValues,
            string argumentName,
            Argument argumentDefinition,
            GraphQLArgument argument)
        {
            var argumentType  = argumentDefinition.Type;
            var defaultValue  = argumentDefinition.DefaultValue;
            var argumentValue = argument?.Value;

            var    hasValue = argumentValue != null;
            object value    = null;

            if (argumentValue is GraphQLVariable variable)
            {
                if (coercedVariableValues == null)
                {
                    hasValue = false;
                }
                else
                {
                    var variableName = variable.Name.Value;
                    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 GraphQLVariable)
                {
                    return(value);
                }

                var coercedValue = Values.CoerceValue(
                    schema.GetInputFields,
                    value,
                    argumentType);

                return(coercedValue);
            }

            return(defaultValue);
        }
 /// <summary>
 /// Initializes a new instance with the specified properties.
 /// </summary>
 public ArgumentsOfCorrectTypeError(ValidationContext context, GraphQLArgument node, string verboseErrors)
     : base(context.Document.Source, NUMBER, BadValueMessage(node.Name.StringValue, verboseErrors), node)
 {
 }
 /// <summary>
 /// Initializes a new instance with the specified properties.
 /// </summary>
 public KnownArgumentNamesError(ValidationContext context, GraphQLArgument node, FieldType fieldDef, IGraphType parentType)
     : base(context.Document.Source, NUMBER,
            UnknownArgMessage(
                node.Name.StringValue,
                fieldDef.Name,
                parentType.ToString() !,
Ejemplo n.º 21
0
 public virtual GraphQLArgument EndVisitArgument(GraphQLArgument argument) => argument;
Ejemplo n.º 22
0
 public virtual GraphQLArgument EndVisitArgument(GraphQLArgument argument)
 {
     return(argument);
 }
 /// <summary>
 /// Initializes a new instance with the specified properties.
 /// </summary>
 public UniqueArgumentNamesError(ValidationContext context, GraphQLArgument node, GraphQLArgument otherNode)
     : base(context.Document.Source, NUMBER, DuplicateArgMessage(node.Name.StringValue), node, otherNode)
 {
 }