本文整理汇总了C#中Variables.ValueFor方法的典型用法代码示例。如果您正苦于以下问题:C# Variables.ValueFor方法的具体用法?C# Variables.ValueFor怎么用?C# Variables.ValueFor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Variables
的用法示例。
在下文中一共展示了Variables.ValueFor方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CoerceValue
public object CoerceValue(ISchema schema, GraphType type, object input, Variables variables = null)
{
if (type is NonNullGraphType)
{
var nonNull = type as NonNullGraphType;
return CoerceValue(schema, schema.FindType(nonNull.Type), input, variables);
}
if (input == null)
{
return null;
}
var variable = input as Variable;
if (variable != null)
{
return variables != null
? variables.ValueFor(variable.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 => CoerceValue(schema, listItemType, item, variables)).ToArray()
: new[] { CoerceValue(schema, listItemType, input, variables) };
}
if (type is ObjectGraphType || type is InputObjectGraphType)
{
var obj = new Dictionary<string, object>();
if (input is KeyValuePair<string, object>)
{
var kvp = (KeyValuePair<string, object>)input;
input = new Dictionary<string, object> { { kvp.Key, kvp.Value } };
}
var kvps = input as IEnumerable<KeyValuePair<string, object>>;
if (kvps != null)
{
input = kvps.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
var dict = input as Dictionary<string, object>;
if (dict == null)
{
return null;
}
type.Fields.Apply(field =>
{
object inputValue;
if (dict.TryGetValue(field.Name, out inputValue))
{
var fieldValue = CoerceValue(schema, schema.FindType(field.Type), inputValue, variables);
obj[field.Name] = fieldValue ?? field.DefaultValue;
}
});
return obj;
}
if (type is ScalarGraphType)
{
var scalarType = type as ScalarGraphType;
return scalarType.Coerce(input);
}
return input;
}
示例2: CoerceValueAst
// 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 list = input as IEnumerable;
return list != null
? list.Map(item => CoerceValueAst(schema, listType, item, variables))
: 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 = CoerceValueAst(schema, schema.FindType(field.Type), dict[field.Name], variables);
obj[field.Name] = fieldValue ?? field.DefaultValue;
});
}
if (type is ScalarGraphType)
{
var scalarType = type as ScalarGraphType;
return scalarType.Coerce(input);
}
return input;
}
示例3: GetArgumentValues
public Dictionary<string, object> GetArgumentValues(ISchema 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 type = schema.FindType(arg.Type);
if (value is Variable)
{
var variable = (Variable) value;
value = variables.ValueFor(variable.Name);
}
object coercedValue = null;
if (IsValidValue(schema, type, value))
{
coercedValue = CoerceValue(schema, type, value, variables);
}
acc[arg.Name] = coercedValue ?? arg.DefaultValue;
return acc;
});
}
示例4: CoerceValue
public object CoerceValue(ISchema schema, IGraphType type, IValue input, Variables variables = null)
{
if (type is NonNullGraphType)
{
var nonNull = type as NonNullGraphType;
return CoerceValue(schema, nonNull.ResolvedType, input, variables);
}
if (input == null)
{
return null;
}
var variable = input as VariableReference;
if (variable != null)
{
return variables != null
? variables.ValueFor(variable.Name)
: null;
}
if (type is ListGraphType)
{
var listType = type as ListGraphType;
var listItemType = listType.ResolvedType;
var list = input as ListValue;
return list != null
? list.Values.Map(item => CoerceValue(schema, listItemType, item, variables)).ToArray()
: new[] { CoerceValue(schema, listItemType, input, variables) };
}
if (type is IObjectGraphType || type is InputObjectGraphType)
{
var complexType = type as IComplexGraphType;
var obj = new Dictionary<string, object>();
var objectValue = input as ObjectValue;
if (objectValue == null)
{
return null;
}
complexType.Fields.Apply(field =>
{
var objectField = objectValue.Field(field.Name);
if (objectField != null)
{
var fieldValue = CoerceValue(schema, field.ResolvedType, objectField.Value, variables);
fieldValue = fieldValue ?? field.DefaultValue;
obj[field.Name] = fieldValue;
}
});
return obj;
}
if (type is ScalarGraphType)
{
var scalarType = type as ScalarGraphType;
return scalarType.ParseLiteral(input);
}
return null;
}