首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用FiledType有效地将DTO类的所有属性映射到GraphQL.NET类?

如何使用FiledType有效地将DTO类的所有属性映射到GraphQL.NET类?
EN

Stack Overflow用户
提问于 2020-03-02 21:06:07
回答 2查看 425关注 0票数 0

我使用GraphQL.NET与asp.net核心3.1一起开发基于graphql的apis。我有以下代码:

ContactDTO.cs

代码语言:javascript
复制
    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

代码语言:javascript
复制
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

代码语言:javascript
复制
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的方法。

有人能帮我更好地完成这个映射吗?

EN

回答 2

Stack Overflow用户

发布于 2021-12-15 09:01:22

您可以使您的ContactType从AutoRegisteringObjectGraphType继承,然后只需在构造函数中添加需要显式解析器的额外字段(如您的情况下的"address“字段)。

https://github.com/graphql-dotnet/graphql-dotnet/blob/master/src/GraphQL/Types/Composite/AutoRegisteringObjectGraphType.cs

票数 1
EN

Stack Overflow用户

发布于 2020-05-28 18:08:33

我认为,从基类属性继承(准确地说,它是模式字段,使用自己的解析器,而不仅仅是简单的属性)并不是那么容易,我所做的只是找到更优雅的准备使用的解决方案,即基于GraphQL.Net的解决方案,在这里我可以在json文件中设置字段(或者如果您有大量字段,只需运行脚本来生成带有db-schema配置的json文件)。另外,定义相关字段的方法非常简单。只是谷歌有点像NReco.GraphQL

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60496639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档