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); } } }); } ); }); }
public async Task should_add_extension_object_when_exception_is_thrown_with_error_code() { string query = "{ firstSync }"; string code = "FIRST"; var result = await Executer.ExecuteAsync(_ => { _.Schema = Schema; _.Query = query; }); var errors = new ExecutionErrors(); var error = new ValidationError(query, code, "Error trying to resolve firstSync."); error.AddLocation(1, 3); error.Path = new[] { "firstSync" }; errors.Add(error); var expectedResult = "{firstSync: null}"; AssertQuery(query, CreateQueryResult(expectedResult, errors), null, null); }
public void TestVariableObject_InvalidType(string param) { var error1 = new ValidationError(null, VariablesAreInputTypesError.NUMBER, VariablesAreInputTypesError.UndefinedVarMessage("arg", "abcdefg")) { Code = "VARIABLES_ARE_INPUT_TYPES" }; error1.AddLocation(1, 7); var error2 = new ValidationError(null, KnownTypeNamesError.NUMBER, KnownTypeNamesError.UnknownTypeMessage("abcdefg", null)) { Code = "KNOWN_TYPE_NAMES" }; error2.AddLocation(1, 13); var expected = CreateQueryResult(null, new ExecutionErrors { error1, error2 }); AssertQueryIgnoreErrors("query($arg: abcdefg) { test1 (arg: $arg) }", expected, inputs: $"{{ \"arg\": {param} }}".ToInputs(), expectedErrorCount: 2, renderErrors: true); }
private void Field(GraphType type, Field field, ValidationContext context) { if (type == null) { return; } if (type.IsLeafType(context.Schema)) { if (field.SelectionSet != null && field.SelectionSet.Selections.Any()) { var error = new ValidationError("", NoSubselectionAllowedMessage(field.Name, type.Name), field); error.AddLocation(field.SourceLocation.Line, field.SourceLocation.Column); context.ReportError(error); } } else if (field.SelectionSet == null || !field.SelectionSet.Selections.Any()) { var error = new ValidationError("", RequiredSubselectionMessage(field.Name, type.Name), field); error.AddLocation(field.SourceLocation.Line, field.SourceLocation.Column); context.ReportError(error); } }
public INodeVisitor Validate(ValidationContext context) { return(new NodeVisitorMatchFuncListener <Argument>( n => n is Argument, argAst => { var argDef = context.TypeInfo.GetArgument(); if (argDef != null) { var type = context.Schema.FindType(argDef.Type); var errors = type.IsValidLiteralValue(argAst.Value, context.Schema).ToList(); if (errors.Any()) { var error = new ValidationError( "5.3.3.1", BadValueMessage(argAst.Name, type, context.Print(argAst.Value), errors)); error.AddLocation(argAst.SourceLocation.Line, argAst.SourceLocation.Column); context.ReportError(error); } } })); }
public void when_argument_provided_cannot_be_parsed() { var query = @" { fieldWithDefaultArgumentValue(input: WRONG_TYPE) } "; var error = new ValidationError(null, ArgumentsOfCorrectTypeError.NUMBER, "Argument \u0027input\u0027 has invalid value. Expected type \u0027String\u0027, found WRONG_TYPE.") { Code = "ARGUMENTS_OF_CORRECT_TYPE", }; error.AddLocation(3, 45); var expected = new ExecutionResult { Errors = new ExecutionErrors { error }, }; AssertQueryIgnoreErrors(query, expected, renderErrors: true, expectedErrorCount: 1); }