运行 ASP.NET Core 7.0。 & GraphQL 7.6.1.
我正在尝试使用 GraphQL.NET 创建模式优先 GraphQL 的最小可行示例,但查询无法解析。 GraphQL 在控制台中解析,但不在 API 中解析,因此可能是 DI 问题...
我不断收到消息
尝试解析字段“用户”时出错
架构
type Query {
user: User
}
type User {
id: String!
name: String!
}
用户.cs
public class User
{
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
}
public class Query
{
[GraphQLMetadata("user")]
public User GetUser()
{
return new User { Id = "1", Name = "Simpleton" };
}
}
程序.cs
// Get the schema information
FileInfo fileInfo = new(Path.Join(AppContext.BaseDirectory, "schema.gql"));
var schemaText = await GetSchema(fileInfo);
var schema = Schema.For(schemaText,
sb =>
{
sb.Types.Include<Query>();
});
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGraphQL(b => b
.AddSchema(schema) // schema
.AddSystemTextJson()); // serializer
builder.Services.AddSingleton<IDataService, DataService>();
builder.Services.AddLogging(b => b.AddConsole());
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseGraphQLPlayground();
}
app.UseGraphQL("/graphql"); // url to host GraphQL endpoint
await app.RunAsync();
简单修复,只需将属性查询类注册为服务即可:
builder.Services.AddSingleton<Query>();