当前位置: 首页 > 知识库问答 >
问题:

http://localhost:49915/api/graphql无法更新GraphQL控制器的路由

皇甫才良
2023-03-14

对于下面提到的GraphQLController,我将Route值从[Route(“[control]”)]更新为[Routh(“api/grapql”)]。

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

这里是启动。cs,具有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();
    }
}

做了上述更改后,我执行了APIServer,然后导航到URL:http://localhost:49915/API/graph QL,发现错误消息:找不到这个localhost页面

如果我尝试打开URL:http://localhost:49915/graphql,它会打开graph QL编辑器,但是在graph QL编辑器的左侧缺少模式。

有人能帮我解决这个问题吗?

共有1个答案

锺离声
2023-03-14

在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进行的查询。

例如,下面是一个示例 URL,它将从用户返回 id

http://localhost:49915/graphql?query={users{id}}
 类似资料:
  • graphql-api graphql-api helps you implement a robust GraphQL API in Haskell. By the time a query makes it to your handler you are dealing with strong, static types that make sense for your problem dom

  • graphql-api-koa GraphQL execution and error handling middleware written from scratch for Koa. Setup To install graphql-api-koa and the graphql peer dependency with npm, run: npm install graphql-api-ko

  • AWS Lambda 上可以运行不同的语言,提供不同语言的运行环境。这也就意味着,它不仅可以跑 Express 来提供一个 RESTful API,它也可以运行各式各样的 Node.js 库,比如说 GraphQL。 GraphQL是一种API查询语言,是一个对自定义类型系统执行查询的服务端运行环境。我们可以编写一个使用 GraphQL 编写一个查询: { me { name }

  • 使用REST,我们可以使用Swagger、RAML或其他技术来记录我们的API,并生成一个HTML文档,我们的消费者可以阅读该文档,而无需与服务器进行任何交互。 GraphQL是否存在类似的情况?是否有任何方法生成资源和属性的文档?

  • GraphQL/REST API Demo A demo of what an equivalent REST API and GraphQL API look like. This code is used in the first chapter of The GraphQL Guide by John Resig and Loren Sands-Ramshaw. Here's the Gra

  • Setup $ yarn install && open http://localhost:4000 && yarn run start Medium: https://medium.com/@wesharehoodies/how-to-setup-a-powerful-api-with-nodejs-graphql-mongodb-hapi-and-swagger-e251ac189649?