简体   繁体   English

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

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

I can't see the schema of my GraphQL Graph.我看不到我的 GraphQL Graph 的架构。 The introspection is not working when I use Web API GraphQL Controller as endpoint.当我使用 Web API GraphQL Controller 作为端点时,内省不起作用。

I've currently tried with GraphiQl and the UI.Playground library我目前已尝试使用 GraphiQl 和 UI.Playground 库

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

I expect to see the schema and the types using introspection provided by the GraphQL.NET library, but unfortunately I don't.我希望使用 GraphQL.NET 库提供的自省来查看模式和类型,但不幸的是我没有。 I'm currently using Insomnia Client which fetches the schema, but GraphiQL and GraphQL.Server.Ui.Playground can't do the job.我目前正在使用 Insomnia Client 来获取模式,但 GraphiQL 和 GraphQL.Server.Ui.Playground 无法完成这项工作。
I'm using GraphQL.NET 2.4.0 by Joe McBride我正在使用 Joe McBride 的 GraphQL.NET 2.4.0

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

Where在哪里

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; }
}

And image of never ending loading和永无止境的加载图像在此处输入图片说明

May be late to the party, but here is a possible solution for .NET Core 3.0 Web API.可能会迟到,但这里是 .NET Core 3.0 Web API 的可能解决方案。

Fix CORS and add default GQL endpoint in Startup.cs修复 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  
  });
}

If GQL playground still doesn't hit your controller, try dynamic parameter如果 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