Beispiel #1
0
        public AuthorizeDirective(
            SerializationInfo info,
            StreamingContext context)
        {
            var node = info.GetValue(
                nameof(DirectiveNode),
                typeof(DirectiveNode))
                       as DirectiveNode;

            if (node == null)
            {
                Roles = (string[])info.GetValue(
                    nameof(Roles),
                    typeof(string[]));
            }
            else
            {
                ArgumentNode rolesArgument = node.Arguments
                                             .FirstOrDefault(t => t.Name.Value == "roles");

                Roles = (rolesArgument != null &&
                         rolesArgument.Value is ListValueNode lv)
                    ? lv.Items.OfType <StringValueNode>()
                        .Select(t => t.Value?.Trim())
                        .Where(s => !string.IsNullOrEmpty(s))
                        .ToArray()
                    : Array.Empty <string>();
            }
        }
        private static EventDescription CreateEventDescription(String _eventName, String _argumentTag, String _argumentValue)
        {
            ArgumentNode     argumentNode     = new ArgumentNode(_argumentTag, _argumentValue);
            EventDescription eventDescription = new EventDescription(_eventName, argumentNode);

            return(eventDescription);
        }
Beispiel #3
0
        protected override ISyntaxVisitorAction Enter(
            ArgumentNode node,
            IDocumentValidatorContext context)
        {
            if (context.Directives.TryPeek(out DirectiveType directive))
            {
                if (directive.Arguments.TryGetField(node.Name.Value, out Argument? argument))
                {
                    context.InputFields.Push(argument);
                    context.Types.Push(argument.Type);
                    return(Continue);
                }
                context.UnexpectedErrorsDetected = true;
                return(Skip);
            }

            if (context.OutputFields.TryPeek(out IOutputField field))
            {
                if (field.Arguments.TryGetField(node.Name.Value, out IInputField? argument))
                {
                    context.InputFields.Push(argument);
                    context.Types.Push(argument.Type);
                    return(Continue);
                }
                context.UnexpectedErrorsDetected = true;
                return(Skip);
            }

            context.UnexpectedErrorsDetected = true;
            return(Skip);
        }
Beispiel #4
0
        public static string DeprecationReason(
            this Language.IHasDirectives syntaxNode)
        {
            DirectiveNode directive = syntaxNode.Directives.FirstOrDefault(t =>
                                                                           t.Name.Value == WellKnownDirectives.Deprecated);

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

            ArgumentNode argument = directive.Arguments.FirstOrDefault(t =>
                                                                       t.Name.Value == WellKnownDirectives.DeprecationReasonArgument);

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

            if (argument.Value is StringValueNode s)
            {
                return(s.Value);
            }

            return(null);
        }
Beispiel #5
0
        public static IValueNode IncludeValue(
            this IEnumerable <DirectiveNode> directives)
        {
            DirectiveNode directive = directives.GetIncludeDirective();

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

            ArgumentNode argumentNode = directive.Arguments.SingleOrDefault();

            if (argumentNode == null)
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetMessage(string.Format(
                                          CultureInfo.InvariantCulture,
                                          CoreResources
                                          .DirectiveCollectionExtensions_NotValid,
                                          directive.Name.Value))
                          .Build());
            }

            return(argumentNode.Value);
        }
        public static NameString GetOriginalName(
            this INamedSyntaxNode typeDefinition,
            NameString schemaName)
        {
            if (typeDefinition == null)
            {
                throw new ArgumentNullException(nameof(typeDefinition));
            }

            schemaName.EnsureNotEmpty(nameof(schemaName));

            DirectiveNode sourceDirective = typeDefinition.Directives
                                            .FirstOrDefault(t => HasSourceDirective(t, schemaName));

            if (sourceDirective != null)
            {
                ArgumentNode argument = sourceDirective.Arguments.First(t =>
                                                                        DirectiveFieldNames.Source_Name.Equals(t.Name.Value));
                if (argument.Value is StringValueNode value)
                {
                    return(value.Value);
                }
            }

            return(typeDefinition.Name.Value);
        }
