1

我将下面提到的 GraphQLController 的 Route 值从 [Route("[controller]")] 更新为 [Route("api/graphql")]。

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

这是具有 Graphiql 代码的 Startup.cs://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();
    }
}

通过上述更改,我执行了 APIServer,然后导航到 url:http://localhost:49915/api/graphql并发现错误消息:找不到此本地主机页面

如果我尝试打开 URL:http://localhost:49915/graphql,它会打开 Graphiql 编辑器,但 Graphiql 编辑器的左侧缺少架构。

谁能帮我解决这个问题?

4

1 回答 1

0

在 GraphQLController 中,添加一个 HttpGet 方法来处理 GET 请求。

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

这将允许您的 API 支持通过 Url 进行的查询。

例如,这里是一个将返回idfrom的示例 Url users

http://localhost:49915/graphql?query={users{id}}
于 2018-11-20T16:16:38.200 回答