Search code examples
c#.netgraphqltype-conversiongraphql.net

How to map C# dictionary list type to GraphQL.NET type?


I am using .NET backend and GraphQL.NET. I have user model which has a List dictionary (AdditionalDataList) as follows:

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

    } 

But, the problem is that in the UserType I can not find any data type for the graphQL and that is why getting error. Following are the code:

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

I need to set type: typeof(ListGraphType<stringGraphType, ObjectGraphType>) but this is not working. What might be the proper solution please put your answer.


Solution

  •  Field(x => x.AdditionalDataList, type: typeof(ListGraphType<StringGraphType>), nullable: true);
    

    This is solved my purpose.