简体   繁体   English

使用 HotChocolate 按 ObjectId 类型过滤

[英]Filtering by ObjectId type with HotChocolate

I am trying to create a Graphql Schema and schema is crating but I cannot see a filtering option for Id (which is MongoDb ObjectId type).我正在尝试创建一个 Graphql 架构并且架构正在创建,但我看不到 Id(这是 MongoDb ObjectId 类型)的过滤选项。 Could you tell me what I am missing?你能告诉我我缺少什么吗? I would like to filter by Id as well but there is no like that option on created schema.我也想按 Id 过滤,但在创建的架构上没有类似的选项。 You can see my test implementation below for that.你可以在下面看到我的测试实现。

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

在此处输入图片说明

This should work when you bind the ObjectId type and register a serialised当您绑定 ObjectId 类型并注册序列化

builder.BindRuntimeType<ObjectId, IdType>();
builder.AddTypeConverter<ObjectId, string>(o => o.ToString());
builder.AddTypeConverter<string,ObjectId>(o => ObjectId.Parse(o));

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM