http://localhost:49915/api/graphql not working on updating the Route of GraphQL controller

1.4k Views Asked by At

I updated the Route value from [Route("[controller]")] to [Route("api/graphql")] for the below mentioned GraphQLController.

[Route("api/graphql")]
public class GraphQLController : Controller
{
    private readonly IDocumentExecuter _documentExecuter;
    private readonly ISchema _schema;
    public GraphQLController(ISchema schema, IDocumentExecuter documentExecuter)
    {
        _schema = schema;
        _documentExecuter = documentExecuter;
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody] GraphQLQuery query)
    {
        if (query == null)
        {
            throw new ArgumentNullException(nameof(query));
        }

        if (string.IsNullOrWhiteSpace(query.Query))
        {
            throw new ExecutionError("A query is required.");
        }

        var inputs = query.Variables.ToInputs();
        var executionOptions = new ExecutionOptions{Schema = _schema, Query = query.Query, Inputs = inputs};
        var result = await _documentExecuter.ExecuteAsync(executionOptions).ConfigureAwait(false);
        if (result.Errors?.Count > 0)
        {
            return BadRequest(result);
        }

        return Ok(result);
    }
}

Here goes the Startup.cs which has the code for Graphiql: //Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration
    {
        get;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        //services.AddDbContext<NHLStatsContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:NHLStatsDb"]));
        services.AddDbContext<NHLStatsContext>(options => options.UseSqlServer(Configuration.GetConnectionString("NHLStatsDb")));
        services.AddTransient<IPlayerRepository, PlayerRepository>();
        services.AddTransient<ISkaterStatisticRepository, SkaterStatisticRepository>();
        services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
        services.AddSingleton<NHLStatsQuery>();
        services.AddSingleton<NHLStatsMutation>();
        services.AddSingleton<PlayerType>();
        services.AddSingleton<PlayerInputType>();
        services.AddSingleton<SkaterStatisticType>();
        var sp = services.BuildServiceProvider();
        services.AddSingleton<ISchema>(new NHLStatsSchema(new FuncDependencyResolver(type => sp.GetService(type))));
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, NHLStatsContext db)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseGraphiQl();
        app.UseMvc();
        db.EnsureSeedData();
    }
}

With the above changes I executed the APIServer and then navigated to the url: http://localhost:49915/api/graphql and found the error with message: This localhost page can’t be found

If I try to open the URL : http://localhost:49915/graphql, it opens the Graphiql editor but with schema missing on the left hand side of the Graphiql editor.

Can anyone help me to fix this issue?

1

There are 1 best solutions below

0
On

In your GraphQLController, add a HttpGet method to handle GET requests.

[HttpGet("")]
public async Task<ContentResult> ExecuteGET([FromQuery] string query, [FromQuery] string variables = null, [FromQuery] string operationName = null)
 {
    //Put your execute code here...
}

This would allow your API to support queries via a Url.

For example, here is an example Url that would return the id from users.

http://localhost:49915/graphql?query={users{id}}