Beispiel #7
0
        private static DirectiveNode CreateExecutionInfo(ISelection selection)
        {
            var arguments = new ArgumentNode[selection.IsInternal ? 4 : 3];

            arguments[0] = new ArgumentNode("id", new IntValueNode(selection.Id));
            arguments[1] = new ArgumentNode("kind", new EnumValueNode(selection.Strategy));

            if (selection.Field.Type.IsListType())
            {
                if (selection.Field.Type.NamedType().IsLeafType())
                {
                    arguments[2] = new ArgumentNode("type", new EnumValueNode("LEAF_LIST"));
                }
                else
                {
                    arguments[2] = new ArgumentNode("type", new EnumValueNode("COMPOSITE_LIST"));
                }
            }
            else if (selection.Field.Type.IsCompositeType())
            {
                arguments[2] = new ArgumentNode("type", new EnumValueNode("COMPOSITE"));
            }
            else if (selection.Field.Type.IsLeafType())
            {
                arguments[2] = new ArgumentNode("type", new EnumValueNode("LEAF"));
            }

            if (selection.IsInternal)
            {
                arguments[3] = new ArgumentNode("internal", BooleanValueNode.True);
            }

            return(new DirectiveNode("__execute", arguments));
        }
Beispiel #8
0
 public AbstractProperty(ArgumentNode node, T parent)
 {
     Name     = node.Name;
     Node     = node;
     Parent   = parent;
     DataType = new AbstractDataType(node.ArgType);
 }
