本文整理汇总了C#中Fields.Apply方法的典型用法代码示例。如果您正苦于以下问题:C# Fields.Apply方法的具体用法?C# Fields.Apply怎么用?C# Fields.Apply使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Fields
的用法示例。
在下文中一共展示了Fields.Apply方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompleteValue
public async Task<object> CompleteValue(ExecutionContext context, GraphType fieldType, Fields fields, object result)
{
var nonNullType = fieldType as NonNullGraphType;
if (nonNullType != null)
{
var completed = await CompleteValue(context, context.Schema.FindType(nonNullType.Type), fields, result);
if (completed == null)
{
throw new ExecutionError("Cannot return null for non-null type. Field: {0}".ToFormat(nonNullType.Name));
}
return completed;
}
if (result == null)
{
return null;
}
if (fieldType is ScalarGraphType)
{
var scalarType = fieldType as ScalarGraphType;
var coercedValue = scalarType.Coerce(result);
return coercedValue;
}
if (fieldType is ListGraphType)
{
var list = result as IEnumerable;
if (list == null)
{
throw new ExecutionError("User error: expected an IEnumerable list though did not find one.");
}
var listType = fieldType as ListGraphType;
var itemType = context.Schema.FindType(listType.Type);
var results = await list.MapAsync(async item =>
{
return await CompleteValue(context, itemType, fields, item);
});
return results;
}
var objectType = fieldType as ObjectGraphType;
if (fieldType is InterfaceGraphType)
{
var interfaceType = fieldType as InterfaceGraphType;
objectType = interfaceType.ResolveType(result);
}
if (objectType == null)
{
return null;
}
var subFields = new Dictionary<string, Fields>();
fields.Apply(field =>
{
subFields = CollectFields(context, objectType, field.Selections, subFields);
});
return await ExecuteFields(context, objectType, result, subFields);
}
示例2: CompleteValueAsync
public async Task<object> CompleteValueAsync(ExecutionContext context, IGraphType fieldType, Fields fields, object result)
{
var field = fields != null ? fields.FirstOrDefault() : null;
var fieldName = field != null ? field.Name : null;
var nonNullType = fieldType as NonNullGraphType;
if (nonNullType != null)
{
var type = nonNullType.ResolvedType;
var completed = await CompleteValueAsync(context, type, fields, result).ConfigureAwait(false);
if (completed == null)
{
var error = new ExecutionError("Cannot return null for non-null type. Field: {0}, Type: {1}!."
.ToFormat(fieldName, type.Name));
error.AddLocation(field, context.Document);
throw error;
}
return completed;
}
if (result == null)
{
return null;
}
if (fieldType is ScalarGraphType)
{
var scalarType = fieldType as ScalarGraphType;
var coercedValue = scalarType.Serialize(result);
return coercedValue;
}
if (fieldType is ListGraphType)
{
var list = result as IEnumerable;
if (list == null)
{
var error = new ExecutionError("User error: expected an IEnumerable list though did not find one.");
error.AddLocation(field, context.Document);
throw error;
}
var listType = fieldType as ListGraphType;
var itemType = listType.ResolvedType;
var results = await list.MapAsync(async item => await CompleteValueAsync(context, itemType, fields, item).ConfigureAwait(false)).ConfigureAwait(false);
return results;
}
var objectType = fieldType as IObjectGraphType;
if (fieldType is IAbstractGraphType)
{
var abstractType = fieldType as IAbstractGraphType;
objectType = abstractType.GetObjectType(result);
if (objectType != null && !abstractType.IsPossibleType(objectType))
{
var error = new ExecutionError(
"Runtime Object type \"{0}\" is not a possible type for \"{1}\""
.ToFormat(objectType, abstractType));
error.AddLocation(field, context.Document);
throw error;
}
}
if (objectType == null)
{
return null;
}
if (objectType.IsTypeOf != null && !objectType.IsTypeOf(result))
{
var error = new ExecutionError(
"Expected value of type \"{0}\" but got: {1}."
.ToFormat(objectType, result));
error.AddLocation(field, context.Document);
throw error;
}
var subFields = new Dictionary<string, Fields>();
var visitedFragments = new List<string>();
fields.Apply(f =>
{
subFields = CollectFields(context, objectType, f.SelectionSet, subFields, visitedFragments);
});
return await ExecuteFieldsAsync(context, objectType, result, subFields).ConfigureAwait(false);
}