首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HotChocolate按ObjectId类型过滤

使用HotChocolate按ObjectId类型过滤
EN

Stack Overflow用户
提问于 2020-11-02 23:22:14
回答 1查看 682关注 0票数 2

我正在尝试创建一个Graphql模式,模式正在创建,但我看不到Id (它是MongoDb ObjectId类型)的过滤选项。你能告诉我我错过了什么吗?我也想按Id进行过滤,但在创建的模式上没有这样的选项。你可以在下面看到我的测试实现。

代码语言:javascript
复制
public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            TypeConversion.Default.Register<string, ObjectId>(from => ObjectId.Parse(from));
            TypeConversion.Default.Register<ObjectId, string>(from => from.ToString());            
            services.AddSingleton<IAuthorService, InMemoryAuthorService>();

            services.AddGraphQL(s => SchemaBuilder.New()
                .AddServices(s)
                .AddType<AuthorType>()
                .AddQueryType<Query>()
                .BindClrType<ObjectId, IdType>()
                .Create());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UsePlayground(new PlaygroundOptions
                {
                    QueryPath = "/api",
                    Path = "/playground"
                });
            }
            app.UseRouting();
            app.UseGraphQL("/api");
        }
    }
 public class AuthorType : ObjectType<Author>
    {
        protected override void Configure(IObjectTypeDescriptor<Author> descriptor)
        {
            descriptor.Field(t => t.Id)
                     .Type<IdType>()
                     .Resolver(c => c.Parent<Author>().Id);
            descriptor.Field(a => a.AuthorId).Type<StringType>();
            descriptor.Field(a => a.Name).Type<StringType>();
            descriptor.Field(a => a.Surname).Ignore();
        }
    }
    public class Author
    {
        public ObjectId Id { get; set; }
        public int AuthorId { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }

    public class Query
    {
        private readonly IAuthorService _authorService;            

        public Query(IAuthorService authorService)
        {
            _authorService = authorService;               
        }
        [UsePaging]
        [UseFiltering]
        public IQueryable<Author> Authors() => _authorService.GetAll();    
      
    }


    public interface IAuthorService
    {
        IQueryable<Author> GetAll();
    }

    public class InMemoryAuthorService : IAuthorService
    {
        private IList<Author> _authors;

        public InMemoryAuthorService()
        {
            _authors = new List<Author>()
            {
                new Author() {Id = ObjectId.Parse("5e4442c28ae77cb23386b911"), AuthorId = 1, Name = "Name 1", Surname = "Surname 1"},
                new Author() {Id = ObjectId.Parse("5e4442c28ae77cb23386b912"), AuthorId = 2, Name = "Name 2", Surname = "Surname 2"},
                new Author() {Id = ObjectId.Parse("5e4442c28ae77cb23386b913"),AuthorId = 3, Name = "Name 4", Surname = "Surname 3"}
            };
        }

        public IQueryable<Author> GetAll()
        {
            return _authors.AsQueryable();
        }
    }

EN

回答 1

Stack Overflow用户

发布于 2021-01-18 02:07:36

当您绑定ObjectId类型并注册序列化的

代码语言:javascript
复制
builder.BindRuntimeType<ObjectId, IdType>();
builder.AddTypeConverter<ObjectId, string>(o => o.ToString());
builder.AddTypeConverter<string,ObjectId>(o => ObjectId.Parse(o));

您可以在此处的文档中阅读有关标量的更多信息:https://chillicream.com/docs/hotchocolate/defining-a-schema/scalars/#custom-converter

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

https://stackoverflow.com/questions/64648326

复制
相关文章

相似问题

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