2

I can't see the schema of my GraphQL Graph. The introspection is not working when I use Web API GraphQL Controller as endpoint.

I've currently tried with GraphiQl and the UI.Playground library

[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. I'm currently using Insomnia Client which fetches the schema, but GraphiQL and GraphQL.Server.Ui.Playground can't do the job.
I'm using GraphQL.NET 2.4.0 by Joe McBride

[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 enter image description here

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
Stefan PEev
  • 481
  • 5
  • 17
  • please share your Post method on the controller and also a screenshot of the problem you are having with the GraphiQL/Playground UI. TIA – Fiyaz Hasan Jul 03 '19 at 12:59
  • I'll provide extensive information later – Stefan PEev Jul 03 '19 at 13:23
  • The problem is that the schema is never loaded, and the autocomplete of the query types is not suggesting anything. The controller itself works when i send request. – Stefan PEev Jul 03 '19 at 20:43
  • See if your dev console is throwing any error or not. I never faced this problem. You can check this repo and see if you are missing something - https://github.com/fiyazbinhasan/GraphQLCore – Fiyaz Hasan Jul 06 '19 at 10:54

1 Answers1

0

May be late to the party, but here is a possible solution for .NET Core 3.0 Web API.

Fix CORS and add default GQL endpoint in Startup.cs

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

[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;
}
Anonymous
  • 1,823
  • 2
  • 35
  • 74