// TODO: combine dupliation with CoerceValueAST public object CoerceValue(Schema schema, GraphType type, object input) { if (type is NonNullGraphType) { var nonNull = type as NonNullGraphType; return CoerceValue(schema, schema.FindType(nonNull.Type), input); } if (input == null) { return null; } if (type is ListGraphType) { var listType = type as ListGraphType; var list = input as IEnumerable; return list != null ? list.Map(item => CoerceValue(schema, listType, item)) : new[] { input }; } if (type is ObjectGraphType) { var objType = type as ObjectGraphType; var obj = new Dictionary<string, object>(); var dict = (Dictionary<string, object>)input; objType.Fields.Apply(field => { var fieldValue = CoerceValue(schema, schema.FindType(field.Type), dict[field.Name]); obj[field.Name] = fieldValue ?? field.DefaultValue; }); } if (type is ScalarGraphType) { var scalarType = type as ScalarGraphType; return scalarType.Coerce(input); } return null; }
public object GetVariableValue(Schema schema, Variable variable, object input) { var type = schema.FindType(variable.Type.Name); if (IsValidValue(schema, type, input)) { if (input == null && variable.DefaultValue != null) { return CoerceValueAst(schema, type, variable.DefaultValue, null); } return CoerceValue(schema, type, input); } throw new Exception("Variable {0} expected type '{1}'.".ToFormat(variable.Name, type.Name)); }
public bool IsValidValue(Schema schema, GraphType type, object input) { if (type is NonNullGraphType) { if (input == null) { return false; } return IsValidValue(schema, schema.FindType(((NonNullGraphType)type).Type), input); } if (input == null) { return true; } if (type is ListGraphType) { var listType = (ListGraphType) type; var list = input as IEnumerable; return list != null ? list.All(item => IsValidValue(schema, type, item)) : IsValidValue(schema, listType, input); } if (type is ObjectGraphType) { var dict = input as Dictionary<string, object>; return dict != null && type.Fields.All(field => IsValidValue(schema, schema.FindType(field.Type), dict[field.Name])); } if (type is ScalarGraphType) { var scalar = (ScalarGraphType) type; return scalar.Coerce(input) != null; } return false; }
public Dictionary<string, object> GetArgumentValues(Schema schema, QueryArguments definitionArguments, Arguments astArguments, Variables variables) { if (definitionArguments == null || !definitionArguments.Any()) { return null; } return definitionArguments.Aggregate(new Dictionary<string, object>(), (acc, arg) => { var value = astArguments != null ? astArguments.ValueFor(arg.Name) : null; var coercedValue = CoerceValueAst(schema, schema.FindType(arg.Type), value, variables); acc[arg.Name] = coercedValue ?? arg.DefaultValue; return acc; }); }
// TODO: combine duplication with CoerceValue public object CoerceValueAst(Schema schema, GraphType type, object input, Variables variables) { if (type is NonNullGraphType) { var nonNull = type as NonNullGraphType; return CoerceValueAst(schema, schema.FindType(nonNull.Type), input, variables); } if (input == null) { return null; } if (input is Variable) { return variables != null ? variables.ValueFor(((Variable)input).Name) : null; } if (type is ListGraphType) { var listType = type as ListGraphType; var listItemType = schema.FindType(listType.Type); var list = input as IEnumerable; return list != null && !(input is string) ? list.Map(item => CoerceValueAst(schema, listItemType, item, variables)).ToArray() : new[] { CoerceValueAst(schema, listItemType, input, variables) }; } if (type is ObjectGraphType || type is InputObjectGraphType) { var objType = type; var obj = new Dictionary<string, object>(); var dict = input as Dictionary<string, object>; if (dict == null) { return null; } objType.Fields.Apply(field => { if (dict.ContainsKey(field.Name)) { var fieldValue = CoerceValueAst(schema, schema.FindType(field.Type), dict[field.Name], variables); obj[field.Name] = fieldValue ?? field.DefaultValue; } }); return obj; } if (type is ScalarGraphType) { var scalarType = type as ScalarGraphType; return scalarType.Coerce(input); } return input; }
public bool IsValidValue(Schema schema, GraphType type, object input) { if (type is NonNullGraphType) { if (input == null) { return false; } return IsValidValue(schema, schema.FindType(((NonNullGraphType)type).Type), input); } if (input == null) { return true; } if (type is ListGraphType) { var listType = (ListGraphType) type; var listItemType = schema.FindType(listType.Type); var list = input as IEnumerable; return list != null && !(input is string) ? list.All(item => IsValidValue(schema, listItemType, item)) : IsValidValue(schema, listItemType, input); } if (type is ObjectGraphType || type is InputObjectGraphType) { var dict = input as Dictionary<string, object>; if (dict == null) { return false; } // ensure every provided field is defined if (type is InputObjectGraphType && dict.Keys.Any(key => type.Fields.FirstOrDefault(field => field.Name == key) == null)) { return false; } return type.Fields.All(field => IsValidValue(schema, schema.FindType(field.Type), dict.ContainsKey(field.Name) ? dict[field.Name] : null)); } if (type is ScalarGraphType) { var scalar = (ScalarGraphType) type; return scalar.Coerce(input) != null; } return false; }
public object GetVariableValue(Schema schema, Variable variable, object input) { var type = schema.FindType(variable.Type.FullName); var value = input ?? variable.DefaultValue; if (IsValidValue(schema, type, value)) { return CoerceValue(schema, type, value); } if (value == null) { throw new Exception("Variable '${0}' of required type '{1}' was not provided.".ToFormat(variable.Name, variable.Type.FullName)); } throw new Exception("Variable '${0}' expected value of type '{1}'.".ToFormat(variable.Name, type.Name)); }
public void ContainsTypeNames(Schema schema, params string[] typeNames) { typeNames.Apply(typeName => { var type = schema.FindType(typeName); type.ShouldNotBeNull("Did not find {0} in type lookup.".ToFormat(typeName)); }); }