当前位置: 首页>>代码示例>>C#>>正文


C# ValidationContext.GetRecursiveVariables方法代码示例

本文整理汇总了C#中ValidationContext.GetRecursiveVariables方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationContext.GetRecursiveVariables方法的具体用法?C# ValidationContext.GetRecursiveVariables怎么用?C# ValidationContext.GetRecursiveVariables使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ValidationContext的用法示例。


在下文中一共展示了ValidationContext.GetRecursiveVariables方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Validate

        public INodeVisitor Validate(ValidationContext context)
        {
            var variableNameDefined = new Dictionary<string, bool>();

            return new EnterLeaveListener(_ =>
            {
                _.Match<VariableDefinition>(varDef => variableNameDefined[varDef.Name] = true);

                _.Match<Operation>(
                    enter: op => variableNameDefined = new Dictionary<string, bool>(),
                    leave: op =>
                    {
                        var usages = context.GetRecursiveVariables(op);
                        usages.Apply(usage =>
                        {
                            var varName = usage.Node.Name;
                            bool found;
                            if (!variableNameDefined.TryGetValue(varName, out found))
                            {
                                var error = new ValidationError(
                                    context.OriginalQuery,
                                    "5.7.4",
                                    UndefinedVarMessage(varName, op.Name),
                                    usage.Node,
                                    op);
                                context.ReportError(error);
                            }
                        });
                    });
            });
        }
开发者ID:mwatts,项目名称:graphql-dotnet,代码行数:31,代码来源:NoUndefinedVariables.cs

示例2: Validate

        public INodeVisitor Validate(ValidationContext context)
        {
            var variableDefs = new List<VariableDefinition>();

              return new EnterLeaveListener(_ =>
              {
            _.Match<VariableDefinition>(def => variableDefs.Add(def));

            _.Match<Operation>(
              enter: op => variableDefs = new List<VariableDefinition>(),
              leave: op =>
              {
            var usages = context.GetRecursiveVariables(op).Select(usage => usage.Node.Name);
            variableDefs.Apply(variableDef =>
            {
              var variableName = variableDef.Name;
              if (!usages.Contains(variableName))
              {
                var error = new ValidationError(context.OriginalQuery, "5.7.5", UnusedVariableMessage(variableName, op.Name), variableDef);
                context.ReportError(error);
              }
            });
              });
              });
        }
开发者ID:graphql-dotnet,项目名称:graphql-dotnet,代码行数:25,代码来源:NoUnusedVariables.cs

示例3: Validate

        public INodeVisitor Validate(ValidationContext context)
        {
            var varDefMap = new Dictionary<string, VariableDefinition>();

            return new EnterLeaveListener(_ =>
            {
                _.Match<VariableDefinition>(
                    varDefAst => varDefMap[varDefAst.Name] = varDefAst
                );

                _.Match<Operation>(
                    enter: op => varDefMap = new Dictionary<string, VariableDefinition>(),
                    leave: op =>
                    {
                        var usages = context.GetRecursiveVariables(op);
                        usages.Apply(usage =>
                        {
                            var varName = usage.Node.Name;
                            VariableDefinition varDef;
                            if (!varDefMap.TryGetValue(varName, out varDef))
                            {
                                return;
                            }

                            if (varDef != null && usage.Type != null)
                            {
                                var varType = varDef.Type.GraphTypeFromType(context.Schema);
                                if (varType != null &&
                                    !effectiveType(varType, varDef).IsSubtypeOf(usage.Type, context.Schema))
                                {
                                    var error = new ValidationError(
                                        context.OriginalQuery,
                                        "5.7.6",
                                        BadVarPosMessage(varName, context.Print(varType), context.Print(usage.Type)));

                                    var source = new Source(context.OriginalQuery);
                                    var varDefPos = new Location(source, varDef.SourceLocation.Start);
                                    var usagePos = new Location(source, usage.Node.SourceLocation.Start);

                                    error.AddLocation(varDefPos.Line, varDefPos.Column);
                                    error.AddLocation(usagePos.Line, usagePos.Column);

                                    context.ReportError(error);
                                }
                            }
                        });
                    }
                );
            });
        }
开发者ID:graphql-dotnet,项目名称:graphql-dotnet,代码行数:50,代码来源:VariablesInAllowedPosition.cs


注:本文中的ValidationContext.GetRecursiveVariables方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。