繁体   English   中英

GraphQL ASP.NET Core Web API Controller 自省问题

[英]Problem with introspection of GraphQL ASP.NET Core Web API Controller

我看不到我的 GraphQL Graph 的架构。 当我使用 Web API GraphQL Controller 作为端点时,内省不起作用。

我目前已尝试使用 GraphiQl 和 UI.Playground 库

[Route("graphql")]
[ApiController]
public class GraphQLController : ControllerBase

我希望使用 GraphQL.NET 库提供的自省来查看模式和类型,但不幸的是我没有。 我目前正在使用 Insomnia Client 来获取模式,但 GraphiQL 和 GraphQL.Server.Ui.Playground 无法完成这项工作。
我正在使用 Joe McBride 的 GraphQL.NET 2.4.0

[HttpPost]
public async Task<IActionResult> PostAsync([FromBody]GraphQLQuery query)

在哪里

public class GraphQLQuery
{
    public string OperationName { get; set; }
    public string NamedQuery { get; set; }
    public string Query { get; set; }
    public Newtonsoft.Json.Linq.JObject Variables { get; set; }
}

和永无止境的加载图像在此处输入图片说明

可能会迟到,但这里是 .NET Core 3.0 Web API 的可能解决方案。

修复 CORS 并在 Startup.cs 中添加默认 GQL 端点

public void ConfigureServices(IServiceCollection services)
{
  services
    .AddGraphQL(o => o.ExposeExceptions = true)
    .AddGraphTypes(ServiceLifetime.Scoped);
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  app.UseDeveloperExceptionPage();
  app.UseCors(o => o.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());  // CORS settings
  app.UseRouting();
  app.UseEndpoints(o => o.MapControllers());

  app.UseGraphQLPlayground(new GraphQLPlaygroundOptions 
  { 
    GraphQLEndPoint = "/services/queries/groups"  // default GraphQL endpoint  
  });
}

如果 GQL playground 仍然没有命中您的控制器,请尝试动态参数

[HttpPost]
[Route("services/queries/groups")]
public async Task<dynamic> Items([FromBody] dynamic queryParams)
{
  var schema = new Schema
  {
    Query = new GroupsQuery() // create query and populate it from dynamic queryParams
  };

  var response = await schema.ExecuteAsync(o =>
  {
    //o.Inputs = queryParams.variables;
    o.Query = queryParams.query;
    o.OperationName = queryParams.operationName;
    o.UserContext = new Dictionary<string, dynamic>();
    o.ValidationRules = DocumentValidator.CoreRules;
    o.ExposeExceptions = true;
    o.EnableMetrics = true;
  });

  return response;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM