我使用的是.NET后端和GraphQL.NET。我有一个用户模型,它有一个列表字典,如下所示:
public class UserResult
{
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string UserType { get; set; }
public List<string> Roles { get; set; }
public string Role { get; set; }
public uint? EntityId { get; set; }
public List<int> PartnerIds { get; set; }
public List<int> ProductIds { get; set; }
public bool Deleted { get; set; }
public string AdditionalData { get; set; }
public List<KeyValuePair<string, object>> AdditionalDataList { get; set; }
} 但是,问题在于,在UserType中,我找不到graphQL的任何数据类型,这就是获取错误的原因。守则如下:
public class UserType : ObjectGraphType<UserResult>
{
public enum UserTypeEnum
{
Employee,
Person
}
public class UserTypeEnumType : EnumerationGraphType<UserTypeEnum>
{
}
public UserType(IUserProductService userProductService)
{
Field(x => x.Id, nullable: true);
Field(x => x.Firstname, nullable: true);
Field(x => x.Lastname, nullable: true);
Field(x => x.Email, nullable: true);
Field(x => x.Roles, nullable: true);
Field(x => x.Role, nullable: true);
Field(x => x.AdditionalData, nullable: true);
Field(x => x.AdditionalDataList, nullable: true); // **I need to set type: typeof(ListGraphType<stringGraphType, ObjectGraphType>) but this is not working**
Field<UserTypeEnumType>(nameof(UserResult.UserType));
FieldAsync<IntGraphType>(
name: "entityId",
resolve: async _ =>
{
return -1;
});
Field(x => x.PartnerIds, nullable: true);
Field(x => x.Deleted, nullable: true);
FieldAsync<ListGraphType<UserProductType>>(
name: "userProducts",
description: "Products owned by this User",
resolve: async _ =>
{
var upr = new List<UserProductResult>();
_.Source.ProductIds?.ForEach(y =>
{
var t = new UserProductResult();
t.ProductId = y;
upr.Add(t);
});
return upr;
});
}
}我需要设置类型:type is (ListGraphType),但这不起作用。什么是正确的解决办法请给出你的答案。
发布于 2021-12-12 11:32:45
Field(x => x.AdditionalDataList, type: typeof(ListGraphType<StringGraphType>), nullable: true);这解决了我的目的。
https://stackoverflow.com/questions/70318736
复制相似问题