首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将C#字典列表类型映射为GraphQL.NET类型?

如何将C#字典列表类型映射为GraphQL.NET类型?
EN

Stack Overflow用户
提问于 2021-12-11 20:24:14
回答 1查看 564关注 0票数 0

我使用的是.NET后端和GraphQL.NET。我有一个用户模型,它有一个列表字典,如下所示:

代码语言:javascript
复制
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的任何数据类型,这就是获取错误的原因。守则如下:

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

我需要设置类型:type is (ListGraphType),但这不起作用。什么是正确的解决办法请给出你的答案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-12 11:32:45

代码语言:javascript
复制
 Field(x => x.AdditionalDataList, type: typeof(ListGraphType<StringGraphType>), nullable: true);

这解决了我的目的。

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

https://stackoverflow.com/questions/70318736

复制
相关文章

相似问题

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