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

Why "IApplicationBuilder" couldn't find definition for "UseGraphQL"?

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

server is not reachable

我已经交叉检查了项目文件,我想问题出在 Startup.cs 文件上。我正在尝试在 Configure 函数中使用 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!");
                 });
            });
        }
    }
}

完整代码: GitHub

你能这样修改你的代码吗

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

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

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