asp.net-core - “没有可用的架构”Graphiql .net 核心 api
问题描述
我正在尝试在 .net 核心中开发一个简单的 graphql api。当我运行应用程序时,我在文档资源管理器中看不到任何架构。
一切似乎都是正确的。我找不到问题。
.net 核心版本 3.1
graphql 3.2 版
GraphiQl 2.0 版
代码部分如下
模型
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
类型
public class CustomerType : ObjectGraphType<Customer>
{
public CustomerType()
{
Field(x => x.Id);
Field(x => x.Name);
}
}
询问
public class GraphQuery : ObjectGraphType
{
public GraphQuery()
{
Field<ListGraphType<CustomerType>>(
"customers",
resolve: context =>
{
return new List<Customer>
{
new Customer
{
Id=1,
Name="Faruk"
}
};
});
}
}
架构
public class GraphSchema : Schema
{
public GraphSchema(IServiceProvider provider)
: base(provider)
{
Query = provider.GetRequiredService<GraphQuery>();
}
}
控制器
namespace GqlDemo2.Api.Controllers
{
[ApiController]
[Route("graphql")]
public class GraphqlController : ControllerBase
{
private readonly IDocumentExecuter _documentExecuter;
private readonly ISchema _schema;
public GraphqlController(ISchema schema, IDocumentExecuter documentExecuter)
{
_schema = schema;
_documentExecuter = documentExecuter;
}
[HttpPost]
public async Task<ActionResult> PostAsync([FromBody] GraphQLQuery query)
{
if (query == null) { throw new ArgumentNullException(nameof(query)); }
var inputs = query.Variables.ToInputs();
var executionOptions = new ExecutionOptions
{
Schema = _schema,
Query = query.Query,
Inputs = inputs
};
var result = await _documentExecuter.ExecuteAsync(executionOptions).ConfigureAwait(false);
if (result.Errors?.Count > 0)
{
return BadRequest(result);
}
return Ok(result);
}
}
}
启动
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.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHttpContextAccessor();
services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
services.AddSingleton<CustomerType>();
services.AddSingleton<GraphQuery>();
var sp = services.BuildServiceProvider();
services.AddSingleton<ISchema>(new GraphSchema(new FuncServiceProvider(type => sp.GetService(type))));
}
// 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.UseGraphiQl("/graphiql");
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
解决方案
清除您的浏览器缓存和 cookie,因为这解决了我的问题