0

I am using GraphQL.NET with asp.net core 3.1 to develop graphql based apis. I have the following code:

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;
            }
        );
    }
}

In the above the ContactType.cs is very extensive class with many properties that are inherited from the ContactDTO.cs.

I am looking for a way to map all the properties of ContactDTO.cs to the ContactType.cs.

Can anyone help me to do this mapping in better way

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143

2 Answers2

1

You can make your ContactType inherit from AutoRegisteringObjectGraphType and then just add the extra fields in the constructor for which you need explicit resolvers(like the "address" field in your case)

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

tv31
  • 31
  • 3
0

I assume, that is not so easy just inherit from base class property (to be precise - it's schema field, with own resolvers, not just simple property) What I did - just find more elegant ready-to-use solution, that is based on GraphQL.Net, where I can set up fields in json file (or if you have tons of fields - just run script to generate that json file with db-schema cofiguration). In additional, quite simple way of defining related fields. just google something like NReco.GraphQL

Dmitriy
  • 124
  • 2