【问题标题】:Hot Chocolate Schema no Loading热巧克力模式没有加载
【发布时间】:2022-01-04 12:10:51
【问题描述】:

我正在尝试使用 Hot Chocolate 12.3.2 在 .NET 中实现 GraphQL API。我能够让项目构建和运行。尝试测试我的查询 CakePop 时,浏览器加载正常,但不存在架构。

startup.cs

namespace Application
{
  public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    [Obsolete]
    public void ConfigureServices(IServiceCollection services)
        {
            services.AddGraphQLServer()
                    .AddQueryType<Query>()
                    .AddType<BillType>().AddGraphQL()
                    .BindRuntimeType<DateOnly, DateType>()
                    .AddTypeConverter<DateOnly, DateTime>(from => DateTime.Parse(from.ToString()))
                    .AddTypeConverter<DateTime, DateOnly>(from => DateOnly.FromDateTime(from));
            services.AddGraphQL(sp => SchemaBuilder.New().AddServices(sp).Create())
                            .AddPooledDbContextFactory<WeVoteContext>(b =>b.UseInMemoryDatabase("Test"));
        }

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

            app
                .UseRouting()
                .UseEndpoints(endpoints =>
                {
                  endpoints.MapGraphQL().WithOptions(new GraphQLServerOptions
                  {
                        EnableSchemaRequests = true,
                        Tool = {
                            Enable = env.IsDevelopment()
                        }
                  });
                });
        }
    }
}

query.cs

namespace Application
{
  public class Query
  {
    static ApplicationContext db = new ApplicationContext();
    public IEnumerable<LegiscanModelBill> GetBills(int N)
    {
      return db.LegiscanModelBills.Take(N).AsEnumerable();
    }

  }
}

Models/LegiscanModelBill.cs

namespace Application.Models
{
  public partial class LegiscanModelBill
    {
        [GraphQLIgnore]
        public int Id { get; set; }
        public string? BillNumber { get; set; }
        public string? ChangeHash { get; set; }
        public string? Url { get; set; }

        [GraphQLIgnore]
        public DateOnly? StatusDate { get; set; }
        public int? StatusId { get; set; }

        [GraphQLIgnore]
        public DateOnly? LastActionDate { get; set; }
        public string? LastAction { get; set; }
        public string? Title { get; set; }
        public string? Description { get; set; }
        [GraphQLIgnore]
        public DateTime CreatedAt { get; set; }
        [GraphQLIgnore]
        public DateTime UpdatedAt { get; set; }
        public int? SessionId { get; set; }
        public int? UpvotesCount { get; set; }
        public int? BodyId { get; set; }
        public DateTime? LegiscanDataUpdatedAt { get; set; }
        public int? BillId { get; set; }
        public int? CommentsCount { get; set; }
        public double? Hotness { get; set; }
        public string? Texts { get; set; }
        public int? CommitteeId { get; set; }
        public int? BillTypeId { get; set; }
        public string? StateLink { get; set; }
    }

  public class BillType : ObjectType<LegiscanModelBill>
  {

  }
}

日志中没有任何内容

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development

更新服务

        public void ConfigureServices(IServiceCollection services)
        {
            services
                .AddPooledDbContextFactory<WeVoteContext>(b => b.UseInMemoryDatabase("Test"))
                .AddGraphQLServer()
                .AddQueryType<Query>()
                .AddType<BillType>();
            // .BindRuntimeType<DateOnly, DateType>()
            // .AddTypeConverter<DateOnly, DateTime>(from => from.ToDateTime(default))
            // .AddTypeConverter<DateTime, DateOnly>(from => DateOnly.FromDateTime(from.Date));
        }

【问题讨论】:

    标签: c# .net graphql hotchocolate


    【解决方案1】:

    设置代码似乎有问题...您正在混合旧版 API 和新的配置 API。

    services
        .AddPooledDbContextFactory<WeVoteContext>(b =>b.UseInMemoryDatabase("Test"))
        .AddGraphQLServer()
        .AddQueryType<Query>()
        .AddType<BillType>()
        .BindRuntimeType<DateOnly, DateType>()
        .AddTypeConverter<DateOnly, DateTime>(from => from.ToDateTime(default))
        .AddTypeConverter<DateTime, DateOnly>(from => DateOnly.FromDateTime(from.Date));
    
    

    在 12.4.0-preview.8 中,我们提供了对 TimeOnlyDateOnly 的支持。

    当您移至 12.4 时,请移除显式绑定和转换器。

    此外,您可以再次使用此配置删除 GraphQLIgnore 属性。

    【讨论】:

    • 谢谢,我猜我无法正确获取订单。它不会让我将它们全部链接在一起。我仍然没有加载架构。我在升级 HotChocolate 后发布了更新 ConfigureServices
    • 可以点击bcp中的刷新按钮吗?
    猜你喜欢
    • 2022-10-25
    • 2020-12-22
    • 2021-11-24
    • 2020-08-21
    • 2020-11-13
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    相关资源
    最近更新 更多