I'm trying to implement a GraphQL API that in .NET using Hot Chocolate 12.3.2. I'm able to get the project to build and run. When attempting to test my query CakePop the browser loads fine but there is no schema present.
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>
{
}
}
Nothing is in the logs
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
Updated Service
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));
}
CodePudding user response:
The setup code seems to have an issue ... you are mixing legacy API and the new configuration 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));
With 12.4.0-preview.8 we have shipped support for TimeOnly
and DateOnly
.
When you move to 12.4 remove the explicit binding and the converters.
Also, you can remove the GraphQLIgnore
attributes with this configuration again.