我正在嘗試使用 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));
}
uj5u.com熱心網友回復:
設定代碼似乎有問題……您正在混合舊 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 中,我們提供了對TimeOnly和 的支持DateOnly。
當您移動到 ??12.4 時,請洗掉顯式系結和轉換器。
此外,您可以GraphQLIgnore再次使用此配置洗掉屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367395.html
