public object ConvertInputEnumValue(RequestContext context, object inpValue, RequestObjectBase anchor) { if (inpValue == null) { return(null); } if (Handler.IsFlagSet) { if (inpValue is string s) { inpValue = new string[] { s } } ; if (inpValue is IList <string> strings) { return(Handler.ConvertStringListToFlagsEnumValue(strings)); } throw new InvalidInputException( $"Input value '{inpValue}' cannot be converted to type '{this.Name}'; expected list of strings.", anchor); } else { // not input flags if (!(inpValue is string stringValue)) { throw new InvalidInputException($"Input value '{inpValue}' cannot be converted to type '{this.Name}'; expected string", anchor); } return(Handler.ConvertStringToEnumValue(stringValue)); } //else }
private ObjectValueSource BuildInputObject(Node fieldsNode, RequestObjectBase parent) { var fields = new Dictionary <string, ValueSource>(); foreach (var fldNode in fieldsNode.ChildNodes) { var name = fldNode.ChildNodes[0].GetText(); _path.Push(name); var valueNode = fldNode.ChildNodes[1]; var fldValue = BuildInputValue(valueNode, parent); if (fields.ContainsKey(name)) { AddError($"Duplicate field '{name}'.", valueNode); } else { fields.Add(name, fldValue); } _path.Pop(); } return(new ObjectValueSource() { SourceLocation = fieldsNode.GetLocation(), Fields = fields, Parent = parent }); }
public void AddError(string message, RequestObjectBase item, string errorType = ErrorCodes.BadRequest) { var path = item.GetRequestObjectPath(); var err = new GraphQLError(message, path, item.SourceLocation, errorType); AddError(err); }
private ConstInputValue CreateConstantInputValue(InputValueDef inputDef, RequestObjectBase anchor, TypeRef resultTypeRef, object value) { // We convert const value upfront to target typeRef var convValue = _requestContext.ValidateConvert(value, resultTypeRef, anchor); var constEval = new ConstInputValue(inputDef, resultTypeRef, anchor, convValue); return(constEval); }
internal static IList <object> GetRequestObjectPath(this RequestObjectBase obj) { var path = (obj.Parent == null) ? new List <object>() : obj.Parent.GetRequestObjectPath(); if (obj is NamedRequestObject namedObj && !(obj is GraphQLOperation)) // Operation name is NOT included in path { path.Add(namedObj.Name); } return(path); }
private ValueSource BuildInputValue(Node valueNode, RequestObjectBase parent) { if (valueNode.Token != null) { if (valueNode.Term.Name == TermNames.VarName) { var varName = valueNode.Token.Text.Substring(1); //cut off $ prefix return(new VariableValueSource() { VariableName = varName, SourceLocation = valueNode.GetLocation(), Parent = parent }); } else { var tkn = valueNode.Token; var tknData = new TokenData() { TermName = tkn.Terminal.Name, Text = tkn.Text, ParsedValue = tkn.Value }; return(new TokenValueSource() { TokenData = tknData, SourceLocation = valueNode.GetLocation(), Parent = parent }); } } switch (valueNode.Term.Name) { case TermNames.ConstList: var values = valueNode.ChildNodes.Select(n => BuildInputValue(n, parent)).ToArray(); return(new ListValueSource() { Values = values, SourceLocation = valueNode.GetLocation(), Parent = parent }); case TermNames.ConstInpObj: return(BuildInputObject(valueNode.ChildNodes[0], parent)); default: // never happens; if it ever does, better throw exc than return null throw new Exception($"FATAL: Unexpected term ({valueNode.Term.Name}), input value expected."); } }
public static object ValidateConvert(this RequestContext context, object value, TypeRef typeRef, RequestObjectBase anchor) { if (value == null) { if (typeRef.IsNotNull) { throw new InvalidInputException( $"Input value evaluated to null, but expected type '{typeRef.Name}' is not nullable.", anchor); } return(value); } // value not null; check if types match var valueType = value.GetType(); if (valueType == typeRef.ClrType) { return(value); } if (typeRef.TypeDef.ClrType == valueType) { return(value); } switch (typeRef.TypeDef) { case ScalarTypeDef sctdef: // most common case - let Scalar take care of it var convValue = sctdef.Scalar.ConvertInputValue(context, value); return(convValue); case EnumTypeDef etd: return(etd.ConvertInputEnumValue(context, value, anchor)); case InputObjectTypeDef _: return(value); default: throw new Exception($"Failed to convert value '{value}' to type {typeRef.Name}."); } }//method
private void AddError(string message, RequestObjectBase item, string errorType = ErrorCodes.BadRequest) { _requestContext.AddError(message, item, errorType); }
public static void AddInputError(this RequestContext context, string message, RequestObjectBase anchor) { var path = anchor.GetRequestObjectPath(); var loc = anchor.SourceLocation; var err = new GraphQLError(message, path, loc, ErrorCodes.InputError); context.AddError(err); }
public InvalidInputException(NamedRequestObject anchor, Exception inner) : base(inner.Message, inner) { Anchor = anchor; }
public InvalidInputException(string message, RequestObjectBase anchor, Exception inner = null) : base(message, inner) { Anchor = anchor; }
private RequestDirective BuildDirective(Node dirNode, DirectiveLocation atLocation, RequestObjectBase parent) { var dirName = dirNode.ChildNodes[0].ChildNodes[1].GetText(); // child0 is dirName-> @+name _path.Push(dirName); try { var dirDef = LookupDirective(dirName, dirNode); if (dirDef == null) { return(null); // error is already logged } if (!dirDef.Registration.Locations.IsSet(atLocation)) { AddError($"Directive @{dirName} may not be placed at this location ({atLocation}). Valid locations: [{dirDef.Registration.Locations}].", dirNode); return(null); } var dir = new RequestDirective() { Def = dirDef, Location = atLocation, Name = dirName, SourceLocation = dirNode.GetLocation(), Parent = parent }; var argListNode = dirNode.FindChild(TermNames.ArgListOpt); dir.Args = BuildArguments(argListNode.ChildNodes, dir); return(dir); } finally { _path.Pop(); } }
private List <RequestDirective> BuildDirectives(Node dirListNode, DirectiveLocation atLocation, RequestObjectBase parent) { var dirList = new List <RequestDirective>(); if (dirListNode == null) { return(dirList); } foreach (var dirNode in dirListNode.ChildNodes) { var dir = BuildDirective(dirNode, atLocation, parent); if (dir == null) { continue; } dirList.Add(dir); } return(dirList); }
public OperationErrorEventArgs(RequestContext context, RequestObjectBase requestItem, Exception exc) { this.RequestContext = context; RequestItem = requestItem; Exception = exc; }