如何将 C# 字典列表类型映射到 GraphQL.NET 类型?
How to map C# dictionary list type to GraphQL.NET type?
我正在使用 .NET 后端和 GraphQL.NET。我有一个用户模型,它有一个列表字典 (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;
});
}
}
我需要设置类型:typeof(ListGraphType) 但这不起作用。什么可能是正确的解决方案,请给出您的答案。
Field(x => x.AdditionalDataList, type: typeof(ListGraphType<StringGraphType>), nullable: true);
这就解决了我的目的。
我正在使用 .NET 后端和 GraphQL.NET。我有一个用户模型,它有一个列表字典 (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;
});
}
}
我需要设置类型:typeof(ListGraphType
Field(x => x.AdditionalDataList, type: typeof(ListGraphType<StringGraphType>), nullable: true);
这就解决了我的目的。