我正在使用 .NET 后端和 GraphQL.NET。我有一个用户模型,它有一个 List 字典(AdditionalDataList),如下所示:
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: typeof(ListGraphType<stringGraphType, ObjectGraphType>) 但这不起作用。什么可能是正确的解决方案,请提出您的答案。