我使用GraphQL.NET与asp.net核心3.1一起开发基于graphql的apis。我有以下代码:
ContactDTO.cs
public class ContactDTO
{
public int ContactId { get; set; }
public int? ImageId { get; set; }
public string ImagePath { get; set; }
public string OfficePhone { get; set; }
public string OfficeFaxNumber { get; set; }
public string MobilePhoneNumber { get; set; }
public string Email { get; set; }
public string Website { get; set; }
public string FaceBookUrl { get; set; }
public string TwitterUrl { get; set; }
public string LinkedInUrl { get; set; }
public string GooglePlusUrl { get; set; }
public string WeChatUrl { get; set; }
public string WeboUrl { get; set; }
public string XINGUrl { get; set; }
public string VKUrl { get; set; }
public int? CountryId { get; set; }
public string CreatedDate { get; set; }
public string UpdatedDate { get; set; }
public string EmpGUID { get; set; }
public int? LanguageId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string NativeName { get; set; }
public string Title { get; set; }
public string Role { get; set; }
public string EmployeeLevel { get; set; }
public string Introduction { get; set; }
public string Organization { get; set; }
public string OrganizationUnitName { get; set; }
public int AddressId { get; set; }
public AddressDTO address { get; set; }
}
}AddressDTO.cs
public class AddressDTO
{
public int AddressId { get; set; }
public string Street { get; set; }
public string Country { get; set; }
public string City { get; set; }
}ContactType.cs
public class ContactType : ObjectGraphType<ContactDTO>, IGraphQLType
{
public ContactType()
{
Name = "Contact";
Field(co => co.ContactId, nullable: true);
Field(co => co.OfficePhone, nullable: true);
Field(co => co.OfficeFaxNumber, nullable: true);
Field(co => co.MobilePhoneNumber, nullable: true);
Field(co => co.Email, nullable: true);
Field(co => co.Website, nullable: true);
Field(co => co.FaceBookUrl, nullable: true);
Field(co => co.TwitterUrl, nullable: true);
Field(co => co.LinkedInUrl, nullable: true);
Field(co => co.GooglePlusUrl, nullable: true);
Field(co => co.WeChatUrl, nullable: true);
Field(co => co.WeboUrl, nullable: true);
Field(co => co.XINGUrl, nullable: true);
Field(co => co.VKUrl, nullable: true);
Field(co => co.FirstName, nullable: true);
Field(co => co.LastName, nullable: true);
Field(co => co.NativeName, nullable: true);
Field(co => co.Title, nullable: true);
Field(co => co.Role, nullable: true);
Field(co => co.EmployeeLevel, nullable: true);
Field(co => co.Introduction, nullable: true);
Field(co => co.Organization, nullable: true);
Field(co => co.OrganizationUnitName, nullable: true);
Field(co => co.ImagePath, nullable: true);
Field<AddressType>(
"address",
resolve: context =>
{
return context.Source.address;
}
);
}
}在上面,ContactType.cs是非常广泛的类,具有许多从ContactDTO.cs继承的属性。
我正在寻找一种将ContactDTO.cs的所有属性映射到ContactType.cs的方法。
有人能帮我更好地完成这个映射吗?
发布于 2021-12-15 09:01:22
您可以使您的ContactType从AutoRegisteringObjectGraphType继承,然后只需在构造函数中添加需要显式解析器的额外字段(如您的情况下的"address“字段)。
发布于 2020-05-28 18:08:33
我认为,从基类属性继承(准确地说,它是模式字段,使用自己的解析器,而不仅仅是简单的属性)并不是那么容易,我所做的只是找到更优雅的准备使用的解决方案,即基于GraphQL.Net的解决方案,在这里我可以在json文件中设置字段(或者如果您有大量字段,只需运行脚本来生成带有db-schema配置的json文件)。另外,定义相关字段的方法非常简单。只是谷歌有点像NReco.GraphQL
https://stackoverflow.com/questions/60496639
复制相似问题