为什么“IApplicationBuilder”找不到“UseGraphQL”的定义?

Ani*_*wat 2 graphql asp.net-core hotchocolate

我是 GraphQL 新手。每当我运行我的项目时它都会显示

服务器无法访问

我交叉检查了项目文件,我猜问题出在该Startup.cs文件上。我尝试app.UseGraphQL在配置函数中使用,但 IDE 无法建议我使用正确的库。

这是我的代码:

using GraphQLMutationBasicCRUD.IService;
using GraphQLMutationBasicCRUD.Service;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using HotChocolate;
using GraphQLMutationBasicCRUD.GraphQL;
using HotChocolate.AspNetCore;
using HotChocolate.AspNetCore.Playground;
using Microsoft.AspNetCore.Http;
namespace GraphQLMutationBasicCRUD
{
    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.AddSingleton<IGroupService, GroupService>();
            services.AddSingleton<IStudentService, StudentService>();

            services.AddGraphQL(x=> SchemaBuilder.New()
                .AddServices(x)
                .AddType<GroupType>()
                .AddType<StudentType>()
                .AddQueryType<Query>()
                .AddMutationType<Mutation>()
                .Create()
                );
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UsePlayground(new PlaygroundOptions
                {
                    QueryPath = "/api",
                    Path = "/Playground"
                });
            }

            app.UseGraphQL("/ api");
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                 {
                     await context.Response.WriteAsync("Hello World!");
                 });
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

完整代码: GitHub

Anu*_*raj 5

你能像这样修改你的代码吗?

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/", async context =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    endpoints.MapGraphQL();
});
Run Code Online (Sandbox Code Playgroud)

我认为你不需要app.UseGraphQL("/api");

您可以浏览您的 GraphQL 客户端 -https://localhost:5001/graphql/