簡體   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