public __Type(Type t, Dictionary <Type, __Type> resolver, GraphQlCustomiseSchema customise, bool isInputType = false) { this.resolver = resolver; this.customiseSchema = customise; dotNetType = t; if (resolver.ContainsKey(t) == false) { resolver[t] = this; } if (TypeCheck.IsNumeric(t) || TypeCheck.IsString(t) || TypeCheck.IsDateTime(t) || TypeCheck.IsBoolean(t)) { ScalarType(); } else if (TypeCheck.IsEnum(t)) { EnumType(); } else if (TypeCheck.IsEnumerableType(t)) { ListType(); } else if (TypeCheck.IsClass(t) && isInputType) { InputObjectType(); } else if (TypeCheck.IsClass(t)) { ObjectOrInterfaceType(__TypeKind.OBJECT); } else if (t.IsInterface) { ObjectOrInterfaceType(__TypeKind.INTERFACE); } else if (TypeCheck.IsValueType(t)) { throw new Exception($"Unexpected value type = {t.Name}"); } else { throw new Exception($"Unexpected type = {t.Name}"); } }
Object CoerceDocumentValue(__Type argumentType, String argumentName, GraphQLParser.ValueContext val) { try { if (val is GraphQLParser.StringValueContext) { var typedVal = Trim(((GraphQLParser.StringValueContext)val).GetText()); if (argumentType.kind == __TypeKind.ENUM) { return(Enum.Parse(argumentType.dotNetType, typedVal)); } else if (argumentType.kind == __TypeKind.SCALAR) { if (TypeCheck.IsNumeric(argumentType.dotNetType)) { Error($"Cannot convert string to numeric value", val); } else if (TypeCheck.IsString(argumentType.dotNetType)) { return(typedVal); } else if (TypeCheck.IsDateTime(argumentType.dotNetType)) { return(DateTime.Parse(typedVal)); } } } else if (val is GraphQLParser.BooleanValueContext) { var typedVal = (val.GetText() == "true") ? true : false; if (argumentType.kind == __TypeKind.SCALAR) { if (TypeCheck.IsBoolean(argumentType.dotNetType)) { return(typedVal); } else if (TypeCheck.IsNumeric(argumentType.dotNetType)) { if (typedVal) { return(1); } return(0); } else if (TypeCheck.IsString(argumentType.dotNetType)) { if (typedVal) { return("true"); } return(false); } } } else if (val is GraphQLParser.NumberValueContext) { var typedValue = Convert.ToDouble(val.GetText()); if (TypeCheck.IsNumeric(argumentType.dotNetType)) { if (argumentType.dotNetType == typeof(int)) { return((int)typedValue); } else if (argumentType.dotNetType == typeof(short)) { return((short)typedValue); } else if (argumentType.dotNetType == typeof(uint)) { return((uint)typedValue); } else if (argumentType.dotNetType == typeof(uint)) { return((uint)typedValue); } else if (argumentType.dotNetType == typeof(float)) { return((float)typedValue); } else { return(typedValue); } } else if (TypeCheck.IsString(argumentType.dotNetType)) { return(typedValue.ToString()); } else if (TypeCheck.IsBoolean(argumentType.dotNetType)) { if (typedValue == 0.0) { return(false); } return(true); } } else if (val is GraphQLParser.ObjectValueContext) { var jsonObj = GraphQlJson.FromJSonString(val.GetText(), argumentType.dotNetType, true); return(jsonObj); } else if (val is GraphQLParser.EnumValueContext) { return(Enum.Parse(argumentType.dotNetType, val.GetText())); } else if (val is GraphQLParser.ArrayValueContext) { List <Object> objects = new List <object>(); foreach (var c in ((GraphQLParser.ArrayValueContext)val).array().value()) { objects.Add(CoerceDocumentValue(argumentType.ofType, argumentName, c)); } return(objects); } Error( $"Encountered unexpected DotNetType when coercing value - {argumentName} - {argumentType.dotNetType.Name}", val); return(0); } catch (Exception e) { Error( $"Error - '{e.Message}' trying to coerce '{argumentName}' of type '{val.GetType().Name}' to '{argumentType.kind}' with DotNetType: '{argumentType.dotNetType.Name}' ", val); return(0); } }