【问题标题】:Problem with introspection of GraphQL ASP.NET Core Web API ControllerGraphQL ASP.NET Core Web API 控制器的自省问题
【发布时间】:2019-11-13 13:27:22
【问题描述】:

我看不到我的 GraphQL 图表的架构。当我使用 Web API GraphQL 控制器作为端点时,内省不起作用。

我目前已尝试使用 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; }
}

还有永远加载不完的图片

【问题讨论】:

  • 请分享您在控制器上的 Post 方法以及您在使用 GraphiQL/Playground UI 时遇到的问题的屏幕截图。 TIA
  • 稍后我会提供大量信息
  • 问题是模式从未加载,查询类型的自动完成没有任何建议。当我发送请求时,控制器本身就会工作。
  • 查看您的开发控制台是否抛出任何错误。我从来没有遇到过这个问题。你可以检查这个 repo 看看你是否遗漏了什么 - github.com/fiyazbinhasan/GraphQLCore

标签: c# asp.net-core graphiql graphql.net


【解决方案1】:

聚会可能会迟到,但这里有一个 .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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    相关资源
    最近更新 更多