Beispiel #9
0
        private CostDirective(
            SerializationInfo info,
            StreamingContext context)
        {
            var node = info.GetValue(
                nameof(DirectiveNode),
                typeof(DirectiveNode))
                       as DirectiveNode;

            if (node is null)
            {
                _complexity        = info.GetInt32(nameof(Complexity));
                _defaultMultiplier = info.GetInt32(nameof(DefaultMultiplier));
                _multipliers       = ((string[])info
                                      .GetValue(nameof(Multipliers), typeof(string[])))
                                     .Where(s => !string.IsNullOrEmpty(s))
                                     .Select(s => new MultiplierPathString(s))
                                     .ToArray();
            }
            else
            {
                ArgumentNode complexityArgument = node.Arguments
                                                  .FirstOrDefault(t => t.Name.Value == "complexity");
                ArgumentNode multipliersArgument = node.Arguments
                                                   .FirstOrDefault(t => t.Name.Value == "multipliers");
                ArgumentNode defaultMultiplierArgument = node.Arguments
                                                         .FirstOrDefault(t => t.Name.Value == "defaultMultiplier");

                _complexity = complexityArgument is { Value : IntValueNode iv }
Beispiel #10
0
        protected static void AddElement(
            TProgram program, ArgumentNode node, SymbolNode rootNode, HashSet <SymbolNode> visited)
        {
            if (program == null)
            {
                return;
            }

            // creates child symbol node if needed and updates count
            var numChildren = program.Children?.Count ?? 0;

            if (!node.Children.ContainsKey(program.Label))
            {
                node.Children.Add(program.Label, new SymbolNode(program.Label, numChildren, rootNode));
            }
            var symbolNode = node.Children[program.Label];

            if (!visited.Contains(symbolNode))
            {
                symbolNode.Value++;
                visited.Add(symbolNode);
            }

            // recurses through children
            if (program.Children == null || program.Children.Count == 0)
            {
                return;
            }
            for (var i = 0; i < program.Children.Count; i++)
            {
                AddElement((TProgram)program.Children[i], symbolNode.Children[i], rootNode, visited);
            }
        }
Beispiel #11
0
        public static int DefaultCalculation(
            IOutputField field,
            FieldNode selection,
            CostDirective?cost,
            int fieldDepth,
            int nodeDepth,
            Func <string, object?> getVariable,
            IMaxComplexityOptionsAccessor options)
        {
            if (cost is null)
            {
                return(options.DefaultComplexity);
            }

            if (options.UseComplexityMultipliers)
            {
                if (cost.Multipliers.Count == 0)
                {
                    return(cost.Complexity);
                }

                int complexity = 0;

                for (int i = 0; i < cost.Multipliers.Count; i++)
                {
                    MultiplierPathString multiplier = cost.Multipliers[i];
                    ArgumentNode         argument   = selection.Arguments.FirstOrDefault(t =>
                                                                                         t.Name.Value.Equals(multiplier.Value));

                    if (argument is { } && argument.Value is { })
Beispiel #12
0
        public static string GetSchemaName(this FieldNode field)
        {
            DirectiveNode directive = field.Directives
                                      .SingleOrDefault(t => IsSchemaDirective(t));

            if (directive == null)
            {
                throw new ArgumentException(
                          "The specified field is not annotated.");
            }

            ArgumentNode argument = directive.Arguments
                                    .SingleOrDefault(t => t.IsNameArgument());

            if (argument == null)
            {
                throw new ArgumentException(
                          "The schema directive has to have a name argument.");
            }

            if (argument.Value is StringValueNode value)
            {
                return(value.Value);
            }

            throw new ArgumentException(
                      "The schema directive name attribute " +
                      "has to be a string value.");
        }
        private static bool?EvaluateDirective(
            this DirectiveNode directive,
            VariableCollection variables)
        {
            if (directive == null)
            {
                return(null);
            }

            ArgumentNode argumentNode = directive.Arguments.SingleOrDefault();

            if (argumentNode == null)
            {
                throw new QueryException(new QueryError(
                                             $"The {directive.Name.Value} attribute is not valid."));
            }

            if (argumentNode.Value is BooleanValueNode b)
            {
                return(b.Value);
            }

            if (argumentNode.Value is VariableNode v)
            {
                return(variables.GetVariable <bool>(v.Name.Value));
            }

            throw new QueryException(new QueryError(
                                         $"The {directive.Name.Value} if-argument value has to be a 'Boolean'."));
        }
 private bool AreFieldArgumentsEqual(
     ArgumentNode argumentA,
     ArgumentNode argumentB)
 {
     return(argumentA.Name.Value == argumentB.Name.Value &&
            argumentA.Value.Equals(argumentB.Value));
 }
        private static bool TryGetMultiplierFromObject(
            FieldNode field,
            IVariableValueCollection variables,
            string multiplierPath,
            out int value)
        {
            var    path = new Queue <string>(multiplierPath.Split('.'));
            string name = path.Dequeue();

            ArgumentNode argument = field.Arguments
                                    .FirstOrDefault(t => t.Name.Value == name);

            if (argument == null)
            {
                value = default;
                return(false);
            }

            IValueNode current = argument.Value;

            while (current is ObjectValueNode)
            {
                if (current is ObjectValueNode obj)
                {
                    current = ResolveObjectField(obj, path);
                }
            }

            return(TryParseValue(current, variables, out value));
        }
        protected override ArgumentNode RewriteArgument(
            ArgumentNode node,
            Context context)
        {
            ArgumentNode current = node;

            if (context.OutputField != null &&
                context.OutputField.Arguments.TryGetField(current.Name.Value,
                                                          out IInputField inputField))
            {
                Context cloned = context.Clone();
                cloned.InputField = inputField;
                cloned.InputType  = inputField.Type;

                if (inputField.TryGetSourceDirective(context.Schema,
                                                     out SourceDirective sourceDirective) &&
                    !sourceDirective.Name.Equals(current.Name.Value))
                {
                    current = current.WithName(
                        new NameNode(sourceDirective.Name));
                }

                return(base.RewriteArgument(current, cloned));
            }

            return(base.RewriteArgument(current, context));
        }
Beispiel #17
0
 protected override void VisitArgument(
     ArgumentNode node,
     TContext context)
 {
     VisitName(node.Name, context);
     VisitValue(node.Value, context);
 }
Beispiel #18
0
 protected override ISyntaxVisitorAction Leave(
     ArgumentNode node,
     IDocumentValidatorContext context)
 {
     context.InputFields.Pop();
     context.Types.Pop();
     return(Continue);
 }
Beispiel #19
0
 protected override void ResolveChildren(
     ArgumentNode node,
     IList <SyntaxNodeInfo> children)
 {
     ResolveChildren(
         nameof(node.Value),
         node.Value,
         children);
 }
Beispiel #20
0
 public static IError ArgumentValueIsInvalid(
     ArgumentNode argument,
     string responseName,
     GraphQLException exception)
 {
     return(ErrorBuilder.FromError(exception.Errors[0])
            .AddLocation(argument)
            .SetExtension("responseName", responseName)
            .Build());
 }
Beispiel #21
0
 public static IError ArgumentValueIsInvalid(
     ArgumentNode argument,
     string responseName,
     ScalarSerializationException exception)
 {
     return(ErrorBuilder.New()
            .SetMessage(exception.Message)
            .AddLocation(argument)
            .SetExtension("responseName", responseName)
            .Build());
 }
 public VisitorAction Leave(
     ArgumentNode node,
     ISyntaxNode parent,
     IReadOnlyList <object> path,
     IReadOnlyList <ISyntaxNode> ancestors)
 {
     if (_action.Pop() == VisitorAction.Continue)
     {
         _type.Pop();
     }
     return(VisitorAction.Continue);
 }
Beispiel #23
0
        public static IError ArgumentNotUnique(
            this IDocumentValidatorContext context,
            ArgumentNode node,
            IOutputField?field      = null,
            DirectiveType?directive = null)
        {
            IErrorBuilder builder = ErrorBuilder.New()
                                    .SetMessage(Resources.ErrorHelper_ArgumentNotUnique)
                                    .AddLocation(node)
                                    .SetPath(context.CreateErrorPath());

            if (field is { })
Beispiel #24
0
 public VariableUsage(
     IInputField inputField,
     ArgumentNode argument,
     VariableNode variable)
 {
     InputField = inputField
                  ?? throw new ArgumentNullException(nameof(inputField));
     Argument = argument
                ?? throw new ArgumentNullException(nameof(argument));
     Variable = variable
                ?? throw new ArgumentNullException(nameof(variable));
 }
 private static bool HasSourceDirective(
     DirectiveNode directive,
     NameString schemaName)
 {
     if (DirectiveNames.Source.Equals(directive.Name.Value))
     {
         ArgumentNode argument = directive.Arguments.FirstOrDefault(t =>
                                                                    DirectiveFieldNames.Source_Schema.Equals(t.Name.Value));
         return(argument != null &&
                argument.Value is StringValueNode sv &&
                schemaName.Equals(sv.Value));
     }
     return(false);
 }
 public static IError ArgumentNonNullError(
     ArgumentNode argument,
     string responseName,
     ArgumentNonNullValidator.ValidationResult validationResult)
 {
     return(ErrorBuilder.New()
            .SetMessage(
                "Detected a non-null violation in argument `{0}`.",
                argument.Name.Value)
            .AddLocation(argument)
            .SetExtension("responseName", responseName)
            .SetExtension("errorPath", validationResult.Path)
            .Build());
 }
Beispiel #27
0
        public void AddCommand(string commandstring)
        {
            CommandStrings.Add(commandstring);
            ArgumentNode an = new ArgumentNode(commandstring);

            if (an.Key != null)
            {
                Commands.Add(an.Key, an);
            }
            else
            {
                throw new ArgumentException("Outermost command must have a name");
            }
        }
Beispiel #28
0
 public static IError ArgumentNonNullError(
     ArgumentNode argument,
     string responseName,
     ArgumentNonNullValidator.ValidationResult validationResult)
 {
     return(ErrorBuilder.New()
            .SetMessage(
                ErrorHelper_ArgumentNonNullError_Message,
                argument.Name.Value)
            .AddLocation(argument)
            .SetExtension("responseName", responseName)
            .SetExtension("errorPath", validationResult.Path)
            .Build());
 }
Beispiel #29
0
 public static IError ArgumentValueIsNotCompatible(
     this IDocumentValidatorContext context,
     ArgumentNode node,
     IInputType locationType,
     IValueNode valueNode)
 {
     return(ErrorBuilder.New()
            .SetMessage(Resources.ErrorHelper_ArgumentValueIsNotCompatible)
            .AddLocation(valueNode)
            .SetPath(context.CreateErrorPath())
            .SetExtension("argument", node.Name.Value)
            .SetExtension("argumentValue", valueNode.ToString())
            .SetExtension("locationType", locationType.Print())
            .SpecifiedBy("sec-Values-of-Correct-Type")
            .Build());
 }
        private static IValueNode GetIfArgumentValue(DirectiveNode directive)
        {
            if (directive.Arguments.Count == 1)
            {
                ArgumentNode argument = directive.Arguments[0];
                if (string.Equals(
                        argument.Name.Value,
                        WellKnownDirectives.IfArgument,
                        StringComparison.Ordinal))
                {
                    return(argument.Value);
                }
            }

            throw ThrowHelper.MissingIfArgument(directive);
        }